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

2012/5/17

Movement for Enemy

1
2
kartikitrak kartikitrak

2012/5/17

#
Hey again. If you have seen my previous discussion post, I was talking about making a space invaders like game. I still have one last problem that comes with it and it is basically the movement of the enemy ships. In Space Invaders the enemies are in a block and they move together as if they're one character however in my game I want each enemy to be its on and not follow that "chunk" movement. It's really hard to explain but basically I am trying to make snake like pattern. When the first enemy in the "chain" reaches the right of the screen it goes down and then turns left and the enemies behind it follow the pattern when they reach the sides of the screen. Currently, I have my attempt which for some reason only lowers the side enemies and moves in the chunk formation.
private void EnemyMovement()
    {
        for (int i = 0; i < enemy.length; i++)
        {
            if (enemy[i].getWorld() != null)
            {
                if (enemy[i].getX() < 605 && enemy[i].getX() > 595)
                {
                    enemyLeft = true;
                    enemy[i].setLocation(594, enemy[i].getY()+20);
                }
                else if (enemy[i].getX() > -5 && enemy[i].getX() < 5)
                {
                    enemyLeft = false;
                    enemy[i].setLocation(6, enemy[i].getY()+20);                                   
                }

                if (!enemyLeft)
                {
                    enemy[i].setLocation(enemy[i].getX() +1, enemy[i].getY());
                }
                if (enemyLeft)
                {
                    enemy[i].setLocation(enemy[i].getX() -1, enemy[i].getY());
                }               
            }
        }
    }
This is the code that is giving me all the trouble, the rest seem to work perfectly and shouldn't be causing any problems.
kartikitrak kartikitrak

2012/5/17

#
The reason why the enemies on the sides lower is because of this part:
if (enemy[i].getX() < 605 && enemy[i].getX() > 595)  
                {  
                    enemyLeft = true;  
                    enemy[i].setLocation(594, enemy[i].getY()+20);  
                }  
                else if (enemy[i].getX() > -5 && enemy[i].getX() < 5)  
                {  
                    enemyLeft = false;  
                    enemy[i].setLocation(6, enemy[i].getY()+20);                                     
                }  
kartikitrak kartikitrak

2012/5/18

#
Still looking for help. Still haven't found the solution to this problem.
danpost danpost

2012/5/18

#
You probably forgot to change every other row's 'enemyLeft' value to the opposite of what it was initialized to. The code actually should work (from what I could tell).
Builderboy2005 Builderboy2005

2012/5/18

#
You are currently controlling *every* enemy from a single method. Might it be easier to have each enemy control themselves? That way, if you can get 1 enemy working, you can get any amount of enemies working.
kartikitrak kartikitrak

2012/5/18

#
How would I go about doing this? The movement pattern, I'm trying to achieve is: ->->->->->->->->-> | <-<-<-<-<-<-<-<-<- | ->->->->->->-> the | = movement down.
Builderboy2005 Builderboy2005

2012/5/18

#
Well I am not going to spell it out in code, but let's think about this for a second. We can describe in words exactly what we want each enemy to do right? Move to the right. If we are at the very right of the screen, start moving down If we have moved down X units, start moving to the left If we are at the very right of the screen, start moving down If we have moved down X units, start moving to the right again Correct? These are 3 different behaviors (moving right, left and down) and we switch between behaviors based on certain conditions (like reaching the side of the screen or moving a certain amount). Keeping this in mind, how would you go about programming different behaviors into an Actor?
danpost danpost

2012/5/18

#
When you add the enemies into the world, are you setting the 'enemyLeft' values to alternate by rows?
kartikitrak kartikitrak

2012/5/18

#
This maybe a bit messy, but here is the code for the MyWorld class that does basically all the movements and most of the program.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class MyWorld extends World
{

    private int counter = 20;
    private int level = 1;
    private int lives = 3;
    private int score = 0;
    private boolean beatenLevel = false;
    private boolean enemyLeft = false;
    private boolean isDead = false;
    private int randomNumber = Greenfoot.getRandomNumber(level*8);   
    private int enemyShootDelay = 0;
    Enemy[] enemy = new Enemy[level*8];
    Ship SpaceShip= new Ship(); 
    ScoreBoard scoreBoard = new ScoreBoard();

    private GreenfootSound explosion = new GreenfootSound ("explosion.wav");
    private GreenfootSound shoot = new GreenfootSound ("shoot.wav");

    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1, false);
        scoreBoard.setScore(score,level,lives);
        addObject(SpaceShip, 300, 376);      
        addObject (scoreBoard, 450, 20);
        createLevel();
    }

    public void act()
    {
        if (!isDead)
        {
            endLevel();
            checkKeys();
            checkShipDead();
            EnemyMovement();
        }

    }

    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("left") && SpaceShip.getX() > 0)
        {
            SpaceShip.setLocation (SpaceShip.getX() - 5, SpaceShip.getY());
        }
        if(Greenfoot.isKeyDown("right") && SpaceShip.getX() < 600)
        {
            SpaceShip.setLocation (SpaceShip.getX() + 5, SpaceShip.getY());
        }
        if(Greenfoot.isKeyDown("space"))
        {
            if (counter == 20)
            {
                if (shoot.isPlaying())
                {
                    shoot.stop();
                }
                else
                {
                    shoot.play();
                }
                sShoot();
                counter = 0;
            }
            else 
            {
                counter++;
            }
        }
    }

    private void EnemyMovement()
    {
        for (int i = 0; i < enemy.length; i++)
        {
            if (enemy[i].getWorld() != null )
            {
                if (enemy[i].getX() < 605 && enemy[i].getX() > 595)
                {
                    enemyLeft = true;
                    enemy[i].setLocation(594, enemy[i].getY()+20);
                }
                if (enemy[i].getX() > -5 && enemy[i].getX() < 5)
                {
                    enemyLeft = false;
                    enemy[i].setLocation(6, enemy[i].getY()+20);                                   
                }

                if (!enemyLeft)
                {
                    enemy[i].setLocation(enemy[i].getX() +1, enemy[i].getY());
                }
                if (enemyLeft)
                {
                    enemy[i].setLocation(enemy[i].getX() -1, enemy[i].getY());
                }

                if (enemyShootDelay == 300)
                {
                    enemy[i].eShoot();
                    enemyShootDelay =0;
                }

                if (enemy[i].death())
                {
                    score +=10;
                    scoreBoard.setScore(score,level, lives);
                }
                enemyShootDelay++;
            }
        }
    }

    public void createLevel()
    {
        enemy = new Enemy[level *8];

        for (int i = 0; i < enemy.length; i++)
        {
            enemy[i] = new Enemy(); 
            addObject(enemy[i],-(i*25), 45);
        }

        SpaceShip.setLocation(300,376);
    }

    public void endLevel()
    {       
        if (enemiesGone())
        {
            addLevel();
            createLevel();
        }
    }

    public void sShoot()
    {
        SpaceShipBullet sBullet= new SpaceShipBullet();
        addObject( sBullet, SpaceShip.getX(), SpaceShip.getY() );
        sBullet.shoot();

    }

    public boolean enemiesGone()
    {  
        return getObjects(Enemy.class).isEmpty();  
    }  

    public void checkShipDead()
    {
        if (SpaceShip.death())
        {
            explosion.play();
            lives--;
            scoreBoard.setScore(score,level,lives);
            if (lives <= 0)
            {
                isDead=true;
                SpaceShip.setImage("SpaceShipDead.png");
                Greenfoot.stop();
            }
            if (!isDead)
            {
                SpaceShip.setLocation(300,376);
            }
        }

    }

    public void addLevel()
    {
        level++;
        scoreBoard.setScore(score,level, lives);
    }
}
@Builderboy2005 I tried doing that in my previous code, but I think the problem is that when one enemy object reaches the side it affects all the enemy objects regardless of their position.
kartikitrak kartikitrak

2012/5/18

#
This is what I think danpost wanted to know...I'm not sure what you meant by the rows though. This is how I import the objects.
public void createLevel()  
    {  
        enemy = new Enemy[level *8];  
  
        for (int i = 0; i < enemy.length; i++)  
        {  
            enemy[i] = new Enemy();   
            addObject(enemy[i],-(i*25), 45);  
        }  
  
        SpaceShip.setLocation(300,376);  
    }  
danpost danpost

2012/5/18

#
I noticed that in you full code post (an unbounded world, where the enemies come in from the top left). Cannot thoroughly investigate right now. Really tired and need rest. Hope there is no rush.
kartikitrak kartikitrak

2012/5/18

#
Ok. I thank you for your help. Going to need to investigate this little bugger.
kartikitrak kartikitrak

2012/5/18

#
Hey Builderboy2005. Can you take a wiz at this? All I need is that pesky enemy movement that keeps messing with me.
Builderboy2005 Builderboy2005

2012/5/18

#
I am actually going to head off to bed as well, but I urge you to try to make each enemy control themselves, as I think it will make everything a bit easier to work with, especially if you ever want to add different types of enemies or other changes like that
RM6442 RM6442

2012/5/18

#
May I take a crack at it? I feel like this is the only part that needs to be a method of the world:
for (int i = 0; i < enemy.length; i++)  
        {  
            if (enemy[i].getWorld() != null )  
            {  
                if (enemy[i].getX() < 605 && enemy[i].getX() > 595)  
                {  
                    enemyLeft = true;  
                    enemy[i].setLocation(594, enemy[i].getY()+20);  
                }  
                if (enemy[i].getX() > -5 && enemy[i].getX() < 5)  
                {  
                    enemyLeft = false;  
                    enemy[i].setLocation(6, enemy[i].getY()+20);                                     
                }  
Everything else should probably a part of each individual enemy.
There are more replies on the next page.
1
2