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

2015/3/4

need help figuring out how to move my actor at a get random speed

kyles247 kyles247

2015/3/4

#
i have 7 actors and i only have this code plugged in for each actor they all move to the right at a certain speed that i have plugged in i need to figure out how to make them move at all different speeds to the right without a keypad just to click run and the take off across page at different speeds ... i know its the code getRandomNumber but how do i have to plug it in without an error? public void act() { { move(5); } }
kyles247 kyles247

2015/3/4

#
Basically its suppose to be a race game to guess who will win
LordLuhar LordLuhar

2015/3/4

#
Are you trying to have a random speed set after it starts, or a random speed each act sequence?
LordLuhar LordLuhar

2015/3/4

#
This code will get a random speed and set it for the rest of the time
int x = Greenfoot.getRandomNumber(10);
public void act()
{
{
            move(x);
 }
}
This will set it random each act sequences
public void act
{
{
            move(Greenfoot.getRandomNumber(10));// You can insert any number there, i just chose ten
 }
}
kyles247 kyles247

2015/3/4

#
sorry im new to this so basically what i have is 7 race cars in the actor class. Racecar1 Racecar2 ext.. For each car i have this code put in so they race across the page to the right. public void act() { { move(5); } } each one has a different speed 2, 8 ext like this one is speed 5. When i reset my game it has all the cars already added to the world in different spots lined up ready to race. Im trying to figure out what code i can use to add to the Racecars so a different car wins everytime you reset the game. I know i can just use one code for every car so it picks a random speed each time that they will go but not sure
kyles247 kyles247

2015/3/4

#
Thanks alot that exactly what i needed
LordLuhar LordLuhar

2015/3/4

#
Glad to have helped
kyles247 kyles247

2015/3/4

#
Do you also know how i could possible set a trap in the world randomly so if the car hits the spot it will stop?
kyles247 kyles247

2015/3/4

#
or if they hit the spot it will shoot them in a different direction?
LordLuhar LordLuhar

2015/3/4

#
What you'd have to do is create an object that would spawn in the world and have green foot check fora a collision, and based off of that, alter the path Put this code in your mover, and randomly spawn in objects, i think this should work
Actor coll = getOneIntersectingObject(//put other class here//.class);
if(coll != null)
        
        {
           turn(Greenfoot.getRandomNumber(360));
        }
So if your trap class was named trap, the first line should read Actor coll = getOneIntersectingObject(Trap.class);
LordLuhar LordLuhar

2015/3/4

#
If you want to randomly spawn the traps, put this in your world class
addObject( new Trap(), Greenfoot.getRandomNumber(//put width here//),Greenfoot.getRandomNumber(//put height here//));
kyles247 kyles247

2015/3/4

#
Ok thanks i will try to also add to this code. Thanks for all the help
You need to login to post a reply.