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

2016/4/15

random to controlled movement

tRapkIng tRapkIng

2016/4/15

#
I'm new to programming and I'm trying to figure find some code to stop a randomly moving object. Then for me to be able to control that object using the arrow keys. I already know how to make a object move with the arrow keys but I can't figure out how to gain control after that. This is what I have already: { randomMove(); if(isTouching(person.class)) { removeTouching(person.class); if (Greenfoot.isKeyDown("up")) { move (2); } if (Greenfoot.isKeyDown("down")) { move (2); } if (Greenfoot.isKeyDown("left")) { setLocation(getX()-2,getY()); } if (Greenfoot.isKeyDown("right")) { setLocation(getX()+2,getY()); } if (isAtEdge()) { Greenfoot.stop(); } } } private void randomMove() { move (Greenfoot.getRandomNumber(4)); turn (Greenfoot.getRandomNumber(10)-5); if(isAtEdge()) { turn(180); } }
Super_Hippo Super_Hippo

2016/4/15

#
You could use a boolean field to decide which method to execute.
private boolean userControlled = false;

//...
if (!userControlled)
{
    randomMove();
}
else
{
    //...
}
You need to login to post a reply.