In my game, I have an actor that is controllable and is called "catcher". It catches objects falling from the sky. Whenever a certain actor randomly spawns and gets caught, I want it to start spawning more objects, like a power up. Here is the code I have for the catcher
Here is the code for MyWorld
I'm not great at explaining so I can help clarify things better if needed but any help is appreciated!
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 | import greenfoot.*; public class Catcher extends Actor { int score = 0 ; int lives = 3 ; public void act() { getWorld().showText( "Score: " + score, 100 , 30 ); getWorld().showText( "Lives: " + lives, 300 , 30 ); if (Greenfoot.isKeyDown( "right" )){ this .setLocation( this .getX() + 5 , this .getY()); } if (Greenfoot.isKeyDown( "left" )){ this .setLocation( this .getX() - 5 , this .getY()); } if (Greenfoot.isKeyDown( "right" ) && Greenfoot.isKeyDown( "shift" )){ move ( 7 ); } if (Greenfoot.isKeyDown( "left" ) && Greenfoot.isKeyDown( "shift" )){ move (- 7 ); } Actor star = this .getOneIntersectingObject(Star. class ); if (star != null ){ getWorld().removeObject(star); score++; if (score % 10 == 0 ){ MyWorld world = (MyWorld)getWorld(); world.interval-= 6 ; } } Actor meteor = this .getOneIntersectingObject(Meteor. class ); if (meteor != null ){ getWorld().removeObject(meteor); lives = lives - 1 ; if (score % 10 == 0 ){ MyWorld world = (MyWorld)getWorld(); world.interval2-= 6 ; } } Actor powerup = this .getOneIntersectingObject(PowerUp. class ); if (powerup != null ){ getWorld().removeObject(powerup); score++; if (score % 10 == 0 ){ MyWorld world = (MyWorld)getWorld(); world.interval-= 6 ; } } } } |
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 | import greenfoot.*; public class MyWorld extends World { int interval = 100 ; int interval2 = 300 ; int interval3 = 4000 ; public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 400 , 350 , 1 ); prepare(); } private void prepare() { Catcher catcher = new Catcher(); addObject(catcher, 189 , 296 ); } public void act(){ if (Greenfoot.getRandomNumber(interval) == 3 ){ Star star = new Star(); addObject( new Star(),Greenfoot.getRandomNumber(getWidth()), 0 ); } if (Greenfoot.getRandomNumber(interval2) == 3 ){ Meteor meteor = new Meteor(); addObject( new Meteor(),Greenfoot.getRandomNumber(getWidth()), 0 ); } if (Greenfoot.getRandomNumber(interval3) == 2 ){ PowerUp powerup = new PowerUp(); addObject( new PowerUp (),Greenfoot.getRandomNumber(getWidth()), 0 ); } } } |