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

2019/4/16

Making a win or lose state.

1
2
danpost danpost

2019/4/23

#
Notted wrote...
Still does not work. The game keeps stopping when the player is still alive. We want it to stop when the player is dead. Is it the ref:
Actor player = spaceInvWorld.getThePlayer();
or the (new) check?
if (player.getWorld() == null) {
Not enough code provided to help, then. Cannot say what all might be needed (MyWorld, Player, GameOverWin, maybe more).
Notted Notted

2019/4/23

#
worldGameWinner:
/**
     * This makes the game end when you kill the aliens
     * 
     */
    public void act() 
    {
        gameOverWin();
    }   
    private void gameOverWin() {
        MyWorld spaceInvWorld = (MyWorld) getWorld();
        Actor basicAlien = spaceInvWorld.getAliens();
        if (basicAlien.getWorld() == null) {
        
            Greenfoot.stop();
        
        }
    }
enemyAlien:
/**
     * This is an alien. Shoots at the player and dies.
     * Danspost from greenfoot fourms comes to save my ass again.
     */
    private int shotCountDownAlien = 60;
    private int rateOfFireAlien =  1;
    private int basicAlienHealth =  1;
    private int gotoX = 300;
    private int basicAlienSpeed = 4;
    public void act() 
    {
        aliensMovement();
        enemeyShoot();
        playersBulletHitsAlien();
        alienDie();
    }
    /**
     * By askgriff on the greenfoot fourms
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }
    private void enemeyShoot()
    {
       xenoBullet aliensBullet = new xenoBullet();
       int alienY = getY()+getImage().getHeight()/2;
       MyWorld spaceInvWorld = (MyWorld) getWorld();
      if (shotCountDownAlien > 0) 
        {
            shotCountDownAlien -= rateOfFireAlien; 
        }
        else if (shotCountDownAlien == 0)
        {
            shotCountDownAlien = 40;
            spaceInvWorld.addObject(aliensBullet, getX(), alienY);
        }
    }
    private void playersBulletHitsAlien()
    {
        MyWorld spaceInvWorld = (MyWorld) getWorld();
        boolean playersBulletHasHitAlien = isTouching(worldBullet.class);
        if (playersBulletHasHitAlien)
        { 
            basicAlienHealth = basicAlienHealth - 1;
            removeTouching(worldBullet.class);
        }
    }
    private void alienDie()
    {
        if (basicAlienHealth <= 0)
        {
            if (!getWorld().getObjects(enemyAlien.class).isEmpty())
            {
                
            getWorld().removeObject(this); 
            
            }
        }
    }
    private void aliensMovement ()
    {
        move(basicAlienSpeed);
        if (atWorldEdge())
        {
            basicAlienSpeed = -basicAlienSpeed;
            int alienY = getY();
            setLocation(getX(), alienY + 5);
            int alienWidth = getWorld().getWidth();
            gotoX = alienWidth/2+(50+Greenfoot.getRandomNumber(alienWidth/2-80))*basicAlienSpeed/4;
        }
    }
    private void gameOverWin() {
        if (this == null) {
        
            Greenfoot.setWorld(new MyWorld());
        
        }
    }
MyWorld:
/**
     * This is a world. Creates stuff.
     * 
     */
    private worldBullet playersBullet;
    public worldBullet getBulletForPlayer()
    {
        return playersBullet;
    }
    private xenoBullet aliensBullet;
    public xenoBullet getBulletForAliens()
    {
        return aliensBullet;
    }
    private enemyAlien anAlien;
    public enemyAlien getAliens()
    {
        return anAlien;
    }
    private playerTurret thePlayer;
    public playerTurret getThePlayer()
    {
        return thePlayer;
    }
    private spaceWall aWall;
    public spaceWall getAWall()
    {
        return aWall;
    }

    public MyWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 480, 1); 
        anAlien = new enemyAlien();
        thePlayer = new playerTurret();
        aWall = new spaceWall();
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        enemyAlien enemyAlien = new enemyAlien();
        addObject(enemyAlien,407,46);
        spaceWall spaceWall = new spaceWall();
        addObject(spaceWall,53,236);
        spaceWall spaceWall2 = new spaceWall();
        addObject(spaceWall2,407,228);
        spaceWall spaceWall3 = new spaceWall();
        addObject(spaceWall3,748,221);
        spaceWall2.setLocation(415,225);
        playerTurret playerTurret = new playerTurret();
        addObject(playerTurret,398,442);
        enemyAlien.setLocation(405,12);
        playerTurret.setLocation(399,384);
        enemyAlien.setLocation(397,66);
        bulletDeathArea bulletDeathArea = new bulletDeathArea();
        addObject(bulletDeathArea,396,469);
        bulletDeathArea bulletDeathArea2 = new bulletDeathArea();
        addObject(bulletDeathArea2,404,2);
        bulletDeathArea.setLocation(385,474);
        bulletDeathArea bulletDeathArea3 = new bulletDeathArea();
        addObject(bulletDeathArea3,385,474);
        bulletDeathArea2.setLocation(398,10);
        bulletDeathArea bulletDeathArea4 = new bulletDeathArea();
        addObject(bulletDeathArea4,398,10);
        enemyAlien.setLocation(403,102);
        bulletDeathArea2.setLocation(402,11);
        bulletDeathArea bulletDeathArea5 = new bulletDeathArea();
        addObject(bulletDeathArea5,402,11);
        bulletDeathArea bulletDeathArea6 = new bulletDeathArea();
        addObject(bulletDeathArea6,702,16);
        bulletDeathArea6.setLocation(786,9);
        bulletDeathArea bulletDeathArea7 = new bulletDeathArea();
        addObject(bulletDeathArea7,786,9);
        bulletDeathArea bulletDeathArea8 = new bulletDeathArea();
        addObject(bulletDeathArea8,736,478);
        bulletDeathArea bulletDeathArea9 = new bulletDeathArea();
        addObject(bulletDeathArea9,10,473);
        bulletDeathArea.setLocation(437,476);
        bulletDeathArea bulletDeathArea10 = new bulletDeathArea();
        addObject(bulletDeathArea10,437,476);
        bulletDeathArea bulletDeathArea11 = new bulletDeathArea();
        addObject(bulletDeathArea11,177,11);
        bulletDeathArea11.setLocation(182,11);
        bulletDeathArea bulletDeathArea12 = new bulletDeathArea();
        addObject(bulletDeathArea12,182,11);
        bulletDeathArea8.setLocation(734,459);
        worldGameWinner worldGameWinner = new worldGameWinner();
        addObject(worldGameWinner,734,459);
        bulletDeathArea6.setLocation(765,27);
        enemyAlien enemyAlien2 = new enemyAlien();
        addObject(enemyAlien2,710,87);
        enemyAlien enemyAlien3 = new enemyAlien();
        addObject(enemyAlien3,54,101);
        enemyAlien2.setLocation(710,89);
        removeObject(enemyAlien3);
        removeObject(enemyAlien2);
        enemyAlien enemyAlien4 = new enemyAlien();
        addObject(enemyAlien4,582,99);
        enemyAlien enemyAlien5 = new enemyAlien();
        addObject(enemyAlien5,207,100);
    }
danpost danpost

2019/4/23

#
I believe your problem is at MyWorld:56-57.
Notted Notted

2019/4/23

#
danpost wrote...
I believe your problem is at MyWorld:56-57.
Why? I think it would just be better to not use the prepare() method and just use the MyWorld constructor.
danpost danpost

2019/4/23

#
Notted wrote...
danpost wrote...
I believe your problem is at MyWorld:56-57.
Why? I think it would just be better to not use the prepare() method and just use the MyWorld constructor.
Just change those lines to:
thePlayer = new playerTurret();
addObject(thePlayer,398,442);
so that your field is assigned the player you add into the world.
Notted Notted

2019/4/24

#
It works! So the problem was with the way it was spawning in?
danpost danpost

2019/4/24

#
Notted wrote...
It works! So the problem was with the way it was spawning in?
Yes. You had the actor spawning in, but the field, thePlayer, was not being set to that actor. My first line applies the assignment and my second one adds that (same) actor into the world.
You need to login to post a reply.
1
2