Go to: http://www.klstudio.com/post/119.html
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 (){
// Role size;
Swidth = 100;
Sheight = 100;
// Move the role to another location;
Sdirection = 0;
// Number of role steps;
SStep = 1;
// Array of role actions;
Maps = new array ();
// Initialize the role action running pointer;
Pointer = 0;
// Initialization time;
Timer = new timer (100 );
Timer. addeventlistener (timerevent. Timer, timerhandler );
// Attach an object to an image;
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 );
}
// Error handling event;
Private function errorhandler (Event: ioerrorevent): void {
Trace ("ioerrorevent ");
}
// Keyboard event. Use the direction key to change the direction of the role;
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;
}
}
// Timer running event;
Private function timerhandler (Event: Event): void {
// Delete the old role action image;
If (map! = NULL ){
Removechild (MAP );
}
// Display the new role action image;
Map = new Bitmap (maps [sdirection] [pointer]);
Addchild (MAP );
// Cyclically process role actions;
If (pointer <sStep-1 ){
Pointer ++;
} Else {
Pointer = 0;
}
}
// Load the Image Processing Event;
Private function completehandler (Event: Event): void {
// Initialize bitmapdata Based on the image size;
/*
* If you want to retain the transparency of the original image, you must set transparent to true and the first two digits of the filled color value to 00;
*/
VaR sbmd: bitmapdata = new bitmapdata (loader. Width, loader. Height, true, 0x00ffffff );
Sbmd. Draw (loader );
// Calculate the number of mobile steps;
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 BMI: bitmapdata = new bitmapdata (swidth, sheight, true, 0x00ffffff );
// Obtain the bitmapdata object of a single role;
BMI. copypixels (sbmd, new rectangle (swidth * I, sheight * j, swidth, sheight), new point ));
Arr. Push (BMI );
}
// Put it in the role array;
Maps. Push (ARR );
}
// Release sbmd resources;
Sbmd. Dispose ();
// Start to run the role action;
Timer. Start ();
}
}
}