touch function with additional arguments

So I have a class called destroy. 

I want to give the destroyer.object (display object) a touch function that decreases the health of the destroyer

QUESTION: How do you implement a touch function with additional arguments? :wacko:

Thanks in advance  :wink:

[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


local function make_money(money, income_rate)

    money = money + income_rate

    print(money);

end


local function got_touched( event , health)

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

        print(health)

    end

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

    local temp_touch_function = function() got_touched(“touch”, newDestroyer.Health) end

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

    local temp_money_timer = function() make_money(newDestroyer.money, newDestroyer.income_rate) 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



return Destroyer
[/lua]

You can’t pass in arguments to the function that way.  Most people add an attribute to the object being touched and when the event gets it, event.target is the touched object:

local brownBear = display.newImageRect("bear.png", 64, 64) brownBear.color = "brown"   local blackBear = display.newImageRect("bear.png", 64, 64) blackBear.color = "black"   local polarBear = display.newImageRect("bear.png", 64, 64) polarBear.color = "white"   local pandaBear = display.newImageRect("bear.png", 64, 64) pandaBear.color = "black and white"     local function touchBear(event)      print(event.target.color)      return true end   blackBear:addEventListener("touch", touchBear) brownBear:addEventListener("touch", touchBear) polarBear:addEventListener("touch", touchBear) pandaBear:addEventListener("touch", touchBear)

You can’t pass in arguments to the function that way.  Most people add an attribute to the object being touched and when the event gets it, event.target is the touched object:

local brownBear = display.newImageRect("bear.png", 64, 64) brownBear.color = "brown"   local blackBear = display.newImageRect("bear.png", 64, 64) blackBear.color = "black"   local polarBear = display.newImageRect("bear.png", 64, 64) polarBear.color = "white"   local pandaBear = display.newImageRect("bear.png", 64, 64) pandaBear.color = "black and white"     local function touchBear(event)      print(event.target.color)      return true end   blackBear:addEventListener("touch", touchBear) brownBear:addEventListener("touch", touchBear) polarBear:addEventListener("touch", touchBear) pandaBear:addEventListener("touch", touchBear)