象強い
アニメーションするキャラクターをもっと簡潔に実装したい
/**
* Copyright kuma360 ( http://wonderfl.net/user/kuma360 )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/etsw
*/
//アニメーションするキャラクターをもっと簡潔に実装したい
package
{
import flash.display.*;
import flash.events.*;
public class Main extends Sprite
{
private var _canvas:BitmapData ;
private var _zo:ZO;
public function Main():void
{
_canvas = new BitmapData ( stage.stageWidth , stage.stageHeight , false , 0 ) ;
addChild ( new Bitmap ( _canvas ) ) ;
_zo = new ZO ;
var C:Boolean = false ;
stage.addEventListener ( MouseEvent.MOUSE_DOWN , function ( ) :void { C = true ; } ) ;
stage.addEventListener ( MouseEvent.MOUSE_UP , function ( ) :void { C = false ; } ) ;
addEventListener (
Event.ENTER_FRAME ,
function ( e:Event ) :void {
_canvas.fillRect ( _canvas.rect , 0 ) ;
_zo.update ( _canvas , mouseX , mouseY , C ) ;
}
) ;
}
}
}
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.net.*;
import flash.system.*;
class ZO {
private var _V:Vector.<BitmapData> = new Vector.<BitmapData> ;
private var _p:Point = new Point ;
private var _wait:int = 0 ;
private var _anim:int = 0 ;
public function ZO ( ) {
var L:Loader = new Loader ( ) ;
L.contentLoaderInfo.addEventListener ( Event.COMPLETE , COMP ) ;
L.load ( new URLRequest ( "http://assets.wonderfl.net/images/related_images/7/7f/7fa5/7fa5dec9973c04fa97d42f3ab12198479ac85c76" ) , new LoaderContext ( true ) ) ;
}
public function COMP ( e:Event ) :void {
var B:Bitmap = e.target.content as Bitmap ;
if ( B ) {
var A:Array = [
new Rectangle ( 64 * 0 , 64 * 0 , 64 , 64 ) ,
new Rectangle ( 64 * 0 , 64 * 1 , 64 , 64 ) ,
new Rectangle ( 64 * 0 , 64 * 2 , 64 , 64 ) ,
new Rectangle ( 64 * 0 , 64 * 3 , 64 , 64 ) ,
new Rectangle ( 64 * 0 , 64 * 4 , 64 , 64 ) ,
new Rectangle ( 64 * 1 , 64 * 0 , 128 , 64 ) ,
new Rectangle ( 64 * 1 , 64 * 1 , 128 , 64 ) ,
new Rectangle ( 64 * 1 , 64 * 2 , 128 , 64 ) ,
new Rectangle ( 64 * 1 , 64 * 3 , 128 , 64 ) ,
new Rectangle ( 64 * 1 , 64 * 4 , 128 , 64 )
];
var BD:BitmapData = new BitmapData ( B.width , B.height , true , 0 ) ;
BD.draw ( B ) ;
var T:BitmapData ;
for ( var I:int = 0 ; I < A.length ; ++ I ) {
T = new BitmapData ( A[I].width , A[I].height , true , 0 ) ;
T.copyPixels ( BD , A[I] , new Point ) ;
_V.push ( T ) ;
}
}
}
public function update ( canvas:BitmapData , X:Number , Y:Number , click:Boolean ) :void {
//状態チェック
var state:int = 0 ;
if ( click ) {
state = 1 ;
} else {
if ( 5 <= _anim ) {
state = 2 ;
} else {
state = 3 ;
}
}
//アニメーション
switch ( state ) {
case 1 :
_wait = 0 ;
_anim = ( _anim == 5 ) ? 6 : 5 ;
break ;
case 2:
if ( 1 < ++ _wait ) {
_wait = 0 ;
++ _anim ;
if ( 10 == _anim ) {
_anim = 0 ;
}
}
break ;
case 3:
//歩く
_p.x += ( X - _p.x ) * .1 ;
_p.y += ( Y - _p.y ) * .1 ;
if ( 5 < ++ _wait ) {
_wait = 0 ;
_anim = ( _anim ) ? 0 : 2 ;
}
break ;
default:
break ;
}
//描画
if ( _anim < _V.length ) {
var BD:BitmapData = _V[_anim] ;
canvas.copyPixels ( BD , BD.rect , _p ) ;
}
}
}