作业无法工作 Lua(corona SDK)

本地变量 _color 和 _animation_index 在调用 car.new 时未定义:

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 nil

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

点赞
用户2136963
用户2136963

在创建表格结束前,所有变量都是 nil。通过在创建表格之后处理变量来解决:

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;
2013-03-10 15:38:09