flash on 2010-4-26
♥0 |
Line 80 |
Modified 2010-04-26 19:35:08 |
MIT License
archived:2017-03-20 00:39:11
ActionScript3 source code
/**
* Copyright 084 ( http://wonderfl.net/user/084 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/A8ad
*/
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class ShipSim extends Sprite{
private var ship:Ship;
private var vr:Number = 0;
private var thrust:Number = 0;
private var vx:Number = 0;
private var vy:Number = 0;
public function ShipSim() {
init();
}
private function init():void {
ship = new Ship();
addChild(ship);
ship.x = stage.stageWidth / 2;
ship.y = stage.stageHeight / 2;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onUp);
}
private function onDown(event:KeyboardEvent):void {
switch(event.keyCode) {
case Keyboard.LEFT:
vr = -5;
break;
case Keyboard.RIGHT:
vr = 5;
break;
case Keyboard.UP:
thrust = 0.2;
ship.draw(true);
break;
defalt;
break;
}
}
private function onUp(event:KeyboardEvent):void {
vr = 0;
thrust = 0;
ship.draw(false);
}
private function onEnterFrame(event:Event):void {
ship.rotation += vr;
var angle:Number = ship.rotation * Math.PI / 180;
var ax:Number = Math.cos(angle) * thrust;
var ay:Number = Math.sin(angle) * thrust;
vx += ax;
vy += ay;
ship.x += vx;
ship.y += vy;
}
}
}
import flash.display.Sprite;
class Ship extends Sprite {
public function Ship() {
draw(false);
}
public function draw(showFlame:Boolean):void {
with (graphics) {
clear();
graphics.lineStyle(1, 0x000000);
moveTo(10, 0);
lineTo(-10, 10);
lineTo(-5,0 );
lineTo(-10, -10);
lineTo(10, 0);
if (showFlame) {
with (graphics) {
moveTo(-7.5, -5);
lineTo(-15, 0);
lineTo(-7.5, 5);
}
}
}
}
}