flash on 2011-7-10
package
{
import flash.display.Bitmap;
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;
import flash.utils.getDefinitionByName;
public class Main extends Sprite
{
private var URL:Array =
[
"http://assets.wonderfl.net/images/related_images/0/02/029f/029fce8f2107caffa5f2d67c747d51f4115651d8",
"http://assets.wonderfl.net/images/related_images/3/35/35be/35beb95061cf172f66b1e2ff290d0c67eefb05d5",
"http://assets.wonderfl.net/images/related_images/0/0e/0e3e/0e3eb07776aedf89dbc6170696303543ee1c72d2",
"http://assets.wonderfl.net/images/related_images/7/77/7784/7784e5666cb4f4430bd0cbfac78bca4955ff6318"
];
private var loadCount:int = 0;
private var images:Array = [];
private var animGroup:Array = [];
public function Main()
{
if (stage)
addedToStage();
else
addEventListener(Event.ADDED_TO_STAGE, addedToStage);
}
private function addedToStage(event:Event = null):void
{
removeEventListener(Event.ADDED_TO_STAGE, addedToStage);
for (var i:int = 0; i < URL.length; i++)
{
var loader:Loader = new Loader();
loader.name = i.toString();
loader.contentLoaderInfo.addEventListener(Event.INIT, initHnadler);
loader.load(new URLRequest(URL[i]), new LoaderContext(true));
}
}
private function initHnadler(event:Event):void
{
var loader:Loader = event.currentTarget.loader as Loader;
var bd:BitmapData = new BitmapData(loader.width, loader.height, true, 0x0);
bd.draw(loader);
images[int(loader.name)] = bd;
if (++loadCount == URL.length) init();
}
private function init():void
{
graphics.beginFill(0x0);
graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
graphics.endFill();
AnimSprite.stage = stage;
for (var i:int = 0; i < Character.MENBER_NUM; i++)
{
var member:Character = new Character(i, images[i]);
addChildAt(member, 0);
animGroup.push(member);
}
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 = [];
public static var wait:Boolean = false;
public var left:Boolean;
public var right:Boolean;
public var up:Boolean;
public var down:Boolean;
}
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 = 16;
public var velocity:Point = new Point();
public static const MENBER_NUM:int = 4;
public static var keys:Array = [];
public static var wait:Boolean;
public var index:int;
public function Character(index:int, bd:BitmapData)
{
if (index == 0)
{
for (var i:int = 0; i < MENBER_NUM * 2 + 1; i++)
{
keys.push(new Key());
}
}
this.index = index;
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)
{
var key:Key = new Key();
if (index == 0)
{
wait = false;
key.left = Key.keys[Key.LEFT];
key.right = Key.keys[Key.RIGHT];
key.up = Key.keys[Key.UP];
key.down = Key.keys[Key.DOWN];
}
else
{
if (!wait)
key = keys[index * 2];
}
if (key.left || key.right)
{
velocity.y = 0;
if (key.left && key.right)
{
if (velocity.x < 0)
play("left_idle");
else if (velocity.x > 0)
play("right_idle");
velocity.x = 0;
}
else if (key.left)
{
velocity.x = -SPEED;
play("left");
}
else if (key.right)
{
velocity.x = SPEED;
play("right");
}
}
else if (key.up || key.down)
{
velocity.x = 0;
if (key.up && key.down)
{
if (velocity.y < 0)
play("up_idle");
else if (velocity.y > 0)
play("down_idle");
velocity.y = 0;
}
else if (key.up)
{
velocity.y = -SPEED;
play("up");
}
else if (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;
if (index == 0)
wait = true;
}
if (index == 0 && !wait)
{
keys.unshift(key);
keys.length = keys.length - 1;
}
}
this.x += velocity.x;
this.y += velocity.y;
super.update();
}
}