This site requires JavaScript, please enable it in your browser!
Greenfoot back
Avenderl
Avenderl wrote ...

2022/1/21

How would I make more objects spawn randomly after my actor collides with another actor?

Avenderl Avenderl

2022/1/21

#
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
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;
            }
        }
    }
}
Here is the code for MyWorld
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);
        }
    }
}
I'm not great at explaining so I can help clarify things better if needed but any help is appreciated!
danpost danpost

2022/1/21

#
Looks like you are already decreasing the intervals to have them spawn more quickly. Just make sure you limit how much you can lower the intervals. After decreasing interval, check its value. For example:
world.interval -= 6;
if (world.interval < 50) world.interval = 50;
Also, I do not know if it is intentional or not, but only two intervals are decreasing with your code (interval3 is not decreasing anywhere; and, interval is decreasing in two places -- lines 33 and 54).
Avenderl Avenderl

2022/1/22

#
Sorry that must've been a mistake. I've changed line 54 to world.interval3 So I tried coming up with some code so whenever the catcher touches the powerup class, it would spawn more..?
if (isTouching(PowerUp.class))
        {
            MyWorld world = (MyWorld)getWorld();
            world.interval3-=0;
        }
I put zero after world.interval3 temporarily, but would this work? Could you maybe give some suggestions? I put this code in the catcher class. I also forgot to say this but when the catcher touches the powerup class, I want it to spawn objects from a different class, not from the powerup class.
danpost danpost

2022/1/23

#
Avenderl wrote...
I put zero after world.interval3 temporarily, but would this work?
Decreasing the world.interval3 value should make them spawn more quickly.
when the catcher touches the powerup class, I want it to spawn objects from a different class, not from the powerup class.
What have you tried?
Avenderl Avenderl

2022/1/23

#
I haven't tried much but I came up with the code I mentioned before to make them spawn more quickly. I'm just a little confused about how I should make the catcher spawn objects from another class when it touches the powerup class. I have 4 actor classes right now: Catcher, meteor (which loses a life if you touch it), star (you gain points), and the powerup class. I assigned different variables to meteor, star, and powerup. The variable I used for star is "interval", for meteor, it's "interval2" and for powerup, it's "interval3". I used those variables to change the amount of objects that spawn in the world. If I wanted to spawn more stars when the catcher touches the powerup, would I have to change "world.interval3" into "world.interval"?
danpost danpost

2022/1/24

#
Avenderl wrote...
If I wanted to spawn more stars when the catcher touches the powerup, would I have to change "world.interval3" into "world.interval"?
Decreasing "world.interval" would cause stars to spawn more quickly.
You need to login to post a reply.