Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package FlashGame.LevelObjects
  2. {
  3.     import flash.events.Event;
  4.    
  5.     import mx.collections.ArrayList;
  6.     import mx.controls.Image;
  7.    
  8.     import spark.components.Group;
  9.    
  10.     /**
  11.      * LevelObject is the superclass for all objects which make up a level.
  12.      * It contains logic for creating a small animation from a series of
  13.      * images, and a local coordinate system for positioning level objects
  14.      * correctly.
  15.      *
  16.      * @author Andrew Dodd <2010-11-15>
  17.      */        
  18.     public class LevelObject extends Group
  19.     {
  20.         private var _mapX:uint, _mapY:uint = 0;
  21.        
  22.         private var _frames:ArrayList;
  23.         private var _currentFrame:uint;
  24.         private var _frameskip:uint, _currFrameskip:uint;
  25.        
  26.         /**
  27.          * Default constructor.
  28.          *
  29.          * @param posX      The X position of the object, in blocks
  30.          * @param posY      The Y position of the object, in blocks
  31.          * @param animated  True if this is to be an animated object
  32.          * @param frameskip The number of frames to skip on an animated object,
  33.          *                  used to control the delay between frames - a
  34.          *                  frameskip of 1 will skip every other frame event
  35.          */
  36.         public function LevelObject(posX:uint, posY:uint,
  37.                                     animated:Boolean = false,
  38.                                     frameskip:uint = 2)
  39.         {
  40.             super();
  41.             _mapX = posX; _mapY = posY;
  42.             updateLocalPosition();
  43.            
  44.             _frames = new ArrayList();
  45.             _currentFrame = _currFrameskip = 0;
  46.             _frameskip = frameskip;
  47.    
  48.             /* If this is an animated sprite then add a frame event handler */
  49.             if(animated) {
  50.                 addEventListener(Event.ENTER_FRAME, onEnterFrame);
  51.             }          
  52.         }
  53.        
  54.         /**
  55.          * updateLocalPosition sets the true x and y coordinates of the object
  56.          * based on its block coordinates
  57.          */
  58.         protected final function updateLocalPosition():void {
  59.             x = _mapX * 32;
  60.             y = _mapY * 32;
  61.         }
  62.        
  63.         /**
  64.          * addSource adds an image to the image array. The first image added to
  65.          * the array is played back first, then the next, and so on.
  66.          *
  67.          * @param source        The image to add as a source.
  68.          */
  69.         protected function addSource(source:String):void {
  70.             var img:Image = new Image();
  71.             img.source = source;
  72.             img.visible = true;
  73.            
  74.             addElement(img);
  75.             _frames.addItem(img);
  76.         }
  77.        
  78.         /**
  79.          * Display the next frame
  80.          */
  81.         private function onEnterFrame(e:Event):void {
  82.             /* If we are skipping frames, check if this is a skipped frame */
  83.             if(_frameskip > 0) {
  84.                 _currFrameskip = (_currFrameskip + 1) % (_frameskip + 1);
  85.             }
  86.  
  87.             /* If it is not a skipped frame, get the next frame */
  88.             if(_currFrameskip == 0)
  89.                 _currentFrame = (_currentFrame + 1) % _frames.length;
  90.            
  91.             /* Set the correct image to be visible */
  92.             for (var i:uint = 0; i < _frames.length; i++) {
  93.                 Image(_frames.getItemAt(i)).visible = (i == _currentFrame);
  94.             }
  95.         }
  96.     }
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement