I’m doing a Asteroids like game, but I am having problem with two things.
I want the ship to rotate when pressing right or left in the specific direction and when pressing up it will accelerate against that direction, but the problem is I don’t know how to do this.
The other thing is that the “laser” should should against that direction, and I want to know how to do this too.
Right now I only got it moving against one direction all the time (laser goes upwards and ship moves funny)
Current code (done in FlashDev).
main.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.Timer;
/**
* ...
* @author ...
*/
public class Main extends Sprite
{
private var
clock:Timer,
player:Ship,
i:int;
private var
bullets:Sprite;
public function Main():void
{
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
// entry point
clock = new Timer(1000 / 60);
clock.addEventListener(TimerEvent.TIMER, update);
clock.start();
player = new Ship(stage.stageWidth / 2, stage.stageHeight / 2);
addChild(player);
bullets = new Sprite();
addChild(bullets);
}
private function update(e:TimerEvent = null):void
{
player.update(bullets);
for (i = bullets.numChildren - 1; i >= 0; i--)
{
var tempBullet:Laser = bullets.getChildAt(i) as Laser;
tempBullet.update();
}
}
}
}
Ship.as
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
/**
* ...
* @author ...
*/
public class Ship extends Sprite
{
private var
KEY_LEFT:Boolean = false,
KEY_RIGHT:Boolean = false,
KEY_UP:Boolean = false,
KEY_DOWN:Boolean = false,
KEY_SPACE:Boolean = false;
private var
bulletTimer:Number = 0,
friction:Number = .98,
xSpeed:Number = 0,
ySpeed:Number = 0;
public function Ship(X:int, Y:int)
{
x = X;
y = Y;
if (stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
graphics.beginFill(0xFFFFFF);
graphics.drawRect( -12, -12, 24, 24);
graphics.endFill();
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyupHandler);
}
public function update(bulletContainer:Sprite):void
{
bulletTimer++;
if (KEY_UP)
{
xSpeed += 3;
ySpeed += 3;
}
if (KEY_LEFT) rotation -= 6;
if (KEY_RIGHT) rotation += 6;
if (KEY_DOWN)
{
xSpeed *= .92;
ySpeed *= .92;
}
xSpeed *= friction;
ySpeed *= friction;
x += xSpeed;
y += ySpeed;
if (xSpeed >= 3) xSpeed = 3;
if (xSpeed <= -3) xSpeed = -3;
if (ySpeed >= 3) ySpeed = 3;
if (ySpeed <= -3) ySpeed = -3;
if (KEY_SPACE && bulletTimer > 24)
{
bulletContainer.addChild( new Laser(x - 12, y - 12, 0, -8));
bulletTimer = 0;
}
if (x > stage.stageWidth + (width / 2)) x = 0;
if (x < 0 - (width / 2)) x = stage.stageWidth;
if (y > stage.stageHeight + (width / 2)) y = 0;
if (y < 0 - (width / 2)) y = stage.stageHeight;
}
private function keydownHandler(e:KeyboardEvent = null):void
{
switch (e.keyCode)
{
case 38:
KEY_UP = true;
break;
case 37:
KEY_LEFT = true;
break;
case 39:
KEY_RIGHT = true;
break;
case 40:
KEY_DOWN = true;
break;
case 32: // Shoot
KEY_SPACE = true;
break;
}
}
private function keyupHandler(e:KeyboardEvent = null):void
{
switch (e.keyCode)
{
case 38:
KEY_UP = false;
break;
case 37:
KEY_LEFT = false;
break;
case 39:
KEY_RIGHT = false;
break;
case 40:
KEY_DOWN = false;
break;
case 32: // Shoot
KEY_SPACE = false;
break;
}
}
}
}
And finally Laser.as
package
{
import flash.display.Sprite
/**
* ...
* @author ...
*/
public class Laser extends Sprite
{
private var
xs:Number,
ys:Number,
destroyTimer:Number = 40;
public function Laser(X:int, Y:int, XSpeed:int, YSpeed:int)
{
x = X;
y = Y;
xs = XSpeed;
ys = YSpeed;
graphics.beginFill(0xFFFFFF);
graphics.drawRect(0, 0, 3, 3);
graphics.endFill();
}
public function update():void
{
if (x < 0 - (width / 2)) x = stage.stageWidth;
if (x > stage.stageWidth + (width / 2)) x = 0;
if (y < 0 - (height / 2)) y = stage.stageHeight;
if (y > stage.stageHeight + (height / 2)) y = 0;
x += xs;
y += ys;
if (destroyTimer <= 0) parent.removeChild(this);
destroyTimer--;
}
}
}
Appreciate any answer that rolls in, even bumps.