flash on 2011-7-27
package
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.geom.Rectangle;
public class Main extends Sprite
{
private var player:Sprite = new Sprite();
private var angle:Number = Math.PI / 4;
private var speed:Number = 0.0;
private var keys:Array = [];
private var pattern:BitmapData;
private var matrix:Matrix = new Matrix();
private const PLAYER_POS:Point = new Point(232.5, 232.5);
private const PATTERN_SIZE:Number = 60.0;
private const MAX_SPEED:Number = 5.0;
private const ACCEL:Number = 0.3;
private const ANGLE:Number = 0.1;
private const LEFT:int = 37;
private const UP:int = 38;
private const RIGHT:int = 39;
private const SIZE:Number = 10;
public function Main()
{
addChild(player);
pattern = new BitmapData(PATTERN_SIZE, PATTERN_SIZE, false, 0xAAB6FF);
pattern.fillRect(new Rectangle(1, 1, pattern.width - 2, pattern.height - 2), 0x0);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onKeyUp(event:KeyboardEvent):void
{
keys[event.keyCode] = false;
}
private function onKeyDown(event:KeyboardEvent):void
{
keys[event.keyCode] = true;
}
private function onEnterFrame(event:Event):void
{
var s:int = -1;
if (keys[UP]) s = 1;
speed = clamp(speed + ACCEL * s, 0.0, MAX_SPEED);
if (0 < speed)
{
var h:int = 0;
if (keys[LEFT]) h = -1;
else if (keys[RIGHT]) h = 1;
angle += ANGLE * h;
matrix.translate( -speed * Math.cos(angle), -speed * Math.sin(angle));
}
draw();
}
private function clamp(value:Number, min:Number, max:Number):Number
{
if (value < min) value = min;
if (max < value) value = max;
return value;
}
private function draw():void
{
graphics.beginBitmapFill(pattern, matrix);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
graphics.endFill();
player.graphics.clear();
player.graphics.lineStyle(1.0, 0x0000FF);
player.graphics.beginFill(0xFFFFFF);
player.graphics.moveTo(PLAYER_POS.x + Math.cos(angle) * SIZE, PLAYER_POS.y + Math.sin(angle) * SIZE);
player.graphics.lineTo(PLAYER_POS.x + Math.cos(angle + Math.PI / 1.5) * SIZE, PLAYER_POS.y + Math.sin(angle + Math.PI / 1.5) * SIZE);
player.graphics.lineTo(PLAYER_POS.x, PLAYER_POS.y);
player.graphics.lineTo(PLAYER_POS.x + Math.cos(angle - Math.PI / 1.5) * SIZE, PLAYER_POS.y + Math.sin(angle - Math.PI / 1.5) * SIZE);
player.graphics.lineTo(PLAYER_POS.x + Math.cos(angle) * SIZE, PLAYER_POS.y + Math.sin(angle) * SIZE);
player.graphics.endFill();
}
}
}