Easiest way to fire a bullet up down left or right?


(Nate ) #1

Hey guys I am working on an example program that I would like to fine tune and tweak to perfection so I can insert it into my actual game.

For now the example consists of a player, an enemy, a bullet and some movement of the player.

I would like the player to fire a bullet either up down left or right when the space bar is pressed. The direction of the bullet is determined by the players current direction traveled.

I have it working, to an extent.

I get bullets to fire, but as soon as I change directions, yep you guessed it… all the bullets on the stage change directions with me… it is interesting to watch and might be fun to implement into some kind of physics or puzzle game. But I would like to have the bullets fired stay on their respective paths.

Figuring the way that I was handling the shoot direction was faulty here is the basic code that I have which will only fire the bullet in one direction. Let me know what I would have to add to make it so I can fire up down left or right!

This is in the player class update method:

		if (Input.check('MOVEMENT') && Input.pressed(Key.SPACE))
		{
			var bullet:Bullet = new Bullet(x, y - 16);
			FP.world.add(bullet);
		}

This is my bullet update method:

override public function update():void
	{
	
		
		ySpeed -= power;
		
		//physics code
		
		if (Math.abs(xSpeed) < 1)
		{
			xSpeed=0;
		}
		
		xSpeed *= hFriction;
		ySpeed *= vFriction;
		
		moveBy(xSpeed, ySpeed, ["player", "enemy"]);
		
		
		//end physics code
		
		//remove bullets once they leave the stage or hit an enemy
		
		if (collide("enemy", x , y - 1) || x < 0 || y < 0 || x + this.width > FP.stage.stageWidth || y + this.height > FP.stage.stageHeight)
		{
			FP.world.recycle(this);
		}
		
		
		
	}

Thanks guys! :smiley:


(Nate ) #2

Here is an example of what I currently have, as you can see when you are firing in any direction then switch direction the bullets will just move to the new direction. I understand why it is happening, I just am not sure how to fix it.

https://www.dropbox.com/s/e2zeez23ckxd1ta/ShootExample.swf

Thanks guys!

CONTROLS:

WASD - move

space - shoot


(Nate ) #3

Still not doing exactly what I want it to, bullets will change direction with whatever the current shooting direction is.

https://www.dropbox.com/s/v3pututkceb5vaz/prototype.swf

Any suggestions?

This is the entire Player class:

