package { import flash.display.*; import flash.events.*; import flash.geom.*; import flash.net.*; import flash.utils.Timer; public class Document extends Sprite { private var timer : Timer; private var sWidth : uint; private var sHeight : uint; private var sStep : uint; private var sDirection : uint; private var loader : Loader; private var maps : Array; private var pointer : uint; private var map : Bitmap; private var spriteBox : Sprite; private var funcArr : Array; public function Document() { // 角色大小; sWidth = 100; sHeight = 100; // 角色移动方向; sDirection = 0; // 角色步数; sStep = 1; // 角色动作数组; maps = new Array(); // 初始化角色动作运行指针; pointer = 0; // 初始化time; timer = new Timer(100); timer.addEventListener(TimerEvent.TIMER, timerHandler); // 图片加载对象; loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler); loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler); loader.load(new URLRequest("sprite.png")); stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); spriteBox = new Sprite(); addChild(spriteBox); } // 错误处理事件; private function errorHandler(event : IOErrorEvent) : void { trace("IOErrorEvent"); } // 键盘事件,通过方向键更改角色移动方向; private function keyDownHandler(event : KeyboardEvent) : void { switch (event.keyCode) { case 40 : trace(loader.width); sDirection = 0; break; case 38 : sDirection = 3; break; case 37 : sDirection = 1; break; case 39 : sDirection = 2; break; } games.sDirection = sDirection; } // 定时器运行事件; private function timerHandler(event : Event) : void { var sum = funcArr.length; for (var i : int = 0; i < sum; i++) { funcArr[i].test(); } } private function rollover(e : Event) : void { var bit : BitmapData = e.target.getChildAt(0).bitmapData; var pixel : uint = bit.getPixel(mouseX, mouseY); if (pixel > 0) { e.target.alpha = 0.5; } else { e.target.alpha = 1; } } private function mouseup(e : Event) : void { } private function rollout(e : Event) : void { } private function completeHandler(event : Event) : void { // trace(loader.width); // 根据图片的大小初始化BitmapData; /* * 如果要保留原来的图片的透明度的话,必将transparent设置为true,同时设置填充色值的前两位为00; */ var sBmd : BitmapData = new BitmapData(loader.width, loader.height, true, 0x00FFFFFF); sBmd.draw(loader); // 计算移动步数; sStep = Math.floor(loader.width / sWidth); for (var j : uint = 0; j < Math.floor(loader.height / sHeight); j++) { var arr : Array = new Array(); for (var i : uint = 0; i < sStep; i++) { var bmd : BitmapData = new BitmapData(sWidth, sHeight, true, 0x00FFFFFF); // 获取单个角色的BitmapData对象; bmd.copyPixels(sBmd, new Rectangle(sWidth * i, sHeight * j, sWidth, sHeight), new Point(0, 0)); arr.push(bmd); } // 放入角色数组里; maps.push(arr); } // 释放sBmd资源; sBmd.dispose(); // 开始运行角色动作; initRoles(); } private var games : GameSprite; private function initRoles() : void { funcArr = new Array(); var sum : int = 200; for (var i : int = 0; i < 1; i++) { games = new GameSprite(this.maps); addChild(games); games.x = games.width * (i % 30); games.y = games.height * Math.floor(i / 30); funcArr.push(games); } timer.start(); } } } package { import flash.display.*; import flash.net.*; import flash.utils.Timer; import flash.events.*; import flash.geom.*; import flash.utils.describeType; public class GameSprite extends Sprite { private var timer : Timer; private var sWidth : uint; private var sHeight : uint; private var sStep : uint; public var sDirection : uint; private var loader : Loader; private var maps : Array; private var pointer : uint; private var map : Bitmap; private var spriteBox : Sprite; public var doMove : Function; private var bitmap : Bitmap; public function GameSprite(maps : Array) { // 角色大小; sWidth = 100; sHeight = 100; // 角色移动方向; sDirection = 0; // 角色步数; sStep = 4; // 角色动作数组; this.maps = maps; // 初始化角色动作运行指针; pointer = 0; // 初始化time; // 图片加载对象; // stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler); spriteBox = new Sprite(); addChild(spriteBox); bitmap = new Bitmap(maps[0][0]); addChild(bitmap); /*var obj:TestObject=new TestObject(); var xmlBody : XML = describeType(obj); trace(xmlBody);*/ } // 错误处理事件; private function errorHandler(event : IOErrorEvent) : void { trace("IOErrorEvent"); } // 键盘事件,通过方向键更改角色移动方向; private function keyDownHandler(event : KeyboardEvent) : void { switch (event.keyCode) { case 40 : trace(loader.width); sDirection = 0; break; case 38 : sDirection = 3; break; case 37 : sDirection = 1; break; case 39 : sDirection = 2; break; } } // 定时器运行事件; public function test() : void { // trace("width:"+this.width); // 删除旧的角色动作图像; /*if (map != null) { spriteBox.removeChild(map); } // 显示新的角色动作图像; map = new Bitmap(maps[sDirection][pointer]); spriteBox.addChild(map);*/ bitmap.bitmapData = maps[sDirection][pointer]; // 角色动作循环处理; if (pointer < sStep - 1) { pointer++; } else { pointer = 0; } // trace("pointer:"+pointer); // trace("height:"+this.height); // this.addEventListener(MouseEvent.ROLL_OVER,rollover,true); // this.addEventListener(MouseEvent.MOUSE_UP,mouseup,true); /*spriteBox.addEventListener(MouseEvent.MOUSE_DOWN,rollover); spriteBox.addEventListener(MouseEvent.MOUSE_OVER,rollover); spriteBox.addEventListener(MouseEvent.MOUSE_MOVE,rollover); spriteBox.addEventListener(MouseEvent.MOUSE_OUT,mouseup);*/ // this.addEventListener(MouseEvent.ROLL_OUT,rollout,true); } private function rollover(e : Event) : void { var bit : BitmapData = e.target.getChildAt(0).bitmapData; // trace(mouseX,mouseY); var pixel : uint = bit.getPixel(mouseX, mouseY); if (pixel > 0) { e.target.alpha = 0.5; } else { e.target.alpha = 1; } // trace(pixel); } private function mouseup(e : Event) : void { } private function rollout(e : Event) : void { // trace(e.target); } /*public function test():void{ trace("dddddd"); }*/ } }