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

2012/10/16

Rocket Missile

Stephon231 Stephon231

2012/10/16

#
i have a Ship class that fires a Bullet but instead of it adding the bullet above the ship like it is supposed to it adds it to the side of the ship here is the code that is in the ship.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class Ship extends Mover
{
    
    private int gunReloadTime;                  // The minimum delay between firing the gun.
    private int reloadDelayCount;               // How long ago we fired the gun the last time.
    private Vector acceleration;                // How fast the rocket is.
    private int shotsFired;                     // Number of shots fired.
    
    public Ship()
    {
        gunReloadTime = 20;
        reloadDelayCount = 0;
        acceleration = new Vector(0, 0.3);
        increaseSpeed(new Vector(13, 0.3)); // initially slowly drifting
        shotsFired = 0;
    }
    
    /**
     * Act - do whatever the Ship wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        checkKeys();
        reloadDelayCount++;
    }    
    
    public void checkKeys()
    {
        if(Greenfoot.isKeyDown("right")) {
            moveRight();
        }
        if(Greenfoot.isKeyDown("left")) {
            moveLeft();
        }
        if(Greenfoot.isKeyDown("space")) {
            fire();
        }        
   }
   
    public int getSpeed()
    {
        return (int) (getMovement().getLength() * 10);
    }
    
    /**
     * Set the time needed for re-loading the rocket's gun. The shorter this time is,
     * the faster the rocket can fire. The (initial) standard time is 20.
     */
    public void setGunReloadTime(int reloadTime)
    {
        gunReloadTime = reloadTime;
    }
    
    private void fire() 
    {
        if(reloadDelayCount >= gunReloadTime) {
            Bullet b = new Bullet(getMovement().copy(), getRotation());
            getWorld().addObject(b, getX(), getY());
            b.move();
            shotsFired++;
            reloadDelayCount = 0;
        }
    }
    
}
danpost danpost

2012/10/16

#
I do not see anywhere where the ship might rotate. So, in line 61, 'getRotation()' is not neccessary (use '0' instead). In line 62, instead of 'getY()', use 'getY() - 25' (adjust '25' to suit). That will move the start location of the bullet up a bit.
Stephon231 Stephon231

2012/10/16

#
where would i put the (0)
danpost danpost

2012/10/16

#
Change line 61 FROM:
Bullet b = new Bullet(getMovement().copy(), getRotation());
TO:
Bullet b = new Bullet(getMovement().copy(), 0);
You need to login to post a reply.