Connect players and enemies

Hi guys,

Please help me with this  :blink:

I am spawning players and enemies in the same function (do not ask why  :smiley: ).

local playersGroup = display.newGroup() local enemyGroup = display.newGroup() local numOfPlayersCreated = 0 local function spawn()      numOfPlayersCreated = numOfPlayersCreated + 1      local player = display.newImageRect(playersGroup, "images/player.png", 50, 50)      player. x = 50      player.y = 100      player.id = numOfPlayersCreated      local enemy = display.newImageRect(enemyGroup, "images/enemy.png", 50, 50)      enemy.x = 300      enemy.y = 200      enemy.id = player.id end

Later on each enemy attacks its own player by selecting precise group index:

self.id = index playersGroup[index]... enemyGroup[index]...

Problem is as I display.remove(enemyGroup[index]) things/group indexes mix up…

Do you have better solution how to tie together enemys and players that are spawned together (only those spawned together)?

Many thanks!

Ivan

We’re still missing what you are attempting to accomplish in the mechanics of your game. “Things mix up” isn’t very descriptive so it’s difficult to give advice. Further, there doesn’t seem to be a problem with “tying” the enemies and players together, rather what happens when they need to be “separated”. 

If you could provide some additional detail (what you are trying to achieve, behavior you are seeing, and the behavior you expect to see) it would assist greatly.

Hi Alex.

For example:

enemy.enterFrame = doSomething
Runtime:addEventListener(“enterFrame”, enemy)

function doSomething ()
local index = self.id

if self.x <= playersGroup[index].x then
playersGroup[index].alpha = 0
end

end

After when you do:
display.remove(enemyGroup[index])
enemyGroup[index] = nil

Indexes are mixed up, second enemy is impacting first player, thrid enemy is impacting second player… and so on.

From what you’re describing above (assuming the first enemy is removed in your statement), it would seem the indexes aren’t mixed up, rather decremented once an enemy is removed. This most likely has to do with how you are creating the id variable for your enemy objects. If you included some print statements in your Runtime listener, perhaps printing the index values, amount of enemies created, amount of enemies removed and so on, perhaps it would shed more light on your issue.

Basically what am I asking here:

What is another way to directly access child in displayGroup other than by its index of creation?

Accessing by child property is not possible:

enemyGroup[index] – this works
enemyGroup.id – this does not work unless you for loop entire group searching for specific id…

If you want an easier way to keep an enemy linked to a particular player, you could just set the player as a property of the enemy:

local function spawn() numOfPlayersCreated = numOfPlayersCreated + 1 local player = display.newImageRect(playersGroup, "images/player.png", 50, 50) player. x = 50 player.y = 100 player.id = numOfPlayersCreated local enemy = display.newImageRect(enemyGroup, "images/enemy.png", 50, 50) enemy.x = 300 enemy.y = 200 enemy.myPlayer = player end if self.x \<= self.myPlayer.x then self.myPlayer.alpha = 0 end

This doesn’t fix your indexing problem, which I believe is caused by using group indices rather than your own tables which you have more control over. If you delete playersGroup[4], that will cause playersGroup[5] to move down to [4] to fill the space, and [6] down to [5] etc. However the .id property you have set will not be changed, and so you will get a mismatch.

Thanks Alan!
I will try that and let you know :slight_smile:

Amazing Alan, it works, thanks! :slight_smile:

Thank you Alex! :slight_smile:

We’re still missing what you are attempting to accomplish in the mechanics of your game. “Things mix up” isn’t very descriptive so it’s difficult to give advice. Further, there doesn’t seem to be a problem with “tying” the enemies and players together, rather what happens when they need to be “separated”. 

If you could provide some additional detail (what you are trying to achieve, behavior you are seeing, and the behavior you expect to see) it would assist greatly.

Hi Alex.

For example:

enemy.enterFrame = doSomething
Runtime:addEventListener(“enterFrame”, enemy)

function doSomething ()
local index = self.id

if self.x <= playersGroup[index].x then
playersGroup[index].alpha = 0
end

end

After when you do:
display.remove(enemyGroup[index])
enemyGroup[index] = nil

Indexes are mixed up, second enemy is impacting first player, thrid enemy is impacting second player… and so on.

From what you’re describing above (assuming the first enemy is removed in your statement), it would seem the indexes aren’t mixed up, rather decremented once an enemy is removed. This most likely has to do with how you are creating the id variable for your enemy objects. If you included some print statements in your Runtime listener, perhaps printing the index values, amount of enemies created, amount of enemies removed and so on, perhaps it would shed more light on your issue.

Basically what am I asking here:

What is another way to directly access child in displayGroup other than by its index of creation?

Accessing by child property is not possible:

enemyGroup[index] – this works
enemyGroup.id – this does not work unless you for loop entire group searching for specific id…

If you want an easier way to keep an enemy linked to a particular player, you could just set the player as a property of the enemy:

local function spawn() numOfPlayersCreated = numOfPlayersCreated + 1 local player = display.newImageRect(playersGroup, "images/player.png", 50, 50) player. x = 50 player.y = 100 player.id = numOfPlayersCreated local enemy = display.newImageRect(enemyGroup, "images/enemy.png", 50, 50) enemy.x = 300 enemy.y = 200 enemy.myPlayer = player end if self.x \<= self.myPlayer.x then self.myPlayer.alpha = 0 end

This doesn’t fix your indexing problem, which I believe is caused by using group indices rather than your own tables which you have more control over. If you delete playersGroup[4], that will cause playersGroup[5] to move down to [4] to fill the space, and [6] down to [5] etc. However the .id property you have set will not be changed, and so you will get a mismatch.

Thanks Alan!
I will try that and let you know :slight_smile:

Amazing Alan, it works, thanks! :slight_smile:

Thank you Alex! :slight_smile: