[As3] role-based action processing for a single image

Source: Internet
Author: User

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 ();
  • }
  • }
  • }
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.