This site requires JavaScript, please enable it in your browser!
Greenfoot back
kartikitrak
kartikitrak wrote ...

2012/5/17

Movement for Enemy

1
2
RM6442 RM6442

2012/5/18

#
Here's your problem, though: if you want all of the aliens to move at once this part will have to be moved:
        enemy[i].setLocation(6, enemy[i].getY()+20);                                       
  
RM6442 RM6442

2012/5/18

#
Something like:
    for (int i = 0; i < enemy.length; i++)    
            {    
                if (enemy[i].getWorld() != null )    
                {    
                    if (enemy[i].getX() < 605 && enemy[i].getX() > 595)    
                    {    
                        enemyLeft = true;    
                        turnInstance=true;    
                    }    
                    if (enemy[i].getX() > -5 && enemy[i].getX() < 5)    
                    {    
                        enemyLeft = false;    
                        turnInstance=true;                  
                    }    
                } 
            }
for (int i = 0; i < enemy.length; i++)    
            {    
                if(turnInstance)
                   {
                       enemy[i].setLocation(enemy[i].getX(), enemy[i].getY()+20); 
                   }
            }
turnInstance=false;
Or something similar.
RM6442 RM6442

2012/5/18

#
"turnInstance" being a new variable of course.
danpost danpost

2012/5/20

#
The main problem here is that you have instance variables for the Enemy in the world class (examples: enemyLeft and enemyShootDelay). These need to be moved to the Enemy class. I did not go into the enemyShootDelay thing, but I did resolve the movement thing. After moving the instance variable declaration of 'enemyLeft' to the 'Enemy' class, as:
private boolean enemyLeft = false;
add the following two methods:
// lets the world change the value of 'enemyLeft'
public void changeDirection()
{
    enemyLeft = !enemyLeft;
}

// lets the world have access to the value of 'enemyLeft'
public boolean getEnemyLeft()
{
    return enemyLeft;
}
Now, in the world class code you provided yesterday, replace lines 82 through 106 with the following:
private void EnemyMovement()
{
    for (int i = 0; i < enemy.length; i++)
    {
        if (enemy[i].getWorld() != null )
        {
            boolean eLeft = enemy[i].getEnemyLeft();
            if (enemy[i].getX() == 560 && !eLeft || enemy[i].getX() == 40 && eLeft)
            {
                enemy[i].changeDirection();
                eLeft = !eLeft;
                enemy[i].setLocation(enemy[i].getX(), enemy[i].getY() + 20);
            }
            // the code 'eLeft ? -1 : 1' lets the value of 'eLeft' determine the direction of movement
            // like saying 'if (eLeft) direction = -1; else direction = 1;'
            enemy[i].setLocation(enemy[i].getX() + (eLeft ? -1 : 1), enemy[i].getY());
// end of replacement, the following continues from line 108 of your code
            if (enemyShootDelay == 300)
            // etc.
That should complete that part.
kartikitrak kartikitrak

2012/5/20

#
Ok. I looked over this for a whole day, and I finally understood it and know what I did wrong. Thanks a bunch danpost. You have helped me yet again.
IsVarious IsVarious

2012/5/21

#
Another tip: part of programming convention, is to place code within the objects which need the code. So for example, if you had a world with 3 actors in it. Each actor should keep track of its own movement code, also any interactions with those actors, you would want to place inside those actors as well. The world code really should only keep track of things like score, objects being placed in the world, or other simular logic. Hope this helps, and when your project is small it doesn't seem to matter as much, but as your project expands in size, you'll want to have good habbits in place.
kartikitrak kartikitrak

2012/5/21

#
Thanks for the advice.
You need to login to post a reply.
1
2