[Resolved] How to I attach event listeners to spawned objects

Hello - newb here learning programming as I go.

I have 12 objects spawning at random locations and then each moving off in a random direction.

I want to be able to tap one and my player object will move to it’s location.

I’ve been able to do it with other objects, but I get errors if its one of my 12 identical objects.

I also tried to make sense of this blog post but I wasn’t able to follow it: http://blog.anscamobile.com/2011/09/how-to-spawn-objects-—-the-right-way/

Here’s the offending code:

[lua]–screen bootstrap
display.setStatusBar(display.HiddenStatusBar)

local whiteBg = display.newRect(0,0,display.contentWidth,display.contentHeight)

local player = display.newImage(“images/player.png”)
player.x = 512
player.y = 384

–Function to spawn enemies and set them moving
local function spawn()
local enemy1 = display.newImage(“images/enemy2.png”)
enemy1.x = math.random(30,994)
enemy1.y = math.random(30,738)
local function moveEnemy1()
transition.to(enemy1, {time=math.random(3000,7000), x=math.random(30,1024), y=math.random(20,768), onComplete=moveEnemy1})
end
moveEnemy1()
return enemy1
end

–Create a table to hold our spawns
local spawnTable = {}

–Spawn 8 enemies
for i = 1, 12 do
spawnTable[i] = spawn()
end[/lua]
[import]uid: 121833 topic_id: 26028 reply_id: 326028[/import]

You really need to post your code within < lua > and < /lua > tags to make it readable :wink:

For your question, you should be able to add it within your spawn function right after you do display.newImage.

Peach :slight_smile: [import]uid: 52491 topic_id: 26028 reply_id: 105384[/import]

Ah yes I forgot - edited for readability.

@Peach - you mean just put ‘enemy1:addEventListener(“tap”, attack)’ there, then make the attack function?
[import]uid: 121833 topic_id: 26028 reply_id: 105387[/import]

You should be doing it like this:

[lua]–Spawn 8 enemies
for i = 1, #spawnTable do
spawnTable[i] = display.newImage(“images/enemy2.png”)
spawnTable[i].x = math.random(30,994)
spawnTable[i].y = math.random(30,738)
spawnTable[i]:addEventListener(“touch”, doWhatever)
end[/lua]

This way for every enemy, you creating it in the for loop. It’s easier to keep it inside the for loop than bringing it into other functions.

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 26028 reply_id: 105410[/import]

Thanks for responding Jordan,

I implemented your method for spawning, which works of course. But I’m having trouble with adding the listener.

[lua]spawnTable[i]:addEventListener(“touch”, doWhatever)
– this isn’t recognised as valid by my text editor.[/lua]

[import]uid: 121833 topic_id: 26028 reply_id: 105561[/import]

Then use Sublime text [import]uid: 29181 topic_id: 26028 reply_id: 105571[/import]

I’m sorry - meant that my code stops working when I added the listener and the terminal output refers to that line in the spawnTable

[lua]–screen bootstrap
display.setStatusBar(display.HiddenStatusBar)

local whiteBg = display.newRect(0,0,display.contentWidth,display.contentHeight)

local player = display.newImage(“images/player.png”)
player.x = 512
player.y = 384

local chargeSound = audio.loadSound(“chargeUp.wav”)
local teleportSound = audio.loadSound(“teleportSound.wav”)

–enemy1 spawing & movement
local spawnTable = {}

for i = 1, 15 do
spawnTable[i] = display.newImage(“images/enemy2.png”)
spawnTable[i].x = math.random(30,994)
spawnTable[i].y = math.random(30,738)
spawnTable[i]:addEventListener(“touch”, attack)
local function moveEnemy1()
transition.to(spawnTable[i], {time=math.random(3000,7000), x=math.random(30,1024),
y=math.random(20,768), onComplete=moveEnemy1})
return true
end
moveEnemy1()
end

– player charge up
charge = 0
local function chargeUp(event)
print (charge)
if charge == 0 then
local saveX = event.target.x
local saveY = event.target.y
player:removeSelf()
player = display.newImage(“images/charged.png”)
player.x = saveX
player.y = saveY
audio.play(chargeSound)
charge = 1
print(charge)
return true
end
end

player:addEventListener(“tap”, chargeUp)

–player teleport
local function teleport(event)
if charge == 1 then
local teleportX = event.x
local teleportY = event.y
player:removeSelf()
player = display.newImage(“images/player.png”)
player.x = teleportX
player.y = teleportY
audio.play(teleportSound)
player:addEventListener(“tap”, chargeUp)
charge = 0
print(charge)
end
end
whiteBg:addEventListener(“tap”, teleport)

–player attack
local function attack(event)
if charge == 1 then
charge = 2
print (charge)
end
end
spawnTable[i]:addEventListener(“touch”, attack)[/lua] [import]uid: 121833 topic_id: 26028 reply_id: 105572[/import]

Not sure why your adding the event listener again here:
[lua]local function attack(event)
if charge == 1 then
charge = 2
print (charge)
end
end
spawnTable[i]:addEventListener(“touch”, attack)[/lua]

Besides spawnTable[i] can only be used in the for loop. Your going to have to recode the program. Also, your attack function should be above the for loop. The for loop should be at the bottom of your code, and remove line 74.

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 26028 reply_id: 105606[/import]

Not sure why your adding the event listener again here:
[lua]local function attack(event)
if charge == 1 then
charge = 2
print (charge)
end
end
spawnTable[i]:addEventListener(“touch”, attack)[/lua]

Besides spawnTable[i] can only be used in the for loop. Your going to have to recode the program. Also, your attack function should be above the for loop. The for loop should be at the bottom of your code, and remove line 74.

Regards,
Jordan Schuetz
Ninja Pig Studios [import]uid: 29181 topic_id: 26028 reply_id: 105607[/import]

I added it there because I don’t know what Im doing and making it up as I go along :wink:

That just helped at lot. Thank you so much.

[import]uid: 121833 topic_id: 26028 reply_id: 105610[/import]