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?