i want the Lobster recreating again in same position after the Bango class was died/lifes-1. how to do it?
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bango here. * * @author (your name) * @version (a version number or a date) */ public class Bango extends Actor { /** * Act - do whatever the Bango wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int lifes = 3 ; private int hp; private int timerhealthbar; private HealthBar healthbar = new HealthBar(); public void act() { getWorld().addObject(healthbar, getX(), getY()); healthbar.setLocation(getX(), getY()); checkLobster(); checkCrocodile(); movement(); getWorld().showText( "Life: " +lifes, 50 , 20 ); } public void movement(){ if (Greenfoot.isKeyDown( "up" )){ setRotation( 270 ); move( 2 ); } if (Greenfoot.isKeyDown( "Down" )){ setRotation( 90 ); move( 2 ); } if (Greenfoot.isKeyDown( "Left" )){ setRotation( 180 ); move( 2 ); } if (Greenfoot.isKeyDown( "Right" )){ setRotation( 0 ); move( 2 ); } } public void testEndGame(){ if (lifes <= 0 ){ Greenfoot.stop(); } } public void resetPosition(){ this .setLocation( 48 , 60 ); healthbar.setLocation(getX(), getY()); this .hp= 0 ; healthbar.drawBar(hp); } public void removeLife(){ lifes--; testEndGame(); resetPosition(); } public void checkLobster(){ if (isTouching(Lobster. class )){ removeTouching(Lobster. class ); } } public void checkCrocodile(){ if (isTouching(Crocodile. class )){ hp++; healthbar.drawBar(hp); if (hp == 100 ){ removeLife(); hp = 0 ; } } } } |
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 | 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 { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); 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() { Lobster lobster = new Lobster(); addObject(lobster, 294 , 244 ); Bango bango = new Bango(); addObject(bango, 145 , 156 ); Crocodile crocodile = new Crocodile(); addObject(crocodile, 399 , 123 ); } } |