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

2015/12/11

How do I make it so the rocket keeps turning in the direction of the last pressed side key?

ConG36C ConG36C

2015/12/11

#
So if I press the left key, when I let go, the rocket keeps turning left, and vice versa for the right. Thanks.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class Rocket extends SmoothMover
{
    private static final int gunReloadTime = 5;         // The minimum delay between firing the gun.
    private static final int protonReloadTime = 25;    // The minimum delay between proton wave bursts.

    private int reloadDelayCount;               // How long ago we fired the gun the last time.
    private int protonDelayCount;               // How long ago we fired the proton wave the last time.

    private GreenfootImage rocket = new GreenfootImage("newRocket.fw.png");    
    private GreenfootImage rocketWithThrust = new GreenfootImage("newRocketWithThrust.png");

    /**
     * Initilise this rocket.
     */
    public Rocket()
    {
        reloadDelayCount = 5;
        protonDelayCount = 3;
        addForce(new Vector(13, 0.3)); // initially slowly drifting
        rocket.scale(50,34);
        rocketWithThrust.scale(50,34);
    }

    /**
     * Do what a rocket's gotta do. (Which is: mostly flying about, and turning,
     * accelerating and shooting when the right keys are pressed.)
     */
    public void act()
    {
        move();
        checkKeys();
        if ( !(getWorld() == null ))
        {
            checkCollision();
        }
        reloadDelayCount++;
        protonDelayCount++;
    }

    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        slowdown(Greenfoot.isKeyDown("down"));

        ignite(Greenfoot.isKeyDown("up"));

        if (Greenfoot.isKeyDown("left")) 
        {
            setRotation(getRotation() - 5);
        }
        else {
            turn(-1);
        }
        if (Greenfoot.isKeyDown("right")) 
        {
            setRotation(getRotation() + 5);
        }
        else {
            turn(1);
        }
        if (Greenfoot.isKeyDown("space")) 
        {
            fire();
        }
        if (Greenfoot.isKeyDown("z")) 
        {
            startProtonWave();
        }
        if (Greenfoot.isKeyDown("d"))
        {
            Space space = (Space) getWorld();
            space.addObject(new Explosion(), getX(), getY());
            space.removeObject(this);
            space.gameOver();
        }
    }

    /**
     * Check whether we are colliding with an asteroid.
     */
    private void checkCollision() 
    {
        Actor a = getOneIntersectingObject(Asteroid.class);
        if (a != null) 
        {
            Space space = (Space) getWorld();
            space.addObject(new Explosion(), getX(), getY());
            space.removeObject(this);
            space.gameOver();
        }
    }

    /**
     * Should the rocket be ignited?
     */
    private void ignite(boolean boosterOn) 
    {
        if (boosterOn) 
        {
            setImage (rocketWithThrust);
            addForce (new Vector(getRotation(), 0.3));
        }
        else 
        {
            setImage(rocket);        
        }
    }

    private void slowdown (boolean reverseThrust)
    {
        if ( reverseThrust) {
            addForce (new Vector(getRotation(), -0.1));
        }
        else {
            setImage(rocket);
        }
    }

    /**
     * Fire a bullet if the gun is ready.
     */
    private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) 
        {
            Bullet bullet = new Bullet (getMovement().copy(), getRotation());
            getWorld().addObject (bullet, getX(), getY());
            bullet.move ();
            reloadDelayCount = 0;
        }
    }

    /**
     * Release a proton wave (if it is loaded).
     */
    private void startProtonWave() 
    {
        if (protonDelayCount >= protonReloadTime) 
        {
            ProtonWave wave = new ProtonWave();
            getWorld().addObject (wave, getX(), getY());
            protonDelayCount = 0;
        }
    }

}
That is my rocket class, this is my smooth mover in case you need it
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public abstract class SmoothMover extends Actor
{
    private Vector movement;
    private double exactX;
    private double exactY;

    public SmoothMover()
    {
        this(new Vector());
    }

    /**
     * Create new thing initialised with given speed.
     */
    public SmoothMover(Vector movement)
    {
        this.movement = movement;
    }

    /**
     * Move in the current movement direction. Wrap around to the opposite edge of the
     * screen if moving out of the world.
     */
    public void move() 
    {
        exactX = exactX + movement.getX();
        exactY = exactY + movement.getY();
        if(exactX >= getWorld().getWidth()) {
            exactX = 0;
        }
        if(exactX < 0) {
            exactX = getWorld().getWidth() - 1;
        }
        if(exactY >= getWorld().getHeight()) {
            exactY = 0;
        }
        if(exactY < 0) {
            exactY = getWorld().getHeight() - 1;
        }
        super.setLocation((int) exactX, (int) exactY);
    }

    /**
     * Set the location from exact coordinates.
     */
    public void setLocation(double x, double y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation((int) x, (int) y);
    }

    /**
     * Set the location from int coordinates.
     */
    public void setLocation(int x, int y) 
    {
        exactX = x;
        exactY = y;
        super.setLocation(x, y);
    }

    /**
     * Return the exact x-coordinate (as a double).
     */
    public double getExactX() 
    {
        return exactX;
    }

    /**
     * Return the exact y-coordinate (as a double).
     */
    public double getExactY() 
    {
        return exactY;
    }

    /**
     * Increase the speed with the given vector.
     */
    public void addForce(Vector force) 
    {
        movement.add(force);
    }

    /**
     * Accelerate the speed of this mover by the given factor. (Factors < 1 will
     * decelerate.)
     */
    public void accelerate(double factor)
    {
        movement.scale(factor);
        if (movement.getLength() < 0.15) {
            movement.setNeutral();
        }
    }

    /**
     * Return the speed of this actor.
     */
    public double getSpeed()
    {
        return movement.getLength();
    }

    /**
     * Stop movement of this actor.
     */
    public void stop()
    {
        movement.setNeutral();
    }

    /**
     * Return the current speed.
     */
    public Vector getMovement() 
    {
        return movement;
    }
}
danpost danpost

2015/12/11

#
Add another int field to the class called 'turningDirection'. When not turning its value should be zero. Positive and negative one can represent turning right and left, respectively. Basically the value would be the mathematical sign of the change in rotation. Then, in the 'checkKeys' method, add a local int for the direction of turn indicated by the keys. Finally, if no turn keys are detected, use the current 'turningDirection' value.
You need to login to post a reply.