Display group object does not move as expected

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

  1. 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  
  1. 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]

Hello,
Before you go too much further down this path, I should alert you to the fact that moving a display group containing physics objects will break the collision sensory relationship between that group and another group which hasn’t been moved (say, the group where your player ship might reside). So, you might want to find a better way to move the enemies… you’ll save yourself a headache later when the bullets aren’t hitting the space invaders, and you’re going crazy trying to figure out why. :wink:

Brent Sorrentino [import]uid: 200026 topic_id: 33528 reply_id: 133328[/import]

Hello,
Before you go too much further down this path, I should alert you to the fact that moving a display group containing physics objects will break the collision sensory relationship between that group and another group which hasn’t been moved (say, the group where your player ship might reside). So, you might want to find a better way to move the enemies… you’ll save yourself a headache later when the bullets aren’t hitting the space invaders, and you’re going crazy trying to figure out why. :wink:

Brent Sorrentino [import]uid: 200026 topic_id: 33528 reply_id: 133328[/import]

Brent thank you for your replay.
I run in some sort of issues with that implementation, and I changed it that instead of moving the group, rather to move the individual enemies from the group, like this:

function moveEnemyOnX()  
if(enemyGroup.numChildren~=0) then  
 for i=1, enemyGroup.numChildren do  
 if(enemyGroup[i]~=nil) then  
 enemyGroup[i].x = enemyGroup[i].x + walkingSpeed  
 end  
 end  
end  
end  

and then I call this function every 15 ms with a timer, like this:

myTimer = timer.performWithDelay(15, moveEnemyOnX, 0)  

This implementation works, but I have some doubts in regards to calling the timer every 15ms to do that job. I don’t have much experience with Corona, what do you think about this? Is this OK, or too bad? [import]uid: 180614 topic_id: 33528 reply_id: 134310[/import]

Brent thank you for your replay.
I run in some sort of issues with that implementation, and I changed it that instead of moving the group, rather to move the individual enemies from the group, like this:

function moveEnemyOnX()  
if(enemyGroup.numChildren~=0) then  
 for i=1, enemyGroup.numChildren do  
 if(enemyGroup[i]~=nil) then  
 enemyGroup[i].x = enemyGroup[i].x + walkingSpeed  
 end  
 end  
end  
end  

and then I call this function every 15 ms with a timer, like this:

myTimer = timer.performWithDelay(15, moveEnemyOnX, 0)  

This implementation works, but I have some doubts in regards to calling the timer every 15ms to do that job. I don’t have much experience with Corona, what do you think about this? Is this OK, or too bad? [import]uid: 180614 topic_id: 33528 reply_id: 134310[/import]

Hello,
Sorry for the delay, I just noticed that I had forgotten to respond! :open_mouth:

Your method here is generally fine, but if you have ALOT of enemies that you’re moving, it might affect performance.

If you’re using the physics engine, is there any way you can just move them using physical properties, for example, apply linear velocities to them? This is generally more efficient, but it might not work for your needs.

Brent [import]uid: 200026 topic_id: 33528 reply_id: 136008[/import]

Hello,
Sorry for the delay, I just noticed that I had forgotten to respond! :open_mouth:

Your method here is generally fine, but if you have ALOT of enemies that you’re moving, it might affect performance.

If you’re using the physics engine, is there any way you can just move them using physical properties, for example, apply linear velocities to them? This is generally more efficient, but it might not work for your needs.

Brent [import]uid: 200026 topic_id: 33528 reply_id: 136008[/import]