2013-03-10 54 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;