Workaround for passing obj.x/y values from an object to a listener associated with that object?

Hi all,

I’ve got a card game in the works, and I’m trying to work on a system that will make a square frame move to a selected die to highlight it.

I put a listener on each die, so that when a die is touched, it’s supposed to move the frame to positions die.x and die.y, die1.x and die1.y, etc… However, if I define the listener first, it won’t recognize the die’s coordinates, since they’re not defined yet. And if I define the die first, it won’t recognize the listener as that hasn’t been defined yet.

I’ve been reading up on passing parameters but I’m still unsure as to how to make this particular setup work (as you can see I use Director). If anyone could point me in the right direction I’d appreciate it. Thanks very much.

Here’s the code I had:

[code]local function dieRead(event)
if event.phase == “ended” then
dieFrame.x=die.x
dieFrame.y=die.y
end
end

local die = sprite.newSprite( diceSet )
die.x = 55
die.y = 55
die.value=1
die:prepare(“die”)
die:addEventListener( “touch”, dieRead )
local dieFrame = display.newImage(“dieframe.png”)
dieFrame.x = 222
dieFrame.y = 222
localGroup:insert(dieFrame)

die:addEventListener(“touch”,dieRead)[/code] [import]uid: 144359 topic_id: 31437 reply_id: 331437[/import]

Should have used event.target.x and y in lines 3-4. Since the listener is on the die object, it will refer to the die through event.target and then any other information (x,y, custom properties). Hope this helps a fellow noob! [import]uid: 144359 topic_id: 31437 reply_id: 125649[/import]

This might work.

[code]local function dieRead(event)
local t = event.target
local phase = event.phase

if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
elseif “ended” == phase then
dieFrame.x = t.x
dieFrame.y = t.y
– do whatever else you need to do, like maybe
– dieValue = t.value --this would set the value of the dice from it’s table so you can reference it
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end[/code]

Hope this helps,

Nail [import]uid: 106779 topic_id: 31437 reply_id: 125651[/import]

Should have used event.target.x and y in lines 3-4. Since the listener is on the die object, it will refer to the die through event.target and then any other information (x,y, custom properties). Hope this helps a fellow noob! [import]uid: 144359 topic_id: 31437 reply_id: 125649[/import]

This might work.

[code]local function dieRead(event)
local t = event.target
local phase = event.phase

if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
elseif “ended” == phase then
dieFrame.x = t.x
dieFrame.y = t.y
– do whatever else you need to do, like maybe
– dieValue = t.value --this would set the value of the dice from it’s table so you can reference it
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end[/code]

Hope this helps,

Nail [import]uid: 106779 topic_id: 31437 reply_id: 125651[/import]