hi! im coding a game where the player has to dodge falling asteroids. i changed my code to have a 1/100 chance to spawn a new asteroid, but now my program stops after the asteroid reaches the edge of the world.
asteroid code:
game world code:
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 | public void act() { move(); Player p = (Player)getWorld().getObjects(Player. class ).get( 0 ); p = (Player)getOneIntersectingObject(Player. class ); if (p != null && GameWorld.getImmunity() == false ) { int hp = p.getHP(); hp--; GameWorld.changePoints(- 50 ); GameWorld.hpCounter.setValue(hp); getWorld().removeObject( this ); if (hp == 0 ) { Greenfoot.setWorld( new LoseWorld()); } } } public void move() { setLocation(getX(), getY() + speed); if (isAtEdge()) { getWorld().removeObject( this ); GameWorld.changePoints( 5 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public void act() { int random = Greenfoot.getRandomNumber( 100 ); if (random == 0 ) { addAsteroid(); } timer.setValue(timeLeft - t.millisElapsed()/ 1000 ); if (timer.getValue() == 0 ) { Greenfoot.setWorld( new LoseWorld()); } } public void addAsteroidp() { int randX = Greenfoot.getRandomNumber(getBackground().getWidth()); addObject( new Asteroid(), randX, WIDTH/ 10 ); } |