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!
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; } } } }
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); } } }