我今天在蓝色论坛里看到一个有关"BitmapData类"的问题,我觉得用BitmapData类是可以实现里说的C++处理图片功能,再加我好久没写AS3的程序,都快生锈了!就当练练手吧!我查了一下Flash帮助文件,整理一下思路花了中午时间写出下面document类文件;这个功能比较适合做Flash游戏开发了。我加了键盘事件,用来控制角色移动方向。如果在对类代码中使用的类对象不清楚的话,请看Flash帮助文件吧!
提问者提供一个角色动作图片,我也就在就直接用这图片了!
GameSprite类
- package {
- import flash.display.*;
- import flash.net.*;
- import flash.utils.Timer;
- import flash.events.*;
- import flash.geom.*;
-
- public class GameSprite 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;
- function GameSprite() {
-
- sWidth = 100;
- sHeight = 100;
-
- sDirection = 0;
-
- sStep = 1;
-
- maps = new Array();
-
- pointer = 0;
-
- 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("/download/sprite.png"));
-
- stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
- }
-
- private function errorHandler(event:IOErrorEvent):void {
- trace("IOErrorEvent");
-
- }
-
- private function keyDownHandler(event:KeyboardEvent):void {
- switch (event.keyCode) {
- case 40 :
- sDirection = 0;
- break;
- case 38 :
- sDirection = 3;
- break;
- case 37 :
- sDirection = 1;
- break;
- case 39 :
- sDirection = 2;
- break;
- }
-
- }
-
- private function timerHandler(event:Event):void {
-
- if (map != null) {
- removeChild(map);
- }
-
- map = new Bitmap(maps[sDirection][pointer]);
- addChild(map);
-
- if (pointer < sStep-1) {
- pointer ++;
- } else {
- pointer = 0;
- }
- }
-
- private function completeHandler(event:Event):void {
-
-
-
-
- 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);
-
- bmd.copyPixels(sBmd,new Rectangle(sWidth*i, sHeight*j, sWidth, sHeight),new Point(0,0));
- arr.push(bmd);
- }
-
- maps.push(arr);
- }
-
- sBmd.dispose();
-
- timer.start();
- }
- }
- }