Hello,
I’m working on a prototype of game similar to this one: http://www.fletcherarmstrongblog.com/wp-content/uploads/2012/04/space-invaders.jpg
- First of all I created a display group and added the enemies into it:
local enemyGroup = display.newGroup()
function createEnemies()
enemyGroup:toFront()
local numOfRows = 4
local numOfColumns = 6
local enemyPlacement = {x = (display.contentWidth/2)-(enemyWidth\*numOfColumns)/2+20, y = 50}
for row = 0, numOfRows-1 do
for column=0, numOfColumns-1 do
-- Create a enemy
local enemy = display.newImage("enemy.png")
enemy.name = "enemy"
enemy.x = enemyPlacement.x + (column \* enemyWidth)
enemy.y = enemyPlacement.y + (row \* enemyHeight)
physics.addBody ( enemy, "static", {density=1, friction=0, bounce=0})
enemyGroup:insert(enemy)
end
end
end
- Now I want to move the whole group left and right, so that when the left margin of the group hits the left margin of the screen the group to start moving in the right part, and when the right margin of the group hits the right margin of the screen to start moving in the left part, and so on in a loop.
This is how I did it:
local motionX = 0
local walkingSpeed =2
function moveEnemyOnX()
if(enemyGroup.x\<=0) then
motionX = walkingSpeed
elseif (enemyGroup.x+enemyGroup.width\>=display.contentWidth) then
motionX = -walkingSpeed
end
if(enemyGroup.numChildren~=0) then
enemyGroup.x = enemyGroup.x + motionX
end
end
Runtime:addEventListener("enterFrame", moveEnemyOnX)
The problem is that the display group never hits the left margin of the screen. So I wanted to debug this and saw that the enemyGroup.x reaches “0”, but this is somewhere in the middle of the screen not as I would expected - when it reaches the left margin of the screen.
Similar issue is when the display group is moved to the right, but this time it crosses the right border of the screen, its like: if(enemyGroup.x+enemyGroup.width>=display.contentWidth) is neglected.
Do you have any idea why this behavior might happen?
I appreciate any suggestions, thank you. [import]uid: 180614 topic_id: 33528 reply_id: 333528[/import]

