Hi Kirupa!
First time poster, long time lurker.
I’ve decided to make a simple little Asteroid style game. I’ve got the basic engine working more or less how I want to, but there’s something not quite right.
I want the enemies to follow the player when they spawn at random locations on the screen. They spawn fine, and follow for 1 frame and then stop. I’ve triple check all event listeners, the enemy follow code, I am totally stuck! If anyone could shed some light on the problem I would be most grateful. I’ll post the entirety of the code, as I’m really not sure what is causing the problem, however the enemy follow section is right down the bottom.
//Import
import flash.events.*;
import flash.display.*;
//Game Mechanics
//Event Listeners
stage.addEventListener(Event.ENTER_FRAME, charRotPos);
stage.addEventListener(MouseEvent.MOUSE_DOWN, shootProjectile);
stage.addEventListener(Event.ENTER_FRAME, addEnemy);
//Variables
var xd:Number;
var yd:Number;
var radAngle:Number;
var size:int = 4;
var shotSpeed:int = 10;
var xSpawnPlacement = Math.floor(Math.random() * 450) + 1;
var ySpawnPlacement = Math.floor(Math.random() * 400) + 1;
//Enemy graphic creation
var enemy:Sprite = new Sprite();
with (enemy.graphics)
{
lineStyle(1, 0x000000, 1);
beginFill(0x000000, 1);
moveTo(-size*2,-size*4);
lineTo(size*2,-size*4);
lineTo(size*2,size*4);
lineTo(-size*2,size*4);
lineTo(-size*2,-size*4);
}
//And placement;
function addEnemy(event:Event):void
{
addChild(enemy);
enemy.x = xSpawnPlacement
enemy.y = ySpawnPlacement
}
//PowerUp graphic creation
var powerUp:Sprite = new Sprite();
with (powerUp.graphics)
{
lineStyle(1, 0x000000, 1);
beginFill(0x000000, 1);
moveTo(-size,-size);
lineTo(size,-size);
lineTo(size,size);
lineTo(-size,size);
lineTo(-size,-size);
}
//Character graphic creation
var triangle:Sprite = new Sprite();
with (triangle.graphics)
{
lineStyle(1, 0x000000, 1);
beginFill(0x000000, 1);
moveTo(-10,10);
lineTo(10,10);
lineTo(0,-10);
lineTo(-10,10);
}
//And placement
addChild(triangle);
triangle.x = 250;
triangle.y = 200;
//Functions
//Finds triangle rotation and position
function charRotPos(event:Event):void
{
xd = triangle.x - stage.mouseX;
yd = triangle.y - stage.mouseY;
radAngle = Math.atan2(yd,xd);
triangle.rotation = int(radAngle*360/(Math.PI*2)-90);
}
//Shoots projectile depending on charRotPos
function shootProjectile(event:MouseEvent):void
{
var shot:Sprite = new Sprite();
with (shot.graphics)
{
lineStyle(1, 0x000000, 1);
moveTo(-size/2,-size);
lineTo(size/2,-size);
lineTo(size/2,size);
lineTo(-size/2,size);
lineTo(-size/2,-size);
}
addChild(shot);
shot.x = triangle.x;
shot.y = triangle.y;
shot.rotation = triangle.rotation;
shot.addEventListener(Event.ENTER_FRAME, moveShot);
}
//Tells projectile how fast/what direction/when to remove the sprite
//Adds PowerUp randomly when enemy is shot
function moveShot(event:Event)
{
with (event.target)
{
x+= shotSpeed*Math.sin(rotation*(Math.PI/180));
y-= shotSpeed*Math.cos(rotation*(Math.PI/180));
if (x>=550||x<=0||y>=400||y<=0)
{
event.target.removeEventListener(Event.ENTER_FRAME, moveShot);
this.removeChild(DisplayObject(event.target));
}
else if (hitTestObject(enemy))
{
xSpawnPlacement = 0;
ySpawnPlacement = 0;
var probability = Math.floor(Math.random() * 10) + 1;
if (probability >= 5)
{
stage.addChild(powerUp);
powerUp.x = enemy.x;
powerUp.y = enemy.y;
xSpawnPlacement = Math.floor(Math.random() * 450) + 1;
ySpawnPlacement = Math.floor(Math.random() * 400) + 1;
}else if (xSpawnPlacement == 0 || ySpawnPlacement == 0){
xSpawnPlacement = Math.floor(Math.random() * 450) + 1;
ySpawnPlacement = Math.floor(Math.random() * 400) + 1;
}
event.target.removeEventListener(Event.ENTER_FRAME, moveShot);
this.removeChild(DisplayObject(event.target));
}
}
}
//Character Movement
//Event Listeners
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(Event.ENTER_FRAME, smoothMovement);
stage.addEventListener(Event.ENTER_FRAME, diagonalSlow);
//Variables;
var downKey = false;
var upKey = false;
var rightKey = false;
var leftKey = false;
var charSpeed:int = 0;
//Keys pressed
function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN || event.keyCode == 83)
{
stage.addEventListener(Event.ENTER_FRAME, down);
downKey = true;
}
else if (event.keyCode == Keyboard.UP || event.keyCode == 87)
{
stage.addEventListener(Event.ENTER_FRAME, up);
upKey = true;
}
else if (event.keyCode == Keyboard.LEFT || event.keyCode == 65)
{
stage.addEventListener(Event.ENTER_FRAME, left);
leftKey = true;
}
else if (event.keyCode == Keyboard.RIGHT || event.keyCode == 68)
{
stage.addEventListener(Event.ENTER_FRAME, right);
rightKey = true;
}
}
//Keys released
function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.DOWN || event.keyCode == 83)
{
stage.removeEventListener(Event.ENTER_FRAME, down);
downKey = false;
}
else if (event.keyCode == Keyboard.UP || event.keyCode == 87)
{
stage.removeEventListener(Event.ENTER_FRAME, up);
upKey = false;
}
else if (event.keyCode == Keyboard.LEFT || event.keyCode == 65)
{
stage.removeEventListener(Event.ENTER_FRAME,left);
leftKey = false;
}
else if (event.keyCode == Keyboard.RIGHT || event.keyCode == 68)
{
stage.removeEventListener(Event.ENTER_FRAME, right);
rightKey = false;
}
}
//Gradual increase to max speed
function smoothMovement(event:Event):void
{
if (charSpeed <= 10 && (downKey || upKey || rightKey || leftKey))
{
charSpeed++;
}
else if (charSpeed >= 0 && (!downKey || !upKey || !rightKey || !leftKey))
{
charSpeed--;
}
}
//Divide speed by half when two non-conflicting directions are called
function diagonalSlow(event:Event):void
{
if (downKey && charSpeed >= 10 && (rightKey || leftKey))
{
charSpeed /= 2;
}
else if (upKey && charSpeed >= 10 && (rightKey || leftKey))
{
charSpeed /= 2;
}
else if (leftKey && charSpeed >= 10 && (upKey || downKey))
{
charSpeed /= 2;
}
else if (rightKey && charSpeed >= 10 && (upKey || downKey))
{
charSpeed /= 2;
}
}
//Move triangle on key press
function down(event:Event):void
{
triangle.y += charSpeed;
trace(charSpeed);
}
function up(event:Event):void
{
triangle.y -= charSpeed;
trace(charSpeed);
}
function left(event:Event):void
{
triangle.x -= charSpeed;
trace(charSpeed);
}
function right(event:Event):void
{
triangle.x += charSpeed;
trace(charSpeed);
}
//Enemy Movement
//Event Listeners
stage.addEventListener(Event.ENTER_FRAME, enemyMovement);
//Variables
//Follow Triangle
function enemyMovement(event:Event):void{
var enemySpeed = 10;
if(enemy.x < triangle.x){
enemy.x += enemySpeed;
}
else if(enemy.x > triangle.x){
enemy.x -= enemySpeed;
}
if(enemy.y < triangle.y){
enemy.y += enemySpeed;
}
else if(enemy.y > triangle.y){
enemy.y -= enemySpeed;
}
}
As promised there is a bonus error in here. When using the WASD keys to control the player, the A key works once, and then never again. Potentially a related issue to the enemy follow? Not too sure.
Eagerly awaiting your response!