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

2017/4/13

Very basic car driving smoothly question

vermox vermox

2017/4/13

#
I am making a dodgem car game. The direction is working fine but the car just jumps to the other side of the screen. I'm sure this has been asked before but I can't find it. My code is
public void act() 
    {
        move(1);
        
        if (Greenfoot.isKeyDown("left") ) {
           setLocation (getX()-1, getY());
           turn(-1);
        }
        if (Greenfoot.isKeyDown("right") ) {
            setLocation (getX()+1, getY());
            turn(1);
        }
        if (Greenfoot.isKeyDown("up") ) {
            setLocation (getX(), getY()-1);
        }
        if (Greenfoot.isKeyDown("down") ) {
            setLocation (getX(), getY()+1);
        }
        
    }    
How do I get the car to move smoothly on the screen? I copied the code from the tutorials but it doesn't work the same. Thanks!
danpost danpost

2017/4/13

#
The code given should not cause your car to "jump" across the screen. To test it, reset the project, manually remove ALL cars from the world: * right-click on world and select 'Inspect' * right-click on world again and select the 'removeObjects(Class cls)' method and in the dialog box, key the following for the parameter: the name of your World subclass in lowercase letters followed by the string ".getObjects(Car.class)" (without the double quotes) then press 'enter'; * right-click the Car class that the code above is located and select 'new Car()'; * click in the world where you want the car to be placed; * run the scenario and test the movement of the car; * report what behavior, usual or unusual, that you get Also, indicate what class the Car class extends and if not the Actor class, show that class as well.
Super_Hippo Super_Hippo

2017/4/13

#
Did you change the execution speed of the scenario?
You need to login to post a reply.