Developing space invaders... one issue. pls help!!

Im just trying to finish off this space invaders game, but i ran into a problem.

Basically Ive got the aliens to move left-right on the top of the screen, but when they hit the left/right boundary they dont rebound as a block. They rebound separately… and then while doing that, they overlap eachother.

How can I make them rebound as a group rather than separately. Eg: The whole group to move from left to right.

http://users.tpg.com.au/egruber/invaders.JPG

public void alienArray()
{
alienCount = 0;
aliens = new ArrayList();

    for (int i=0; i<4; i++)
    {
        for (int ii=0; ii<8; ii++)
        {
            Alien alienA = new Alien(10+ii*30,10+i*30);
            alienA.setMovingRight( true );                
            
            aliens.add( alienA );                
            alienCount++;
        }
    }


private void checkPosition()

{
    //Alien alienB = new Alien(10, 10);
   
    
    
    for ( int ee=0; ee<32; ee++)
    {
        alien = (Alien)aliens.get(ee);
        
        if ( alien.getXPos() == 600)
        {
            
            alien.setMovingRight(false);
            alien.setMovingLeft(true);
        }
        
        if ( alien.getXPos() == 10)
        {
            
            alien.setMovingLeft(false);
            alien.setMovingRight(true);
        }
        
        
    }

Any ideeas?

Thanks

Well u must calculate collision for each ship separately.
Take into consideration the gap between the aliens and multuply it with it index number in row. Then add that value to your collision code.

When one of the aliens collides, you will need to change the movement of all aliens in the group. In your current code you change the direction of each colliding alien individiually.


private void checkPosition() {
	int direction = 0;
	for (int i = 0; i < aliens.size(); i++) {
		alien = (Alien)aliens.get(i);
		if (alien.getXPos() >= RIGHT_LIMIT) {
			direction = -1;
			break;
		} else if (alien.getXPos() <= LEFT_LIMIT) {
			direction = 1;
			break;
		}
	}
	if (direction == 0) {
		return;
	}
	for (int i = 0; i < aliens.size(); i++) {
		alien = (Alien)aliens.get(i);
		alien.setMovingLeft(direction < 0);
		alien.setMovingRight(direction > 0);
	}
}

I would change the booleans that indicates the direction to static.

During checkPosition I would leave the if the directions has been changed. With this all aliens should change moving direction as soon as one of them collides.

Thanks, I followed some of your advice and figured it out, but now i stumbled into another issue.

Once my aliens have reached the edges I want them to move down once, then move to the left /right.

So what I came up with is:

/**
* Checks the position of the alien, then moves them to the right and left
* until they hit the boundaries. Then they rebound
*
*/
private void checkPosition()
{
int maxLeft = 300;
int maxRight = 300;
int currentY = 0;

    for ( int ee=0; ee<32; ee++)
    {
        alien = (Alien)aliens.get(ee);

        if( alien.getXPos() > maxRight)
        maxRight = alien.getXPos();
        else if( alien.getXPos() < maxLeft )
        maxLeft = alien.getXPos();
        
        currentY = alien.getYPos();
    }
    
    //If they hit the right edge, they will stop moving right and start moving left.
    if ( maxRight >= 600)
    {
        
        for ( int ee=0; ee<32; ee++)
        {
           
            alien = (Alien)aliens.get(ee);
            alien.setMovingRight(false);
            alien.setMovingDown(true);
                            
            if (currentY > 0)
            {
                alien.setMovingLeft(true);
                alien.setMovingDown(false);
            }
            
            
        }
        
    }
    
    //If they hit the left edge, they will stop moving left and start moving right
    if ( maxLeft <= 10)
    {
        for ( int ee=0; ee<32; ee++)
        {
            
            alien = (Alien)aliens.get(ee);
            alien.setMovingLeft(false);
            alien.setMovingRight(true);      
            
        }
    }
}

But I think my ‘currentY’ methodology isnt working. Anyone can bring some light into this?

Thanks

Is the currentY supposed to be the alien’s coordinate? In that case when the above code is reached, currentY == aliens.get(31).getYPos() (Y position of the last alien in the group), which is always greater than 0. So when the above code is executed, it will be the same as this:


            for ( int ee=0; ee<32; ee++)
            {
                alien = (Alien)aliens.get(ee);
                alien.setMovingRight(false);
                alien.setMovingLeft(true);
                alien.setMovingDown(false);
            }