flash on 2011-7-7
package
{
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.geom.Rectangle;
import flash.net.URLRequest;
import flash.system.LoaderContext;
public class Main extends Sprite
{
private var animGroup:Array = [];
public function Main()
{
graphics.beginFill(0x0);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
graphics.endFill();
var bd:BitmapData = new BitmapData(32, 32, true, 0x33FFFFFF);
bd.fillRect(new Rectangle(1, 1, bd.width - 2, bd.height - 2), 0x0);
var sprite:Sprite = new Sprite();
sprite.graphics.beginBitmapFill(bd);
sprite.graphics.drawRect(0, 0, 465, 465);
sprite.graphics.endFill();
addChild(sprite);
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(event:Event):void
{
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, initHandler);
loader.load(new URLRequest("http://assets.wonderfl.net/images/related_images/4/4a/4a16/4a1679dcbabe6776f39f652ac0574f15a8b15bb5"), new LoaderContext(true));
}
private function initHandler(event:Event):void
{
var loader:Loader = event.currentTarget.loader as Loader;
var chip:BitmapData = new BitmapData(loader.width, loader.height, true, 0x0);
chip.draw(loader);
AnimSprite.stage = stage;
var player:Character = new Character(chip);
addChild(player);
animGroup.push(player);
player.x = 32 * 6;
player.y = 32 * 6;
addEventListener(Event.ENTER_FRAME, onEnterFrame);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
}
private function onKeyDown(event:KeyboardEvent):void
{
Key.keys[event.keyCode] = true;
}
private function onKeyUp(event:KeyboardEvent):void
{
Key.keys[event.keyCode] = false;
}
private function onEnterFrame(event:Event):void
{
for each (var anim:AnimSprite in animGroup)
{
anim.update();
}
}
}
}
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.display.Stage;
import flash.geom.Point;
import flash.geom.Rectangle;
class AnimSprite extends Sprite
{
public static var stage:Stage;
public static const ZERO:Point = new Point();
public var curAnim:Anim;
public var animGroup:Array = [];
public var finished:Boolean = false;
public var time:Number = 0.0;
public var chip:BitmapData;
public var chipWidth:Number;
public var chipHeight:Number;
public var xLength:int;
public var bitmap:Bitmap;
public function AnimSprite(chip:BitmapData, width:Number, height:Number)
{
this.chip = chip;
this.chipWidth = width;
this.chipHeight = height;
this.xLength = chip.width / width;
var bd:BitmapData = new BitmapData(width, height, true, 0x0);
addChild(bitmap = new Bitmap(bd));
}
public function addAnimation(name:String, frames:Array, delay:Number, looped:Boolean = true):void
{
animGroup.push(new Anim(name, frames, 0, 1.0 / delay, looped));
}
public function play(name:String, force:Boolean = false):void
{
if (!force && curAnim != null && curAnim.name == name && (curAnim.looped || !finished)) return;
for each (var anim:Anim in animGroup)
{
if (anim.name == name)
{
curAnim = anim;
finished = false;
time = 0.0;
break;
}
}
}
public function update():void
{
bitmap.bitmapData.fillRect(bitmap.bitmapData.rect, 0x0);
if (curAnim)
{
var tx:int = curAnim.frames[curAnim.index] % xLength;
var ty:int = curAnim.frames[curAnim.index] / xLength;
bitmap.bitmapData.copyPixels(chip, new Rectangle(tx * chipWidth, ty * chipHeight, chipWidth, chipHeight), ZERO);
}
if (curAnim && curAnim.delay > 0 && (curAnim.looped || !finished))
{
time += 1 / AnimSprite.stage.frameRate;
while (time > curAnim.delay)
{
time -= curAnim.delay;
if (curAnim.index >= curAnim.frames.length - 1)
{
finished = true;
if (curAnim.looped) curAnim.index = 0;
}
else curAnim.index++;
}
}
}
}
class Anim
{
public var frames:Array;
public var index:int;
public var delay:Number;
public var name:String;
public var looped:Boolean;
public function Anim(name:String, frames:Array, index:int, delay:Number, looped:Boolean):void
{
this.name = name;
this.frames = frames;
this.index = index;
this.delay = delay;
this.looped = looped;
}
}
class Key
{
public static const LEFT:int = 37;
public static const RIGHT:int = 39;
public static const UP:int = 38;
public static const DOWN:int = 40;
public static var keys:Array = [];
}
class Character extends AnimSprite
{
public static const FRAME_RATE:Number = 12.0;
public static const SPEED:Number = 4.0;
public static const MOVE_SIZE:int = 32;
public var velocity:Point = new Point();
public function Character(bd:BitmapData)
{
super(bd, 32, 32);
addAnimation("down", [0, 1, 2], FRAME_RATE);
addAnimation("left", [3, 4, 5], FRAME_RATE);
addAnimation("right", [6, 7, 8], FRAME_RATE);
addAnimation("up", [9, 10, 11], FRAME_RATE);
addAnimation("down_idle", [1], 0, false);
addAnimation("left_idle", [4], 0, false);
addAnimation("right_idle", [7], 0, false);
addAnimation("up_idle", [10], 0, false);
play("down_idle");
}
override public function update():void
{
if (this.x % MOVE_SIZE == 0 && this.y % MOVE_SIZE == 0)
{
if (Key.keys[Key.LEFT] || Key.keys[Key.RIGHT])
{
velocity.y = 0;
if (Key.keys[Key.LEFT] && Key.keys[Key.RIGHT])
{
if (velocity.x < 0) play("left_idle");
else if (velocity.x > 0) play("right_idle");
velocity.x = 0;
}
else if (Key.keys[Key.LEFT])
{
velocity.x = -SPEED;
play("left");
}
else if (Key.keys[Key.RIGHT])
{
velocity.x = SPEED;
play("right");
}
}
else if (Key.keys[Key.UP] || Key.keys[Key.DOWN])
{
velocity.x = 0;
if (Key.keys[Key.UP] && Key.keys[Key.DOWN])
{
if (velocity.y < 0) play("up_idle");
else if (velocity.y > 0) play("down_idle");
velocity.y = 0;
}
else if (Key.keys[Key.UP])
{
velocity.y = -SPEED;
play("up");
}
else if (Key.keys[Key.DOWN])
{
velocity.y = SPEED;
play("down");
}
}
else
{
if (velocity.x < 0) play("left_idle");
else if (velocity.x > 0) play("right_idle");
else if (velocity.y < 0) play("up_idle");
else if (velocity.y > 0) play("down_idle");
velocity.x = 0;
velocity.y = 0;
}
}
this.x += velocity.x;
this.y += velocity.y;
super.update();
}
}