Search code examples
actionscript-3flashsprite-sheet

SpriteSheet Class AS3


I want to do my own SpriteSheet Class for ActionScript 3.

The purpose of this class would be a replacement for the MovieClip class, the only difference is that the frames will come from a SpriteSheet. Many of my classes will extend from this class.

My problem is: What if I had a sub-class, for example Ball, that uses the same SpriteSheet for all instances of Ball and i would like to re-use that SpriteSheet to use in all balls to save a lot of memory.


Solution

  • If you want to share the same SpriteSheet for all instances of Ball, your Ball class shouldn't extend from SpriteSheet class. The base class should contain a variable of SpriteSheet type, and be initialized when the class is created.

    So you need a singleton class to save all the SpriteSheet classes with a dictionary, the key of dictionary may like "Ball", "MAN_RUN".The SpriteSheet class will contain a list of BitmapData.

    The code may like this

    public class Animation {
    
        private var _spriteSheet:SpriteSheet;
    
        public function Animation(key:String)
        {
            spriteSheet = SpriteSheetMgr.instance.getSpriteSheet(key);
        }
    }
    
    private var ball:Ball = new Ball("Ball");