AccelerationDocument1
/**
* Copyright 883108 ( http://wonderfl.net/user/883108 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/dWJE
*/
package {
import flash.display.*;
import flash.events.*;
import flash.ui.Keyboard;
import flash.text.*;
public class AccelerationDocument1 extends Sprite {
private var _ship:Ship;
private var _vx:Number = 0;
private var _vy:Number = 0;
private var _ax:Number = 0;
private var _ay:Number = 0;
private var _accell:Number = .2;
private var _keyText:TextField;
private var _velText:TextField;
public function AccelerationDocument1() {
init();
}
private function init():void {
var tex:TextField = new TextField ;
addChild(tex);
tex.width = 450;
tex.x = tex.y = 10;
tex.multiline = true;
tex.textColor = 0xffffff;
tex.text = '矢印キーにあわせてスプライトが移動します。\n';
addChild(_keyText = new TextField);
_keyText.y = 30;
_keyText.width = 100;
_keyText.height = 16;
_keyText.border = true;
_keyText.textColor = 0xffffff;
_keyText.borderColor = 0x333333;
addChild(_velText = new TextField);
_velText.width = 200;
_velText.height = 16;
_velText.x = stage.stageWidth - (_velText.width + 10)
_velText.y = 30;
_velText.textColor = 0xffffff;
_velText.borderColor = 0x333333;
// 背景を黒で塗りつぶします。
graphics.beginFill(0x000000);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
graphics.endFill();
// ステージ上にスプライトを配置します。
addChild(_ship = new Ship);
_ship.x = stage.stageWidth / 2;
_ship.y = stage.stageHeight / 2;
// イベントハンドルを設定します。
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
}
private function enterFrameHandler($event:Event):void {
_vx += _ax;
_vy += _ay;
_ship.x += _vx;
_ship.y += _vy;
// ステージ内にスプライトを保つ処理を入れます。
if(_ship.x < 0){
_ship.x = stage.stageWidth;
}else if(_ship.x > stage.stageWidth){
_ship.x = 0;
}
if(_ship.y < 0){
_ship.y = stage.stageHeight;
}else if(_ship.y > stage.stageHeight){
_ship.y = 0;
}
_velText.text = 'velocity: x = ' + (_vx * 100 | 0)/100 + ', y = ' + (_vy * 100 | 0)/100 + ' px / frame';
}
private function keyDownHandler($event:KeyboardEvent):void {
_ship.boost();
switch ($event.keyCode) {
case Keyboard.LEFT :
_keyText.text = 'LEFT';
_ax = -_accell;
break;
case Keyboard.RIGHT :
_keyText.text = 'RIGHT';
_ax = _accell;
break;
case Keyboard.UP :
_keyText.text = 'UP';
_ay = -_accell;
break;
case Keyboard.DOWN :
_keyText.text = 'DOWN';
_ay = _accell;
break;
default :
break;
}
}
private function keyUpHandler($event:KeyboardEvent):void {
_ax = 0;
_ay = 0;
_keyText.text = ' - ';
_ship.stop_boost();
}
}
}
import flash.display.Sprite;
import flash.events.Event;
class Ship extends Sprite {
private var _flame:Sprite;
private var _body:Sprite;
public function Ship() {
init();
}
private function init():void {
addChild(_flame = new Sprite);
_flame.x=-2;
_flame.graphics.beginFill(0xd90028, .9);
_flame.graphics.moveTo(5,0);
_flame.graphics.lineTo(2, 5);
_flame.graphics.lineTo(-2, 0);
_flame.graphics.lineTo(2, -5);
_flame.graphics.endFill();
_flame.visible=false;
addChild(_body = new Sprite);
_body.graphics.beginFill(0xd90028, .4);
_body.graphics.lineStyle(0, 0xffffff);
_body.graphics.moveTo(20, 0);
_body.graphics.lineTo(0, 10);
_body.graphics.lineTo(5, 0);
_body.graphics.lineTo(0, -10);
_body.graphics.lineTo(20, 0);
_body.graphics.endFill();
}
public function boost():void {
addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
public function stop_boost():void {
removeEventListener(Event.ENTER_FRAME, enterFrameHandler);
_flame.visible=false;
}
private function enterFrameHandler($event:Event):void {
_flame.visible=! _flame.visible;
}
}