2013-03-10 58 views
0
local car={}; 
local car_mt = { __index=car }; 
function car.new(_x, _y,_color,_animation_index) 
    local ncar= 
    { 
     x=_x or 0; 
     y=_y or 0; 
     color=_color or 0x005500; 

     print(_animation_index,animation_index); 
     animation_index=(_animation_index or 1); 
     print((_animation_index or 1),animation_index); 
    } 
    return setmetatable(ncar,car_mt); 
end 
return car; 

輸出是分配不工作的Lua(電暈SDK)

零零

1零

_Color並且當car.new是_animation_index沒有定義所謂:

local pcar=require("car") 
... 
function scene:enterScene(event) 
    local group = self.view 
    physics.start(); 
    local car1=pcar.new(200,200); 

end 

爲什麼在分配後沒有更改animation_index值?

UPD: 我不能用類似rranimation_index這樣的名稱來指定變量。

 rranimation_index=(_animation_index or 1); 

     rranimation_index=5; 
     print((_animation_index or 1),rranimation_index); 

是:

1零

這是不可能減去已使用全局變量的名稱引起的。

+1

本地附近的意外符號 – user2136963 2013-03-10 15:04:46

+0

首先解決編譯器錯誤。 – 2013-03-10 21:56:03

回答

0

所有變量在表創建結束前都是nils。通過與變量之後的工作來解決:

local car={}; 
local car_mt = { __index=car }; 
local C=require("_const"); 
function car.new(_x, _y,_color,_animation_index) 
    local ncar= 
    { 
     x=_x or 0; 
     y=_y or 0; 
     color=_color or 0x005500; 
     animation_index=(_animation_index or 1); 
     img; 
     frames=0; 
    } 
    function ncar:setup() 
     self.img=display.newImageRect((C.CARS_DIR..C.ANIMATIONS_CARS[self.animation_index]) or C.EMPTY_IMAGE,50,120,true); 
    end 
    ncar:setup(); 
    return setmetatable(ncar,car_mt); 
end 
return car;