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

2013/11/17

Firing a missle to go in the direction of the ship

MontclairState MontclairState

2013/11/17

#
When I tried coding the shot to go in the direction of the ship I made it so it keeps changing with the ship's direction. I just want its direction to be the ship's when it's fired not always changing. I know why it's happening but I don't know how to fix it. the code of the ship is
public class Rocket extends Actor
{
    
    private int shotTimer = 0;    
    
    public Rocket()
	{
		setImage("rocket.png");
		getImage().scale(50, 35);
		turn(-90);
	}
    
    /**
     * Act - do whatever the Rocket wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.isKeyDown("left")) {
            turn(-5); // delay next shot
        }
        if (Greenfoot.isKeyDown("right")) {
            turn(5); // delay next shot
        }
        if (Greenfoot.isKeyDown("up")) {
            move(4); // delay next shot
        }
        if (Greenfoot.isKeyDown("down")) {
            move(-4); // delay next shot
        }
        if (shotTimer > 0) {
            shotTimer = shotTimer - 1;
        }
        else if (Greenfoot.isKeyDown("space")) {
            getWorld().addObject(new Shot(this), getX(), getY());
            shotTimer = 50; // delay next shot
        }
        
    }    
}
and the shot's code is
public void act() 
    {
        Rocket rocket = (Rocket) getWorld().getObjects(Rocket.class).get(0);
        setRotation(rocket.getRotation());
        move(5);
        if((this.getX() < 5 || this.getX() > this.getWorld().getWidth() - 5) && (this != null))  
        {  
            this.getWorld().removeObject(this);  
            return;  
        }  
        if((this.getY() < 5 || this.getY() > this.getWorld().getHeight() - 5)  && (this != null))  
        {  
            this.getWorld().removeObject(this);  
            return;  
        }
        
        Actor rock = getOneIntersectingObject(Asteroid.class);
        if (rock != null) {
                // We've hit an asteroid!
                hitAnAsteroid();
                getWorld().removeObject(rock);
                getWorld().removeObject(this);
            }
    }    
    
    private void hitAnAsteroid()
    {
        Space spaceWorld = (Space) getWorld();
        Counter counter = spaceWorld.getCounter();
        counter.bumpCount(5);
    }

    
}
I know the reason is that this code
Rocket rocket = (Rocket) getWorld().getObjects(Rocket.class).get(0);
        setRotation(rocket.getRotation());
is making the shot keep changing to the ships direction but I don't know how I can add it so it only does it once it's fired.
RUMMAKER RUMMAKER

2013/11/17

#
make a constructor for the Shot: public Shot(int rotation) { setRotation(rotation); } the constructor will only execute the code when the shot is created so it will only happen once. When you make the shot just do Shot s = new Shot(*ship's rotation);
MontclairState MontclairState

2013/11/17

#
omg thanks :D all i had to do was change my constructor I already had from
public Shot(Rocket myShip)
    {
        this.myShip = myShip;
}
to this
public Shot(Rocket myShip)
    {
        this.myShip = myShip;
        setRotation(myShip.getRotation());
    }
Without you I would have probably never realized that so thank you very much :D
RUMMAKER RUMMAKER

2013/11/17

#
well np :D glad i can help
You need to login to post a reply.