【问题标题】:Assignment doesn't work Lua (corona SDK)分配不起作用 Lua (corona SDK)
【发布时间】:2013-03-10 14:55:51
【问题描述】:
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;

输出是

无无

1 无

调用car.new时没有定义_color和_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的值没有变化?

更新: 我也无法分配名称为 rranimation_index 的变量。

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

是:

1 无

这不太可能是由已使用的全局变量名称引起的。

【问题讨论】:

  • 本地附近出现意外符号
  • 先修复编译器错误。

标签: lua coronasdk


【解决方案1】:

在表创建结束之前,所有变量都是 nil。通过使用 var 解决它:

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;

【讨论】:

    猜你喜欢
    • 2014-06-22
    • 2013-06-04
    • 2015-03-20
    • 1970-01-01
    • 2012-05-08
    • 2014-06-09
    • 2014-10-29
    • 2012-01-15
    • 2013-03-23
    相关资源
    最近更新 更多