Help With Loops And Tables

Ok, so I posted a question in “lua programming”, but it has gone a little dead so unsure if its in the wrong department.

Thread here:

http://forums.coronalabs.com/topic/33251-table-within-a-table-is-that-the-correct-way/#entry172369

Basically I trying to create a grid of enemies like space invaders.

I can create and populate the grid ok, however I just dont get how to use the for loop and table to create a different enemy on each row.

I can get them to populate random.

I am really truley stuck on this one, I have tried loads of things but I am missing something, including sleep trying to figure it out.

If anybody would take a look at the code I posted and help me out I will be eternally grateful.

Thank you

This should do it:

[lua]

 

–enemy.lua

 

function enemys:init(xloc, yloc, row )
   

    self.image = display.newImage(“images/enemy”…row…".png" )
    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, j )     
        allEnemys[#allEnemys]:start()
        end
     end

[/lua]

@nick,sherman,

ahhh, I nearly had it yesterday using concat of the images but couldnt quite get it. BTT suggested this method also in the linked post.

Damn, I see where I went wrong:

[lua]allEnemys[#allEnemys]:init(i * 60, j* 70 +70, j ) [/code]

didnt pass  the row again.

Thank you so much, I can now sleep.

Bud

Can I ask another question ,

if I was to use sprites instead of static images.

I have an external sprite.lua which contains all my sprite data, I use

[/lua] local alien = display.newSprite(sheets.alien, sequences.alien)[/lua]

to display from the sprite.lua, is it a simple matter of:

[lua]self.image = display.newSprite(sheets.alien, sequences.alien…row…".png" )[/lua]

I guess it isnt, any suggestions? I cant test I am away from the PC at the moment.

thankyou

This should do it:

[lua]

 

–enemy.lua

 

function enemys:init(xloc, yloc, row )
   

    self.image = display.newImage(“images/enemy”…row…".png" )
    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, j )     
        allEnemys[#allEnemys]:start()
        end
     end

[/lua]

@nick,sherman,

ahhh, I nearly had it yesterday using concat of the images but couldnt quite get it. BTT suggested this method also in the linked post.

Damn, I see where I went wrong:

[lua]allEnemys[#allEnemys]:init(i * 60, j* 70 +70, j ) [/code]

didnt pass  the row again.

Thank you so much, I can now sleep.

Bud

Can I ask another question ,

if I was to use sprites instead of static images.

I have an external sprite.lua which contains all my sprite data, I use

[/lua] local alien = display.newSprite(sheets.alien, sequences.alien)[/lua]

to display from the sprite.lua, is it a simple matter of:

[lua]self.image = display.newSprite(sheets.alien, sequences.alien…row…".png" )[/lua]

I guess it isnt, any suggestions? I cant test I am away from the PC at the moment.

thankyou