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

2016/11/14

Best way to get the projectile to Fire in a certain direction.

dlamc dlamc

2016/11/14

#
I'm trying to create a shooter game that involves a tank that fires a Bullet. I am able to get the tank to move and fire projectiles, but I still can't figure out how to fire a bullet and keep it moving in the same direction that it was fired. What is the best way to do this, and what kind of code do I need?
public class Tank extends Actor
{
    /**
     * Act - do whatever the Tank wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {   int speed=1;
        
        if (Greenfoot.isKeyDown("up"))
        {
           setRotation(270);
           move(speed);
        }
        if (Greenfoot.isKeyDown("down"))
        {
            setRotation(90);
            move(speed);
        }
        if (Greenfoot.isKeyDown("right"))
        {
            setRotation(0);
            move(speed);
       
        }
        if (Greenfoot.isKeyDown("left"))
        {
            setRotation(180);
            move(speed);
        }
        if(Greenfoot.isKeyDown("Space"))
        {
            World w = getWorld();
  
                int x = (int)(this.getX());
                int y = (int)(this.getY());
            w.addObject(new Bullet(),x,y);
            
        
           
            
        }
    }    
}
Here is the Bullet code
public class Bullet extends Actor
{   
    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
   { int speed=4;
      move(speed);
       if (Greenfoot.isKeyDown("up"))
    {
        setRotation(270);
        
    }
    if (Greenfoot.isKeyDown("down"))
    {
        setRotation(90);
        
    }
    if (Greenfoot.isKeyDown("left"))
    {
        setRotation(180);
        
    }
    if (Greenfoot.isKeyDown("right"))
    {
        setRotation(0);
        
    }
    if (isAtEdge())
        {
            getWorld().removeObject(this);
        }
   }
}
danpost danpost

2016/11/14

#
Remove lines 10 through 29 of the Bullet class. Then, replace lines 35 through 37 of the Tank class with the following:
Bullet bullet = new Bullet();
getWorld().addObject(bullet, getX(), getY());
bullet.setRotation(getRotation());
dlamc dlamc

2016/11/15

#
danpost wrote...
Remove lines 10 through 29 of the Bullet class. Then, replace lines 35 through 37 of the Tank class with the following: ? 1 2 3 Bullet bullet = new Bullet(); getWorld().addObject(bullet, getX(), getY()); bullet.setRotation(getRotation());
I think I understand what your change does, but I'm still learning Greenfoot's syntax. Could you/someone explain to me what this code does just to make sure I understand?
danpost danpost

2016/11/15

#
dlamc wrote...
I think I understand what your change does, but I'm still learning Greenfoot's syntax. Could you/someone explain to me what this code does just to make sure I understand?
Line 1 creates the Bullet object and stores it in an object reference variable 'bullet' which is declared to hold an object of type 'Bullet'. Line 2 adds the bullet into the world at the location of the tank. Line 3 sets the rotation of the bullet to that of the tank. I could have spent lines of code on it like this:
Bullet bullet;
bullet = new Bullet();
World w;
w = this.getWorld();
int x = this.getX();
int y = this.getY();
int r = this.getRotation();
w.addObject(bullet, x, y);
bullet.setRotation(r);
'getX' and 'getY' return int values, so casting is not necessary.
You need to login to post a reply.