danpost wrote...
It actually works, but I have 3 class of aliens and I want that when there is none of the aliens, then it ends completely, because it stops when I only kill one kind of class alien if you can explain me


1 | if (getObjects(Alien. class ).isEmpty() && getObjects(Alien2. class ).isEmpty() && ...) |
1 | Greenfoot.setWorld( new GameOver()); |
1 | Greenfoot.setWorld( new GameOver()); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Alien here. * * @author (your name) * @version (a version number or a date) */ public class Alien extends Actor { private int timer = 2 +Greenfoot.getRandomNumber( 500 ); public Alien() { setRotation( 0 ); } /** * Act - do whatever the Alien wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Actor bullet = getOneIntersectingObject(Bullet. class ); //variable qui retourne la classe bullet if (bullet!= null ) { World world = getWorld(); MyWorld myWorld = (MyWorld)world; Counter counter = myWorld.getCounter(); counter.addScore(); getWorld().removeObject( this ); } if (--timer== 0 ){ shoot(); timer = 2 +Greenfoot.getRandomNumber( 500 ); } setLocation(getX()+ 1 , getY()); } public void shoot() { getWorld().addObject( new AlienBullet(), getX(), getY()); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Player here. * * @author (your name) * @version (a version number or a date) */ public class Player extends Actor { private int IntervalBetweenShots = Integer.MAX_VALUE; //Donne la valeur maximale de l'intervalle de temps entre les tirs. public Player() { setRotation( 0 ); } /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { boolean isLeftKeyDown = Greenfoot.isKeyDown( "left" ); if (isLeftKeyDown) { int x = getX(); int y = getY(); x = x - 5 ; setLocation(x,y); } boolean isRightKeyDown = Greenfoot.isKeyDown( "right" ); if (isRightKeyDown) { int x = getX(); int y = getY(); x = x + 5 ; setLocation(x,y); } boolean isSpaceKeyDown = Greenfoot.isKeyDown( "space" ); if (isSpaceKeyDown){ if (IntervalBetweenShots > 30 ){ getWorld().addObject( new Bullet(), getX(), getY()); IntervalBetweenShots = 0 ; } IntervalBetweenShots++; } Actor bullet = getOneIntersectingObject(AlienBullet. class ); if (bullet!= null ) { World world = getWorld(); GameOver gameover = new GameOver(); //GameOver est un type de variable (l'objet game over) et new game over crée un acteur game over à placer sur le jeu getWorld().addObject(gameover, 450 , 300 ); getWorld().removeObject( this ); Greenfoot.stop(); } } } |
1 2 3 4 5 6 7 | private boolean alienHitsShooter() { if (getOneIntersectingObject(Alien. class ) != null ) return true ; if (getOneIntersectingObject(Alien2. class ) != null ) return true ; ... return false ; } |
1 | if (alienHitsShooter()) |