Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package FlashGame.LevelObjects
- {
- import flash.events.Event;
- import mx.collections.ArrayList;
- import mx.controls.Image;
- import spark.components.Group;
- /**
- * LevelObject is the superclass for all objects which make up a level.
- * It contains logic for creating a small animation from a series of
- * images, and a local coordinate system for positioning level objects
- * correctly.
- *
- * @author Andrew Dodd <2010-11-15>
- */
- public class LevelObject extends Group
- {
- private var _mapX:uint, _mapY:uint = 0;
- private var _frames:ArrayList;
- private var _currentFrame:uint;
- private var _frameskip:uint, _currFrameskip:uint;
- /**
- * Default constructor.
- *
- * @param posX The X position of the object, in blocks
- * @param posY The Y position of the object, in blocks
- * @param animated True if this is to be an animated object
- * @param frameskip The number of frames to skip on an animated object,
- * used to control the delay between frames - a
- * frameskip of 1 will skip every other frame event
- */
- public function LevelObject(posX:uint, posY:uint,
- animated:Boolean = false,
- frameskip:uint = 2)
- {
- super();
- _mapX = posX; _mapY = posY;
- updateLocalPosition();
- _frames = new ArrayList();
- _currentFrame = _currFrameskip = 0;
- _frameskip = frameskip;
- /* If this is an animated sprite then add a frame event handler */
- if(animated) {
- addEventListener(Event.ENTER_FRAME, onEnterFrame);
- }
- }
- /**
- * updateLocalPosition sets the true x and y coordinates of the object
- * based on its block coordinates
- */
- protected final function updateLocalPosition():void {
- x = _mapX * 32;
- y = _mapY * 32;
- }
- /**
- * addSource adds an image to the image array. The first image added to
- * the array is played back first, then the next, and so on.
- *
- * @param source The image to add as a source.
- */
- protected function addSource(source:String):void {
- var img:Image = new Image();
- img.source = source;
- img.visible = true;
- addElement(img);
- _frames.addItem(img);
- }
- /**
- * Display the next frame
- */
- private function onEnterFrame(e:Event):void {
- /* If we are skipping frames, check if this is a skipped frame */
- if(_frameskip > 0) {
- _currFrameskip = (_currFrameskip + 1) % (_frameskip + 1);
- }
- /* If it is not a skipped frame, get the next frame */
- if(_currFrameskip == 0)
- _currentFrame = (_currentFrame + 1) % _frames.length;
- /* Set the correct image to be visible */
- for (var i:uint = 0; i < _frames.length; i++) {
- Image(_frames.getItemAt(i)).visible = (i == _currentFrame);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement