1
votes

I'm try to create a new game on corona SDK i'm new in lua language, my goal is had a set of enemies in a kind of action game.

For this i think the best way is have a array to store all my enemeis in this case i use three.

So my code is :

local enemies = {}

enemy1 = display.newImageRect( "assets/images/sheep_mini.png", 60, 60 )
enemy1.anchorX = 0
enemy1.anchorY = 0
enemy1.name = 'enemy'
enemy1.id = 1
enemy1.x, enemy1.y = 28, display.contentHeight - 260
enemy1.angularVelocity = 0
enemies[1] =enemy1 



 enemy2 = display.newImageRect( "assets/images/sheep_mini.png", 60, 60 )
enemy2.anchorX = 0
enemy2.anchorY = 0
enemy1.id = 2
enemy2.name = "enemy"
enemy2.x, enemy2.y = screenW - 120, display.contentHeight - 420
enemy2.angularVelocity = 0
enemies[2] =enemy2

So after that i've a while to iterate to this enemies enemies, but when i try to get the enemies from the array i only getting this :

Mar 31 02:23:36.576: table: 0x600000a66640 Mar 31 02:23:36.577: table: 0x600000a78e00

i'm using this code for doing while :

  local len = #enemies
local i= 1
while i <= len do
    enemy1 = enemies[i]
    print(enemy1)
end

Can you help here? i'm now on corona and also on lua

thanks in advance

1
Where are you incrementing the i? - hjpotter92
You are trying to print the table value, so what you want to do exactly ? - Kumar KS
yes i'm increment the i only not copy this code, as i said i'm new in LUA so i try to get the object on each position, like i do on JavaScript. the problem is only the print? if I try accessing for example on id property i'm able to do this? - mastervv

1 Answers

1
votes

What you are trying to achieve can be done through

table.print(enemy1)

For more information I suggest you read this: Table Serialization which explains how:

functions to serialize/unserialize a table or object (usually, not always, represented as a table), which is to convert it to and from a string representation. This is typically used for display (e.g. debugging) or storing data in a file (e.g. persistence).