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

2015/3/5

I Need Help With Some Random Spawning

Mikeburdge Mikeburdge

2015/3/5

#
I am trying to make a little game where the monster runs around trying to catch the food, but so far the food is already there and i want it to spawn in at the maximum number of 3 per time and then as the monster eats the food another one spawns i have tried looking thing up on other peoples discussions but nothing they have has wrote have worked yet. so far i have
    public void act() 
    {
        moveAround();
    }    
    
    public void moveAround()
    {
        move(4);
        if (Greenfoot.getRandomNumber(100) < 10)
        {
            turn(Greenfoot.getRandomNumber(90) - 45);
        }
        if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
        {
            turn(180);
        }
        if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
        {
            turn(180);
        }
    }
Im sorry its not much to go by Please help me and thank you for your time Michael
LordLuhar LordLuhar

2015/3/5

#
I did something similar to this a while back, Put this in your world class
public GameWorld
{
for (int i =0;i<=2;i++)
{
addObject( new Food(), //x//,//y//);
}
}
And this code in your food class
Actor coll = getOneIntersectingObject(Monster.class);
        if(coll != null)
        
        
         {
            
            
            World world=getWorld();
            world.removeObject(this);
            world.addObject( new Food(),//x//,//y//);
}
Mikeburdge Mikeburdge

2015/3/5

#
Thank you for your reply as soon as i get near a computer with green foot i will try it and i truly hope it works
Mikeburdge Mikeburdge

2015/3/5

#
i see whats going on but what do i put in the //x//,//y// part because i want it to spawn in a random place
LordLuhar LordLuhar

2015/3/5

#
just put in
world.addObject( new Food(),Greenfoot.getRandomNumber(//World width here//),Greenfoot.getRandomNumber(//World height here//));
Mikeburdge Mikeburdge

2015/3/5

#
ok thanks there is only one problem, you have given me 2 places to put the x and y. do i put them in both or only 1?
LordLuhar LordLuhar

2015/3/5

#
Yeah put them in both, once in the world code, and once in the food code
You need to login to post a reply.