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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | 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); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | 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 ); } } } |