Help with tables & spawning new enemies.

Sorry I know this is really basic/lame but it is holding me back. I’m using a function to iterate through a table and spawn a new enemy.

[lua]local enemyTable ={}
loadShip = function()

smallShip1 = {}
smallShip1.image = “shipSmall.png”
smallShip1.x = 1500
smallShip1.y = 172
smallShip1.s = 0.3 – Speed
smallShip1.d = “left” – Direction
smallShip1.h = 200 – Health
smallShip1.myName = “smallShip1”
table.insert(enemyTable, smallShip1)

end

spawnEnemies = function()

enemyProp = enemyTable[math.random(1, #enemyTable)]
enemy = display.newImage(enemyProp.image)
enemy.x = enemyProp.x
enemy.y = enemyProp.y
enemy.myName = enemyProp.myName
enemy.s = enemyProp.s
enemy.d = enemyProp.d
enemy.h = enemyProp.h

enemy:setReferencePoint( display.CenterReferencePoint )
physics.addBody( enemy, “static”, { isSensor = true, shape = smallShipShape } )
enemyGroup:insert(enemy)

enemyShipCount = enemyShipCount + 1;

return enemy

end

loadEnemyShips = function()

if enemyShipCount <= 1 then

spawnEnemies()

end

end[/lua]
When the enemy is spawned I use a basic enterFrame listener to move the enemy around and control it’s particles and a few other little things. For example

[lua]enemy.x = enemy.x - enemy.s [/lua]
My problem is when I spawn more enemies the properties move over to the new “enemy” and the old ones stop moving. What are they now called inside the script? I know this is all tied up in the name “enemy” but I can’t wrap my brain around how to fix it.

Any suggestions?

Thanks heaps [import]uid: 26289 topic_id: 15011 reply_id: 315011[/import]

@renjit,

you might want to fix line 3 as
enemy[#enemy+1] = display.newImage(enemyProp.image)[/code]you are missing the #, which would make a big difference in the code.cheers,?:) [import]uid: 3826 topic_id: 15011 reply_id: 55463[/import]

you should create individual handles for each enemy. for that you may use tables for storing enemies.

eg:
[lua]local enemy = {}
spawnEnemies = function()
enemy[#enemy + 1] = display.newImage(enemyProp.image)


end[/lua]
now you can get to individual enemies like enemy[1],enemy[2] etc.

let me know if you want further help on this… :slight_smile:

[import]uid: 71210 topic_id: 15011 reply_id: 55459[/import]

Yes Jayant. it was a typo. I edited it. Thanks for pointing it out. [import]uid: 71210 topic_id: 15011 reply_id: 55474[/import]

@renvis Thanks a lot, this looks like exactly what I need, and what i’ve been searching for! Although my problem now is that the properties don’t apply to “enemy” anymore?

For instance my code now looks like

[lua]local enemy = {}

spawnEnemies = function()

enemyProp = enemyTable[math.random(1, #enemyTable)]
enemy[#enemy + 1] = display.newImage(enemyProp.image)
enemy.x = enemyProp.x
enemy.y = enemyProp.y
enemy.myName = enemyProp.myName
enemy.s = enemyProp.s
enemy.d = enemyProp.d
enemy.h = enemyProp.h

physics.addBody( enemy, “static”, { isSensor = true, shape = smallShipShape } )
enemyGroup:insert(enemy)

enemyShipCount = enemyShipCount + 1;
enemy.collision = enemyCollision

enemy:addEventListener( “collision”, enemy )

return enemy

end [/lua]

So the, enemy.x = enemyProp.x, etc doesn’t work anymore.

What is the proper syntax for this? Should I put something like

[lua]enemy[1-10].x = enemyProp.x

or

enemy[#enemy + 1].x = enemyProp.x[/lua]

Cheers very much appreciated [import]uid: 26289 topic_id: 15011 reply_id: 55628[/import]

Once you spawn the enemy what are they supposed to do ? what is ur expected behavior ?

you have to control each enemy separately using there handle like enemy[1], enemy[2] etc.
if you want to do it for all together use a loop
[lua]for i =1, #enemy,1
–do something
enemy[i].x = …
end[/lua] [import]uid: 71210 topic_id: 15011 reply_id: 55631[/import]

It looks like SpawnEnemies job is to create one enemy and it gets called multiple time.

Your spawnenemies needs one small change.

change:

enemy = display.newImage(enemyProp.image)

to

local enemy = display.newImage(enemyProp.image)

This will keep “enemy” scoped to that function. Right now you are overwriting a global variable called “enemy” on each call. By making it local, you will get a new variable on each time and that variable gets returned in the return call.

Now your loadEnemies function needs to do something with the enemy object that’s being returned. That would be a good place to have a table called enemies and you’re keeping track of each returned enemy:

  
enemies = {}  
  
loadEnemyShips = function()  
   
 if enemyShipCount \<= 1 then  
   
 enemies[#enemies + 1] = spawnEnemies()  
   
 end  
   
end  

[import]uid: 19626 topic_id: 15011 reply_id: 55632[/import]

You might want to read up on this article here to get a better understanding.

As for a simple oneline fix for your problem,

the thing you are doing wrong is

  1. you are creating an array outside of the spawn function
  2. you are populating the array inside of the function and changing the x, y for the array than for the element

yes, you have kind of identified the issue yourself where towards the end of your question, you have got a solution.

Now, this is what *I recommend*,
always use a reference, it is better to work with. so your code can look a bit like

local enemy = {}   
  
spawnEnemies = function()  
   
 local enemyProp = enemyTable[math.random(1, #enemyTable)]  
 enemy[#enemy+1] = display.newImage(enemyProp.image)  
 local element = enemy[#enemy+1]  
  
 element.x = enemyProp.x  
 element.y = enemyProp.y  
 element.myName = enemyProp.myName  
 element.s = enemyProp.s  
 element.d = enemyProp.d  
 element.h = enemyProp.h  
  
 physics.addBody( enemy, "static", { isSensor = true, shape = smallShipShape } )  
 enemyGroup:insert(element)  
  
 enemyShipCount = enemyShipCount + 1;  
 element.collision = enemyCollision  
  
 element:addEventListener( "collision", element )  
 return element   
end  

that should work for you, as what you were adding to the display group was not the display item but an array.

It is easy to kind of get a bit lost with the flexibility offered by lua in comparison to other languages.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 15011 reply_id: 55634[/import]

Thanks a bunch guys! As always Jayantv and Robmiracle, you guys are absolute legends. Renvis too, cheers mate. I’ve only been coding a few months so still grappling with basic stuff but hey everyday I’m getting a bit more confident and you guys are making it a lot easier.

I’ve ended up using Rob’s method, with a splash of Renvis thrown in, so my code looks like

[lua]spawnEnemies = function()

enemyProp = enemyTable[math.random(1, #enemyTable)]
local enemy = display.newImage(enemyProp.image)
enemy.x = enemyProp.x
enemy.y = enemyProp.y
enemy.myName = enemyProp.myName
enemy.s = enemyProp.s * math.random(5)
enemy.d = enemyProp.d
enemy.h = enemyProp.h

physics.addBody( enemy, “static”, { isSensor = true, shape = smallShipShape } )
enemyGroup:insert(enemy)
enemyShipCount = enemyShipCount + 1;
enemy.collision = enemyCollision
enemy:addEventListener( “collision”, enemy )

return enemy

end

enemies = {}

loadEnemyShips = function()

–if enemyShipCount == 0 then

enemies[#enemies + 1] = spawnEnemies()

–end

end[/lua]
And then in my main game loop I’m moving the ships with

[lua]if enemies[1] then

enemies[1].x = enemies[1].x - enemies[1].s

end

if enemies[2] then

enemies[2].x = enemies[2].x - enemies[2].s

end

if enemies[3] then

enemies[3].x = enemies[3].x - enemies[3].s

end

if enemies[4] then

enemies[4].x = enemies[4].x - enemies[4].s

end

if enemies[5] then

enemies[5].x = enemies[5].x - enemies[5].s

end[/lua]

Cheers guys, too much [import]uid: 26289 topic_id: 15011 reply_id: 55641[/import]

Actually change that last part i updated the loop to just use

[lua] for i =1, #enemies,1 do

enemies[i].x = enemies[i].x - enemies[i].s

end[/lua]
Grand [import]uid: 26289 topic_id: 15011 reply_id: 55642[/import]

you may use this inside your main game loop.so that you don;t have to hardcode things and can have more than 5 enemies.
[lua]for i=1,#enemies,1 do
enemies[i].x = enemies[i].x - enemies[i].s
end[/lua]
* Good, you figured it out before I posted… :slight_smile: [import]uid: 71210 topic_id: 15011 reply_id: 55643[/import]