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

2016/2/15

Direction of Ball

GreenMan15 GreenMan15

2016/2/15

#
Hey on my scenario Ball, it always starts off moving the right, I want to make it where it would go either left or right, not just right the whole time. Would I need to use something like move(Greenfoot.getRandomNumber(10));
Super_Hippo Super_Hippo

2016/2/15

#
Use this in the class' constructor to make it move either right or left at the start.
public Ball() //if that is the name of the class
{
    setRotation(180*Greenfoot.getRandomNumber(2));
}
The code you suggested lets the ball move in the direction it is facing (default is right) by 0 to 9 cells. Making it moving randomly right or left like 'move(2-4*Greenfoot.getRandomNumber(2));' (=move 2 cells either left or right) does not look good. Better is to move to one side until it reaches a certain x-value und then move back. I am not sure what exactly you wanted (starting direction or changing direction over time), so I wrote about both.
GreenMan15 GreenMan15

2016/2/15

#
Hmm Didn't work for me. The ball just stood there spinning
GreenMan15 GreenMan15

2016/2/15

#
Oh never mind it is working. Appreciate your help
GreenMan15 GreenMan15

2016/2/15

#
Would it work if I put in the the act method as well?
Super_Hippo Super_Hippo

2016/2/15

#
That would only let the ball set its rotation to right or left randomly in every act cycle... What do you want to achieve?
You need to login to post a reply.