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?
Here is the Bullet code
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);
}
}
}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);
}
}
}
