event phase is nil

I have a class of destoyers and I need to assign each destroyer a touch listener when it is created. Is there any way to do this?

Right now when I do it I get a nil value for my touch phase… I’m assuming it is because event has not been assigned but properly.

I have been stuck on this and could really use some help thanks  :slight_smile:

[lua]

– Destroyer.lua


local Destroyer = {}

local Destroyer_mt = { __index = Destroyer }    – metatable


– PRIVATE FUNCTIONS


local function set_referance_point(object,x,y)

    object:setReferencePoint(display.CenterReferencePoint)

    object.x=x

    object.y=y

end




– PUBLIC FUNCTIONS


function Destroyer.new(x,y)    – constructor    

    local newDestroyer = 

    {

        Health = 100,

        money = 1000,

        income_rate=5,

        object = display.newImage( “deathstar.png” )

    }

    set_referance_point(newDestroyer.object,x,y) – positions the Destroyer

    newDestroyer.object.touch = function() newDestroyer:got_touched(“touch”) end

    newDestroyer.object:addEventListener( “touch”, newDestroyer.object )

    local temp_money_timer = function() newDestroyer:make_money() end

    timer.performWithDelay( 500, temp_money_timer, -1) – gives Destroyer a runtime funciton

    return setmetatable( newDestroyer, Destroyer_mt )

end


function Destroyer:print_confirmation()

    print(  “Destroyer Has been created” )

end


function Destroyer:make_money()

    self.money = self.money + self.income_rate

end


function Destroyer:got_touched( event )

    print(event.phase)

    if(event.phase==“began”) then

        print(“lol”)

    end

end

return Destroyer
[/lua]

Think I got your mistake.

[LUA]

newDestroyer.object.touch = function(event) Destroyer:got_touched(event) end

[/LUA]

Change the line to this and it should work.

Think I got your mistake.

[LUA]

newDestroyer.object.touch = function(event) Destroyer:got_touched(event) end

[/LUA]

Change the line to this and it should work.