package  {
import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.FP;
import net.flashpunk.World;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;


public class Player extends Entity
{
	private var power:Number = 0.8;
	private var xSpeed:Number = 0;
	private var ySpeed:Number = 0;
	private var hFriction:Number = 0.9;
	private var vFriction:Number = 0.9;
	private var pressed:Boolean = false;
	public static var shootingDirection:int = 0;
	
	[Embed(source = "assets/player.png")] public static var PLAYER:Class;
	
	
	public function Player(posX:int, posY:int) 
	{
		graphic = new Image(PLAYER);
		setHitbox(32, 32);
		
		type = "player";
		
		x = posX;
		y = posY;
		
		
	}
	
	override public function update():void
	{

		
		//control code
		
		Input.define('MOVEMENT', Key.W, Key.A, Key.S, Key.D);
		
		if (Input.check(Key.W) && !Input.check(Key.S) && !Input.check(Key.A) && !Input.check(Key.D))
		{
			ySpeed -= power;
			pressed = true;
		}
		if ( Input.check(Key.A) && !Input.check(Key.D) && !Input.check(Key.S) && !Input.check(Key.W))
		{
			xSpeed -= power;
			pressed = true;
		}
		if (Input.check(Key.S) && !Input.check(Key.W) && !Input.check(Key.A) && !Input.check(Key.D))
		{
			ySpeed += power;
			pressed = true;
		}
		if (Input.check(Key.D) && !Input.check(Key.A) && !Input.check(Key.S) && !Input.check(Key.W))
		{
			xSpeed += power;
			pressed = true;
		}
		
		//end of control code
		
		
		
		//shoot code
		
					//shoot up
						if (Input.check(Key.W) && Input.pressed(Key.SPACE) && !Input.check(Key.A) && !Input.check(Key.S) && !Input.check(Key.D))
						{
								var bulletU:Bullet = new Bullet(x + 8, y - 16);
								FP.world.add(bulletU);
								shootingDirection = 1;
						}
		
					//shoot left
						if (Input.check(Key.A) && Input.pressed(Key.SPACE) && !Input.check(Key.S) && !Input.check(Key.D) && !Input.check(Key.W))
						{
								var bulletL:Bullet = new Bullet(x - 16, y + 8);
								FP.world.add(bulletL);
								shootingDirection = 2;
						}
	
					//shoot down
						if (Input.check(Key.S) && Input.pressed(Key.SPACE) && !Input.check(Key.D) && !Input.check(Key.W) && !Input.check(Key.A))
						{
								var bulletD:Bullet = new Bullet(x + 8, y + 32);
								FP.world.add(bulletD);
								shootingDirection = 3;
						}
		
					//shoot right
						if (Input.check(Key.D) && Input.pressed(Key.SPACE) && !Input.check(Key.S) && !Input.check(Key.W) && !Input.check(Key.A))
						{
								var bulletR:Bullet = new Bullet(x + 32, y + 8);
								FP.world.add(bulletR);
								shootingDirection = 4;
						}
		
		//end of shoot code
		
		
		
		
		
		
		//physics code
		
		if (Math.abs(xSpeed) < 1 && ! pressed)
		{
			xSpeed=0;
		}
		
		xSpeed *= hFriction;
		ySpeed *= vFriction;
		
		moveBy(xSpeed, ySpeed, ["enemy"]);
		
		
		//end physics code
		
		//keep player on the stage
		
		if (x < 0)
		{
			x = 0;
		}
		if (y < 0)
		{
			y = 0;
		}
		
		if (x + this.width > FP.stage.stageWidth)
		{
			x = FP.stage.stageWidth - this.width;
		}
		if ( y + this.height > FP.stage.stageHeight)
		{
			y = FP.stage.stageHeight - this.height;
		}
		
	}/*end of update method*/
	
}}

This is the bullet class:

package  {
import net.flashpunk.FP;
import net.flashpunk.Entity;
import net.flashpunk.graphics.Image;
import net.flashpunk.World;
import net.flashpunk.utils.Input;
import net.flashpunk.utils.Key;

public class Bullet extends Entity
{
	public static var power:Number = 10;
	private var xSpeed:Number = 0;
	private var ySpeed:Number = 0;
	private var hFriction:Number = 0.9;
	private var vFriction:Number = 0.9;
	
	
	public static var shot:Boolean = false;	
	
	//private var _bullet:Image = new Image(BULLET);
	
	[Embed(source="assets/bullet.png")] public static var BULLET:Class;
	

	public function Bullet(posX:int, posY:int) 
	{
		graphic = new Image(BULLET);
		super(posX, posY);
		setHitbox(16, 16);
		
		type = "bullet";
		
		//x = posX;
		//y = posY;		
	}
	
	override public function update():void
	{
			//ySpeed -= power; //up
	
			//xSpeed -= power;//left
			
			//ySpeed += power;//down
			
			//xSpeed += power;//right
			
			if (Player.shootingDirection == 1)
			{
				ySpeed -= power;
			}
			if (Player.shootingDirection == 2)
			{
				xSpeed -= power;
			}
			if (Player.shootingDirection == 3)
			{
				ySpeed += power;
			}
			if (Player.shootingDirection == 4)
			{
				xSpeed += power;
			}

		
		//physics code
		
		if (Math.abs(xSpeed) < 1)
		{
			xSpeed=0;
		}
		
		xSpeed *= hFriction;
		ySpeed *= vFriction;
		
		moveBy(xSpeed, ySpeed, ["player", "enemy"]);
		
		
		//end physics code
		
		//remove bullets once they leave the stage or hit an enemy
		
		if (collide("enemy", x , y - 1) || collide("enemy", x, y + 1) || collide("enemy", x - 1, y) || collide("enemy", x + 1, y) || x < 0 || y < 0 || x + this.width > FP.stage.stageWidth || y + this.height > FP.stage.stageHeight)
		{
			FP.world.recycle(this);
		}
		
		
		
	}/*end of the update method*/
	

	
}}

My last question is, how easy would it be to make the enemies able to fire back at the player using the same bullet?