For Loop to Create Two Rows of Enemy Fighters

I’m creating a space shooter game where the enemy ships will appear in two rows of 8 at the top of the screen in Level 1.

I created a For Loop to handle the multiple enemy ships which is handled in a table. I don’t have a problem with the first row and everything displays fine. When creating the second row I’m having a problem coming up with the code to display it correctly. Here’s the code:
local function loadEnemies()
for i = 1, 10 do
enemyTable[numOfEnemies] = display.newImage(“enemy1.png”)
enemyTable[numOfEnemies]:scale(.4,.4)
enemyTable[numOfEnemies]:setReferencePoint(display.CenterLeftReferencePoint)
enemyTable[numOfEnemies].x = (numOfEnemies * (display.viewableContentWidth * .025))
enemyTable[numOfEnemies].myName=“enemy”
numOfEnemies= numOfEnemies + 5
end
end

The top row displays fine. I have 8 enemy ships appearing at the top based on the code above. I increment the numOfEnemies by 5 each pass to be able to create space between the ships. My question is,“How would I create another row of enemy fighters so that I have a total of 16 on the screen to start the game?” So in essence, “How could I change the y position and possibly refer back to the previous 8 ship’s x position so that they are perfectly aligned?” Also, I created the enemyTable and numOfEnemies variables as local variables outside of the function.

Thanks in advance. Corona is an amazing tool!
[import]uid: 115755 topic_id: 22151 reply_id: 322151[/import]

>>The top row displays fine. I have 8 enemy ships appearing at the top based on the code above. <<

Do you?
If so, I think you’ve been lucky.
You dont seem to refer to the i variable in that loop at all.

try this:

[code]
local function loadEnemies()
local eighthWidth = (display.viewableContentWidth * .025)
for wave = 0,1, do
for i = 1, 10 do
enemyTable[wave * 8 + i] = display.newImage(“enemy1.png”)
enemyTable[wave * 8 + i]:scale(.4,.4)

enemyTable[wave * 8 + i].x = i * eighthWidth
enemyTable[wave * 8 + i].y = wave * eighthWidth
enemyTable[wave * 8 + i].myName=“enemy”

end
end
end
[/code] [import]uid: 108660 topic_id: 22151 reply_id: 88093[/import]

btw why are you incrementing number of enemies by 5 each time? Aren’t you adding 1 enemy at a time? [import]uid: 8872 topic_id: 22151 reply_id: 88095[/import]

this is probably a better way to do it:

local function loadEnemies() for y = 0, 1 do for x = 1, 10 do enemyTable[numOfEnemies] = display.newImage("enemy1.png") enemyTable[numOfEnemies]:scale(.4,.4) enemyTable[numOfEnemies]:setReferencePoint(display.CenterLeftReferencePoint) enemyTable[numOfEnemies].x = x \* spaceBetweenEnemies + FirstEnemyOffsetFromLeftOfScreen enemyTable[numOfEnemies].y = y \* spaceBetweenRowsOfEnemies + PixelsFromEdgeOfScreen enemyTable[numOfEnemies].myName="enemy" numOfEnemies= numOfEnemies + 5 end end end [import]uid: 8872 topic_id: 22151 reply_id: 88094[/import]

Thanks a lot. This really helps!! [import]uid: 115755 topic_id: 22151 reply_id: 88098[/import]

You’re right. That’s was one of the issues. I was never incrementing the i variable. Here’s the revised function as it works!!! Amazing JOB!!! Thanks.

local function loadEnemies()
local ninthWidth = (display.viewableContentWidth * .09)
local eightHeight = (display.viewableContentHeight * .08)
local wave
for wave = 1, 2 do
for i = 1, 10 do
enemyTable[wave * 8 + i] = display.newImage(“enemy1.png”)
enemyTable[wave * 8 + i]:scale(.3,.3)
enemyTable[wave * 8 + i].x = i * eighthWidth
enemyTable[wave * 8 + i].y = wave * ninthHeight
enemyTable[wave * 8 + i].myName=“enemy”
end
end
end [import]uid: 115755 topic_id: 22151 reply_id: 88100[/import]

Thanks!!! [import]uid: 115755 topic_id: 22151 reply_id: 88118[/import]

I would have fixed my code but coudlnt find an edit button.

The code creates 10 across the page, but needs to do only 8.

So:

local function loadEnemies()
local ninthWidth = (display.viewableContentWidth * .09)
local eightHeight = (display.viewableContentHeight * .08)
local wave
for wave = 1, 2 do
for i = 1, 8 do – enemyTable[wave * 8 + i] = display.newImage(“enemy1.png”)
enemyTable[wave * 8 + i]:scale(.3,.3)
enemyTable[wave * 8 + i].x = i * eighthWidth
enemyTable[wave * 8 + i].y = wave * ninthHeight
enemyTable[wave * 8 + i].myName=“enemy”
end
end
end
[import]uid: 108660 topic_id: 22151 reply_id: 88120[/import]