0
votes
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;

The output is

nil nil

1 nil

_color and _animation_index are not defined when car.new is called:

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

end

Why hasn't the animation_index value changed after assignment?

UPD: I can't assgn to variables with names like rranimation_index too.

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

is:

1 nil

It's not likely to be caused by already used global variables names.

1
unexpected symbol near local - user2136963
Fix your compiler errors first. - Nicol Bolas

1 Answers

0
votes

All the variables were nils before end of table creation. Solved by working with vars after it:

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;