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;

出力は

nil nil

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の値が変更されないのはなぜですか?

UPD:rranimation_indexのような名前の変数にも割り当てることができません。

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

は:

1なし

すでに使用されているグローバル変数名が原因である可能性は低いです。

4

1 に答える 1

0

テーブルの作成が終了する前は、すべての変数が nil でした。その後にvarsを使用することで解決しました:

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-10T15:38:09.267 に答える