宇宙船2!!!
forked from 宇宙船!!! (diff: 19)
左右キーで回転。 上キーでロケット噴射。 画面からでなくなりました。
ActionScript3 source code
/**
* Copyright pykgg476 ( http://wonderfl.net/user/pykgg476 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/ce11
*/
// forked from pykgg476's 宇宙船!!!
//左右キーで回転。
//上キーでロケット噴射。
//画面からでなくなりました。
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class ShipSim extends Sprite
{
private var vr:Number = 0;
private var ship:Sprite;
private var thrust:Number=0;
private var vx:Number = 0;
private var vy:Number = 0;
private var flame:Boolean = false;
public function ShipSim()
{
init();
}
private function init():void{
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
ship = new Sprite();
addChild(ship);
ship.graphics.clear();
ship. graphics.lineStyle(1,0xffffff);
ship. graphics.moveTo(10,0);
ship. graphics.beginFill(0x000000);
ship. graphics.lineTo(-10,10);
ship. graphics.lineTo(-5,0);
ship. graphics.lineTo(-10,-10);
ship. graphics.lineTo(10,0);
ship.x = stage.stageWidth/2;
ship.y = stage.stageHeight/2;
addEventListener(Event.ENTER_FRAME,onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN,onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,onKeyUp);
}
private function onKeyDown(event:KeyboardEvent):void{
switch(event.keyCode){
case Keyboard.LEFT:
vr = -5;
break;
case Keyboard.RIGHT:
vr = 5;
break;
case Keyboard.UP:
thrust = 0.2;
flame = true;
break;
default:
break;
}
}
private function onKeyUp(event:KeyboardEvent):void{
vr = 0;
thrust = 0;
flame = false;
}
private function onEnterFrame(event:Event):void{
if(flame == true){
ship.graphics.clear();
ship. graphics.lineStyle(1,0xffffff);
ship.graphics.moveTo(10,0);
ship.graphics.beginFill(0x000000);
ship.graphics.lineTo(-10,10);
ship.graphics.lineTo(-5,0);
ship.graphics.lineTo(-10,-10);
ship.graphics.lineTo(10,0);
ship.graphics.moveTo(-7.5,-5);
ship.graphics.lineTo(-15,0);
ship.graphics.lineTo(-7.5,5);
}else{
ship.graphics.clear();
ship. graphics.lineStyle(1,0xffffff);
ship.graphics.moveTo(10,0);
ship.graphics.beginFill(0x000000);
ship.graphics.lineTo(-10,10);
ship.graphics.lineTo(-5,0);
ship.graphics.lineTo(-10,-10);
ship.graphics.lineTo(10,0);
}
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;
var left:Number = 0;
var right:Number=stage.stageWidth;
var top:Number = 0;
var bottom:Number = stage.stageHeight;
if(ship.x-ship.width/2>right){
ship.x=left-ship.width/2;
}else if(ship.x+ship.width/2<left){
ship.x=right+ship.width/2;
}else if(ship.y-ship.height/2>bottom){
ship.y=top-ship.height/2;
}else if(ship.y<top-ship.height/2){
ship.y=bottom+ship.height/2;
}
}
}
}