Table Within A Table ? Is That The Correct Way?

I have been messing with positioning in grid like formations like the classics Space invaders, Galaxian etc.

I am able to create a table of the same instance and position it in a grid formation with a simple for loop.

I have stripped out all irrelevant code.

What I am looking at now is how to change the image on second,third,fourth and fifth rows for a different image, they will have all the same attributes as each other…

I was thinking having a table within a table but I cant really find anything on the net pointing me in the correct direction.

Anyone here give me a pointer please?

[lua]

– enemy.lua –

function enemys:init(xloc, yloc)
   
    self.image = display.newImage(“images/Enemy2.png”)
    self.image:setReferencePoint( CenterReferencePoint )
    self.image.x = xloc
    self.image.y = yloc
    self.image.name = “enemy”
    self.movement = 0.5  

  

function enemys:start()

    Runtime:addEventListener( “enterFrame”, self )
    self.image:addEventListener( “collision”, self )
  
end

function enemys.enterFrame(self, event)
    self.image.x = self.image.x + self.movement
end

[/lua]

[lua]

– main.lua –

 local enemys = require(“modules.enemy”)
        for j = 1, 5 do
        for i = 1, 11 do
        allEnemys[#allEnemys + 1] = enemys:new()
        allEnemys[#allEnemys]:init(i * 60, j* 70 +70)  
        allEnemys[#allEnemys]:start()
        end
        end

[/lua]

How about when you init each of the objects, you can pass additional arguments, like enemy type, image, etc.

allEnemys[#allEnemys]:init(i \* 60, j\* 70 +70, { type = 1, intelligence = 50 - ( j \* 10 ), speed = 50 - ( j \* 2 ) } ) 

function enemys:init(xloc, yloc, details ) self.image = display.newImage("images/Enemy" .. details.type or 1 .. ".png") self.image:setReferencePoint( CenterReferencePoint ) self.image.x = xloc self.image.y = yloc self.name = "enemy" self.movement = 0.5 self.type = details.type or 1 self.intelligence = details.intelligence or 100 self.speed = details.speed or 50 ...

@BeyondtheTech, this looks good, however when implemented I am failing to find the enemy1 image. I can see what your doing but I am missing something simple ?

btw, read some of your stuff on your website, quite informative and entertaining :slight_smile:

Sorry, I’m assuming you have 5 different enemy images (Enemy1.png, Enemy2.png, etc…) 

When you pass the type = 1 argument in the table, it will attempt to use the file Enemy 1.png for the image.

And, thanks, I’ve been meaning to update the site, but am too heavily focused on developing my current game.

@btt, yes do have 5 images that is why I can’t get what is happening now.

Iam not clued up enough to figure it out, any ideas ?

Thank you

Are you on Windows?  The files may be case-sensitive.

See if you can manually use Enemy1.png instead of using " … details.type or 1 …" and let me know if it still works.

@BTT,

yup, windows but the files are, as is. named correctly.

I passed just he Enemy1.png in but still no joy. Not sure what to do next now its really bugging me.

Sorry to take up your time.

Change everything to lowercase in Windows and your app. See if that works.

BTT,

yeah I changed all to lowercase earlier.

So all my images are in a file “images/enemy1.png”, etc etc, placed them in the root also to be sure, no luck.

Can you explain the passing in the type etc ( I do understand it but maybe I am missing something so obvious )

I could post more code if required but I dont think anything is relevant.

Are there any examples around I can look at you know of ?

Thanks

Ok, I have an update.

I couldnt get this code to work so I placed al my images in a table within the inIt of the enemy.lua, I then pass the index to display.newImage.

I created the index as math.random.

I now have enemys all random within the grid, looks quite nice :slight_smile: however I want to be able to pass the index and have each image on a seperate row.

I dont know how to do this, here is my code again:

[lua]

–enemy.lua

function enemys:init(xloc, yloc, details )
      image = {}

      image[#image + 1] = “images/enemy1.png”
      image[#image + 1] = “images/enemy2.png”
      image[#image + 1] = “images/enemy3.png”
      image[#image + 1] = “images/enemy4.png”
      image[#image + 1] = “images/enemy5.png”

    local index = math.random( 1, #image )

    self.image = display.newImage(image[index] )
    self.image:setReferencePoint( CenterReferencePoint )
    self.image.x = xloc
    self.image.y = yloc
    self.image.name = “enemy”

end

[/lua]

[lua]

local enemys = require(“modules.enemy”)
        for j = 1, 5 do
        for i = 1, 11 do
        allEnemys[#allEnemys + 1] = enemys:new()
        allEnemys[#allEnemys]:init(i * 60, j* 70 +70 )     
        allEnemys[#allEnemys]:start()
        end
     end

[/lua]

How about when you init each of the objects, you can pass additional arguments, like enemy type, image, etc.

allEnemys[#allEnemys]:init(i \* 60, j\* 70 +70, { type = 1, intelligence = 50 - ( j \* 10 ), speed = 50 - ( j \* 2 ) } ) 

function enemys:init(xloc, yloc, details ) self.image = display.newImage("images/Enemy" .. details.type or 1 .. ".png") self.image:setReferencePoint( CenterReferencePoint ) self.image.x = xloc self.image.y = yloc self.name = "enemy" self.movement = 0.5 self.type = details.type or 1 self.intelligence = details.intelligence or 100 self.speed = details.speed or 50 ...

@BeyondtheTech, this looks good, however when implemented I am failing to find the enemy1 image. I can see what your doing but I am missing something simple ?

btw, read some of your stuff on your website, quite informative and entertaining :slight_smile:

Sorry, I’m assuming you have 5 different enemy images (Enemy1.png, Enemy2.png, etc…) 

When you pass the type = 1 argument in the table, it will attempt to use the file Enemy 1.png for the image.

And, thanks, I’ve been meaning to update the site, but am too heavily focused on developing my current game.

@btt, yes do have 5 images that is why I can’t get what is happening now.

Iam not clued up enough to figure it out, any ideas ?

Thank you

Are you on Windows?  The files may be case-sensitive.

See if you can manually use Enemy1.png instead of using " … details.type or 1 …" and let me know if it still works.

@BTT,

yup, windows but the files are, as is. named correctly.

I passed just he Enemy1.png in but still no joy. Not sure what to do next now its really bugging me.

Sorry to take up your time.

Change everything to lowercase in Windows and your app. See if that works.

BTT,

yeah I changed all to lowercase earlier.

So all my images are in a file “images/enemy1.png”, etc etc, placed them in the root also to be sure, no luck.

Can you explain the passing in the type etc ( I do understand it but maybe I am missing something so obvious )

I could post more code if required but I dont think anything is relevant.

Are there any examples around I can look at you know of ?

Thanks

Ok, I have an update.

I couldnt get this code to work so I placed al my images in a table within the inIt of the enemy.lua, I then pass the index to display.newImage.

I created the index as math.random.

I now have enemys all random within the grid, looks quite nice :slight_smile: however I want to be able to pass the index and have each image on a seperate row.

I dont know how to do this, here is my code again:

[lua]

–enemy.lua

function enemys:init(xloc, yloc, details )
      image = {}

      image[#image + 1] = “images/enemy1.png”
      image[#image + 1] = “images/enemy2.png”
      image[#image + 1] = “images/enemy3.png”
      image[#image + 1] = “images/enemy4.png”
      image[#image + 1] = “images/enemy5.png”

    local index = math.random( 1, #image )

    self.image = display.newImage(image[index] )
    self.image:setReferencePoint( CenterReferencePoint )
    self.image.x = xloc
    self.image.y = yloc
    self.image.name = “enemy”

end

[/lua]

[lua]

local enemys = require(“modules.enemy”)
        for j = 1, 5 do
        for i = 1, 11 do
        allEnemys[#allEnemys + 1] = enemys:new()
        allEnemys[#allEnemys]:init(i * 60, j* 70 +70 )     
        allEnemys[#allEnemys]:start()
        end
     end

[/lua]