flash on 2010-4-13
♥0 |
Line 75 |
Modified 2010-04-13 23:13:51 |
MIT License
archived:2017-03-10 09:09:41
ActionScript3 source code
/**
* Copyright kihon ( http://wonderfl.net/user/kihon )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/3JMt
*/
package
{
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Rectangle;
public class Main extends Sprite
{
private var player:Sprite;
private var left:Boolean = false;
private var right:Boolean = false;
private var top:Boolean = false;
private var bottom:Boolean = false;
private var vx:int = 0;
private var vy:int = 0;
private var isMoving:Boolean = false;
private const SIZE:int = 60;
private const SPEED:int = 10;
public function Main()
{
var bd:BitmapData = new BitmapData(SIZE, SIZE, false, 0xaaaaaa);
bd.fillRect(new Rectangle(1, 1, SIZE - 2, SIZE - 2), 0xFFFFFF);
graphics.beginBitmapFill(bd);
graphics.drawRect(0, 0, 465, 465);
graphics.endFill();
player = new Sprite;
player.graphics.beginFill(0x0);
player.graphics.drawCircle(SIZE / 2, SIZE / 2, SIZE / 2 - 2);
player.graphics.endFill();
player.x = player.y = 0;
addChild(player);
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onEnterFrame(event:Event = null):void
{
if (isMoving)
{
player.x += vx;
player.y += vy;
if (vx && player.x % SIZE == 0) isMoving = false;
if (vy && player.y % SIZE == 0) isMoving = false;
}
else
{
vx = vy = 0;
if (left) vx = -SPEED;
else if (right) vx = SPEED;
else if (top) vy = -SPEED;
else if (bottom) vy = SPEED;
if (vx || vy)
{
isMoving = true;
onEnterFrame();
}
}
}
private function onKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == 37) left = true;
if (event.keyCode == 39) right = true;
if (event.keyCode == 38) top = true;
if (event.keyCode == 40) bottom = true;
}
private function onKeyUp(event:KeyboardEvent):void
{
if (event.keyCode == 37) left = false;
if (event.keyCode == 39) right = false;
if (event.keyCode == 38) top = false;
if (event.keyCode == 40) bottom = false;
}
}
}