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();
if (player.getWorld() == null) {
Actor player = spaceInvWorld.getThePlayer();
if (player.getWorld() == null) {
/** * 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(); } }
/** * 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()); } }
/** * 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); }
thePlayer = new playerTurret(); addObject(thePlayer,398,442);