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

2016/9/6

Project

Footnote1 Footnote1

2016/9/6

#
I am trying to make balloons turn in different directions at spawn, but all I get is constant spinning.
boolean isLeftTurn = rng.nextInt(2) > 1;
        if(isLeftTurn){
            turn(rng.nextInt(360));
        }
        else{
            turn(rng.nextInt(360) - 360);
        }
danpost danpost

2016/9/7

#
Footnote1 wrote...
I am trying to make balloons turn in different directions at spawn, but all I get is constant spinning. < Code Omitted >
Line 1 will never be true. The 'rng.nextInt(2)' will only return a zero or a one (the two possibles). Try changing '> 1' to '> 0'. Also, lines 3 and 6 should probably just be:
// line 3
turn(-1);

// line 6
turn(1);
A larger value other than '1' could be used in both lines.
Super_Hippo Super_Hippo

2016/9/7

#
I am not sure what the 'isLeftTurn' should do there. It looks like you want to set the rotation randomly between 0 and 359 (or -1 and -360). You can probably just use this line in the constructor if I understand you correctly:
public Ballon()
{
    setRotation(Greenfoot.getRandomNumber(360));
}
danpost danpost

2016/9/7

#
Nevermind my last post. I missed where you indicated 'at spawn'. However, the terminology threw me when you indicated 'turn', where more appropriate would have been 'set their rotations'.
You need to login to post a reply.