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

2013/11/17

Need help with random spawning for object.

1
2
qpommer0 qpommer0

2013/11/17

#
Hello, I am trying to make it so in my game when you kill the object that it spawns another one. I have looked up some stuff on how to do it, but I can't seem to get it to work. So, I was hoping some one could help. I do have some code done, but it isn't right. All of this is under my World class by the way.
public PizzaWorld()
    {
        super(SIZE, SIZE, 1);
        setup2();
        addFly();
        addBee();
    }
    public void addBee()
    {
        int spawnX = 0;
        int spawnY = 0;
        if (Greenfoot.getRandomNumber(2) == 0)
        {
            spawnY = Greenfoot.getRandomNumber(2) * (getHeight() - 1);
            spawnX = Greenfoot.getRandomNumber(getWidth());
        }
        else
        {
            spawnY = Greenfoot.getRandomNumber(2) * (getWidth() - 1);
            spawnX = Greenfoot.getRandomNumber(getHeight());
        }
        addObject(new bee(), spawnX, spawnY);
    }
Thank you!
When you use Greenfoot.getRandomNumber(n) you will get a number from the range of 0 to n-1. So, what you want to do instead is this:
Greenfoot.getRandomNumber(2)+1
That will give you a range between 1 and 2. Hope that helps!
qpommer0 qpommer0

2013/11/17

#
Hm, that still didn't work.
Don't put it on the first if statement. It should look like this:
if (Greenfoot.getRandomNumber(2) == 0)  
    {  
        spawnY = (Greenfoot.getRandomNumber(2)+1) * (getHeight() - 1);  
        spawnX = Greenfoot.getRandomNumber(getWidth());  
    }  
    else  
    {  
        spawnY = (Greenfoot.getRandomNumber(2)+1) * (getWidth() - 1);  
        spawnX = Greenfoot.getRandomNumber(getHeight());  
    }  
By the way, do you want the objects to spawn out of the world?
qpommer0 qpommer0

2013/11/18

#
That is what I changed it to, and no. I want it to spawn in the world. Sorry, if I am being a pain.
you don't even need your crazy if statement then.
spawnY = Greenfoot.getRandomNumber(getHeight());
spawnX = Greenfoot.getRandomNumber(getWidth());
RUMMAKER RUMMAKER

2013/11/18

#
so if your bee dies, then you spawn a new bee wouldn't it be the same thing as just changing the location of the bee?
danpost danpost

2013/11/18

#
The code originally given should spawn a bee randomly along one of the edges of the world. But, this is not the code where a bee dies and needs to be re-spawned. Show the code in the class where the bee dies.
danpost danpost

2013/11/18

#
FlyingRabidUnicornPig wrote...
you don't even need your crazy if statement then.
spawnY = Greenfoot.getRandomNumber(getHeight());
spawnX = Greenfoot.getRandomNumber(getWidth());
Actually, that is not a crazy 'if' statement. It really makes good sense if the bee is to be spawned randomly along any edge of the world. @qpommer0, if that is not how you want the bees to spawn, please explain exactly how the bee is to be spawned.
Yah, but they said they wanted it in the world, I was wondering why they had that kind of if statement if they wanted it in the world.
danpost danpost

2013/11/18

#
FlyingRabidUnicornPig wrote...
Yah, but they said they wanted it in the world, I was wondering why they had that kind of if statement if they wanted it in the world.
I think qpommer0 needs to be more clear on what is wanted. Along the edge of the world can still be considered 'in the world'.
qpommer0 qpommer0

2013/11/18

#
Sorry, about not being clear. I am just trying to get an object to spawn in the world after being "killed." I don't want it to spawn along the world edge (if that is possible to do). I am still really new to programming. Here is the code for how the bee dies.
    public void removeFromWorld()
    {
        if (Greenfoot.mouseClicked(this))
        {
            ((PizzaWorld)getWorld()).getScoreCounter().add(20);
            getWorld().removeObject(this);
        }
    }
Thank you guys for all your help.
danpost danpost

2013/11/18

#
I thought that maybe the code you originally gave looked too neat -- that you acquired the code from somewhere else and did not write it yourself without exactly knowing what it does. This is why you need to learn how to create the code, yourself, to do what you want it to do. You need to be able to look at code and determine what it does also. Replacing lines 3 through 14 in your 'addBee' method with the two-liner that FRUP suggested will do what you want as far as the initial random placement of the bee. In your 'removeFromWorld' method in the Bee class, you can insert at line 6 '((PizzaWorld)getWorld()).addBee();' to spawn a new Bee object before removing the current one.
qpommer0 qpommer0

2013/11/18

#
It works fine now, thank you so much for the help guys. I also do really appreciate the tip. Once again, thank you!
hasta hasta

2016/1/6

#
Can you help me guys? I can't random my text for quiz gaming
There are more replies on the next page.
1
2