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

2021/3/8

Get Location

JuliusF03 JuliusF03

2021/3/8

#
So, i'm working on a school project and i want to implement a mechanic allowing a randomly moved actor to turn at a specific point, if he reaches it. if(actor at specific coordinates, for example x=500, y=400) //here lies my problem { if(Greenfoot.getRandomNumber(1) == 1) setRotation(270); else setRotation(90); }
Gbasire Gbasire

2021/3/8

#
you must put double == if you want to check a value. If you're just changing a variable, you just say for example money = 100 But if you want to check a variable in a "if", you must do if(money == 100) also, you can't just say "x" or "y", you must use getX() or getY() also, this can't work, because getRandomNumber chooses a number between 0 and the number you put -1 (for example : getRandomNumber(5) will choose a number between 0 and 4) so for example here, do this :
if(getX() == 500 && getY() == 400) //use "&&" to add another required condition, use "||" to add another possible condition
{
    if(Greenfoot.getRandomNumber(2) == 1)
    {
        setRotation(270);
    }
    else
    {
        setRotation(90);
    }
}
danpost danpost

2021/3/8

#
More simply might be:
if (getY() == 400 || Greenfoot.getRandomNumber(2) == 1) setRotation(90);
else setRotation(270);
or
setRotation(getY() == 400 || Greenfoot.getRandomNumber(2) == 1 ? 90 : 270);
Swap rotations ("90" and "270") if needed.
You need to login to post a reply.