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

2017/4/14

Steering a car

vermox vermox

2017/4/14

#
Yet another question, sorry guy! I want to drive a car around the screen but I can't get the car to only move in the direction that the front is facing. I really don't know how to set the direction that the car moves. I tried using some trigonometry to set the direction but setLocation would only take an int. It's been driving me crazy for hours! Here's what I have:
        move(1);
        if (Greenfoot.isKeyDown("left") ) {
            setLocation (getX()-3, getY());
            turn(-3);
            

        }
        if (Greenfoot.isKeyDown("right") ) {
            setLocation (getX()+3, getY());
            turn(3);
            
   
        }
        if (Greenfoot.isKeyDown("up") ) {
                setLocation (getX(), getY()-3);
                move(-3);
        }
        if (Greenfoot.isKeyDown("down") ) {
                setLocation (getX(), getY()+3);
                move(3);
        }
danpost danpost

2017/4/14

#
vermox wrote...
I want to drive a car around the screen but I can't get the car to only move in the direction that the front is facing.
Remove lines 3, 9, 15, and 19. Then switch '3' and '-3' on lines 16 and 20.
You need to login to post a reply.