Function parameters with tap events?

Apologies if this is not the place to ask this… this forum is frighteningly big

But I’m trying to build a very simple version of an ingame shop, where the player can exchange credits for items, but I’m having some difficulty with working with parameters

this is what I got so far:

display.setStatusBar( display.HiddenStatusBar )  
  
local credits = 100  
  
function buyItem(price)  
 print( credits )  
end  
  
local item1 = display.newRect( 0, 0, 50, 50 )  
item1:setFillColor( 0, 0, 255 )  
item1.x = display.contentCenterX   
item1.y = display.contentCenterY  
item1:addEventListener( "tap", buyItem )  
  
function test(price)  
 print( credits - price )  
end  
  
test(50)  

Now, the test function works as it should… but why cant I do the same for my event listener? When I try to use buyItem(50) I get an error I dont understand:

Runtime error  
assertion failed  
  
stack traceback:  
[C]: ?  
[C]: in function 'assert'  
?: in function 'getOrCreateTable'  
?: in function 'addEventListener'  
?: in function'addEventListener'  
  
etc, etc...  

I have no idea how to fix this!
[import]uid: 200198 topic_id: 33910 reply_id: 333910[/import]

Hi in your code you haven’t attached a “price” to the item. I don’t think you can call item1:addEventListener( “tap”, buyItem(50) ) that will send the error you are getting I believe.

Try adding
item1.price = 50
to the item1 properties before the event listener.

At very least if this still doesnt work you could add the price property and change the function to:

function buyItem(event)
item = event.target
credits = credits - item.price
print( credits )
end

Good luck [import]uid: 131622 topic_id: 33910 reply_id: 134827[/import]

Well, I kinda found out that I can pass parameters with event listeners

[code]local function listener(param1, param2)
return function(event)
print(event.name, event.phase, param1, param2)
end
end

Runtime:addEventListener(“touch”, listener(12, 33))
Runtime:addEventListener(“tap”, listener(55, 77))[/code]

It’s as simple as that… I just wasnt aware that I needed a return function

Though what you suggested works as well! And is probably a better method to use in a shop, since its easier to read [import]uid: 200198 topic_id: 33910 reply_id: 134841[/import]

Ah ha, well I’m still learning the ropes a little too so you’ve taught me something here. I do get a bit confused with parameters so I have to keep it simple to read for myself, but is not necessarily the quickest or most dynamic.

Well done on the find. Glad it’s working. [import]uid: 131622 topic_id: 33910 reply_id: 134847[/import]

Hi in your code you haven’t attached a “price” to the item. I don’t think you can call item1:addEventListener( “tap”, buyItem(50) ) that will send the error you are getting I believe.

Try adding
item1.price = 50
to the item1 properties before the event listener.

At very least if this still doesnt work you could add the price property and change the function to:

function buyItem(event)
item = event.target
credits = credits - item.price
print( credits )
end

Good luck [import]uid: 131622 topic_id: 33910 reply_id: 134827[/import]

Well, I kinda found out that I can pass parameters with event listeners

[code]local function listener(param1, param2)
return function(event)
print(event.name, event.phase, param1, param2)
end
end

Runtime:addEventListener(“touch”, listener(12, 33))
Runtime:addEventListener(“tap”, listener(55, 77))[/code]

It’s as simple as that… I just wasnt aware that I needed a return function

Though what you suggested works as well! And is probably a better method to use in a shop, since its easier to read [import]uid: 200198 topic_id: 33910 reply_id: 134841[/import]

Ah ha, well I’m still learning the ropes a little too so you’ve taught me something here. I do get a bit confused with parameters so I have to keep it simple to read for myself, but is not necessarily the quickest or most dynamic.

Well done on the find. Glad it’s working. [import]uid: 131622 topic_id: 33910 reply_id: 134847[/import]