Space
A little bit of practice to get better in Actionscript and also the first thing I've ever done resembling a game!
To Do:
- Fix up excess line breaks and sloppy code.
- Fix projectiles for multiple instances (a new class is necessary, I think?)
- Add something to actually shoot at.
- Make the ship a little less ugly (make it a lot less ugly, actually).
- Smooth out ship movement.
/**
* Copyright devenulol ( http://wonderfl.net/user/devenulol )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/8twT
*/
package {
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.Sprite;
import flash.filters.BitmapFilterQuality;
import flash.filters.GlowFilter;
import flash.ui.Keyboard;
[SWF(backgroundColor="#000000", frameRate=30)]
public class Space extends Sprite {
public var stars:Array = new Array();
public var starCount:Number = 0;
public var glow1:GlowFilter = new GlowFilter(0x000055, 8, 8, 8, 15, 2, false, false);
public var glow2:GlowFilter = new GlowFilter(0x440044, 8, 8, 8, 15, 2, false, false);
public var glow3:GlowFilter = new GlowFilter(0x222222, 8, 8, 8, 15, 2, false, false);
public var shipGlow:GlowFilter = new GlowFilter(0x440000, 8, 8, 8, 9, 2, false, false);
public var laserGlow:GlowFilter = new GlowFilter(0x004400, 8, 8, 8, 9, 2, false, false);
public var starColors:Array = new Array(glow1,glow2,glow3);
public var i:int = 0;
public var sLocations:Array = new Array();
public var ship:Sprite = new Sprite();
public function Space():void {
for (starCount; starCount < 400; starCount++){
var star:Sprite = new Sprite();
var colorCheck:int = (Math.random() * 3);
star.graphics.beginFill(0xFFFFFF);
star.graphics.drawCircle(0,0,1.3);
star.graphics.endFill();
star.filters = [starColors[colorCheck]];
stars.push(star);
sLocations.push((Math.random() * 10) + 4);
}
for (i; i < starCount; i++){
stars[i].x = (Math.random()*stage.stageWidth);
stars[i].y = (Math.random()*stage.stageHeight);
addChild(stars[i]);
addEventListener(Event.ENTER_FRAME, moveStars);
}
spaceShip();
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
}
public function moveStars(e:Event):void{
for (var n:int = 0; n < i; n++){
stars[n].x -= sLocations[n];
if (stars[n].x < 0){
stars[n].x = stage.stageWidth;
}
}
}
public function spaceShip():void{
ship.graphics.lineStyle(5,0x000000,0);
ship.graphics.beginFill(0xFF0000);
ship.graphics.lineTo(0,10);
ship.graphics.lineTo(25,0);
ship.graphics.lineTo(0,-10);
ship.graphics.lineTo(0,0);
ship.graphics.endFill();
ship.filters = [shipGlow];
addChild(ship);
ship.x = 10;
ship.y = ((stage.stageHeight / 2) - 10);
}
private function onKeyDown(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.UP){
ship.y -= 5;
if (ship.y < 0){
ship.y += 5;
}
}
if (e.keyCode == Keyboard.DOWN){
ship.y += 5;
if (ship.y > stage.stageHeight){
ship.y -= 5;
}
}
if (e.keyCode == Keyboard.SPACE){
immafirinmahlazah();
}
}
public function immafirinmahlazah():void {
var laser:Sprite = new Sprite();
laser.graphics.beginFill(0x00FF00);
laser.graphics.drawRect(0,-1,10,4);
laser.graphics.endFill();
laser.filters = [laserGlow];
laser.x = 10;
laser.y = ship.y;
addChild(laser);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
laser.addEventListener(Event.ENTER_FRAME, moveLaser);
}
public function moveLaser(e:Event, laser){
laser.x += 10;
if (laser.x > (stage.stageWidth)){
removeChild(laser);
}
}
}
}