A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Open source: AnimatedSprite class

  1. #1
    Senior Member sand858's Avatar
    Join Date
    Aug 2001
    Posts
    327

    Open source: AnimatedSprite class

    I've been playing around with AS 3.0, and I will be the first to admit that I feel like a total nubb as I try to figure out my own set of new "best practices." Apart of my tinkerings have involved putting together a test project. This test project needs an animated sprite class, so I wrote up a quick one this afternoon.

    You're free to use this code however you want, rename it, resell it, whatever. I'm hoping to gain some insights into how best to optomize it from people more smarter/experienced than myself (yes, you who is reading this).

    If it is a complete piece of crap, then please, tear it to shreds, but do so in this thread so I can figure out why it is crap so I can fix it.

    The part of it that makes me the most uncomfortable is how I update frames. I just removeChild the current, and addChild the next in the sequence. I can't help but feel this is suboptimal.

    Without further adieu:

    Code:
    package {
    
    import flash.display.*;
    import flash.events.*;
    import flash.utils.getTimer;
    
    // Usage: gotoAndStop/gotoAndPlay are 0-indexed
    // time is in milliseconds
    
    public class AnimatedSprite extends Sprite
    {
    	public static const k_loopForever = -1;
    	public static const k_loopOnce = 0;
    	
    	var m_frames : Array = new Array();
    	
    	var m_activeFrameIndex : int;
    	var m_activeObject : DisplayObject;
    	var m_timeOfLastUpdate : int;
    	
    	var m_timeBetweenFrames : int;
    	var m_accumulatedTime : int;
    	
    	var m_isPaused : Boolean;
    	var m_desiredLoopCount : int;
    	var m_actualLoopCount : int;
    	
    	function AnimatedSprite( a_frames : Array, a_timeBetweenFrames : int, a_desiredLoopCount : int = k_loopForever )
    	{
    		m_frames = a_frames;		
    		m_timeBetweenFrames = a_timeBetweenFrames;
    		m_desiredLoopCount = a_desiredLoopCount;
    		if( m_desiredLoopCount < k_loopForever )
    		{
    			trace( "AnimatedSprite.constructor, desired loop count cannot be negative.");
    			m_desiredLoopCount = k_loopForever;
    		}
    		initialize();
    	}
    	
    	public function dispose()
    	{
    		m_activeObject = null;
    		m_frames.splice( 0 );
    		m_frames = null;
    	}	
    	
    	public function stop()
    	{
    		m_isPaused = true;
    	}
    	
    	public function play()
    	{
    		m_isPaused = false;		
    	}
    	
    	public function gotoAndStop( a_frameNumber : int )
    	{
    		gotoAndPlay( a_frameNumber );
    		stop();
    	}
    	
    	public function gotoAndPlay( a_frameNumber : int )
    	{
    		if( a_frameNumber < 0 && a_frameNumber >= m_frames.length )
    		{
    			trace( "AnimatedSprite.gotoAndPlay, frame out of range." );
    			a_frameNumber = 0;
    		}
    		m_activeFrameIndex = a_frameNumber;
    		forceUpdate();
    	}
    	
    	public function getCurrentFrame() : int
    	{
    		return m_activeFrameIndex;
    	}
    	
    	// Private Functions - Nothing to see here, move along now...
    	
    	private function handleFrame( a_event : Event )
    	{
    		var deltaTime : int = calculateDeltaTime();
    		if( m_isPaused ) return;
    		
    		m_accumulatedTime += deltaTime;
    		
    		if( m_accumulatedTime < m_timeBetweenFrames )
    		{
    			// We haven't accrued enough time to tick the sprite yet
    			return;
    		}
    		
    		m_accumulatedTime -= m_timeBetweenFrames;
    		
    		m_activeFrameIndex++;
    
    		if( m_activeFrameIndex >= m_frames.length )
    		{
    			m_actualLoopCount++;
    			
    			if( m_desiredLoopCount == k_loopForever || m_actualLoopCount <= m_desiredLoopCount )
    			{
    				m_activeFrameIndex = 0;				
    			}
    			else
    			{
    				m_activeFrameIndex = m_frames.length - 1;
    				stop();
    			}
    		}
    			
    		updateFrame();
    	}
    	
    	private function forceUpdate()
    	{
    		m_accumulatedTime = 0;
    		updateFrame();
    	}	
    	
    	private function initialize()
    	{
    		m_actualLoopCount = 0;
    		m_accumulatedTime = 0;
    		m_isPaused = false;
    		
    		m_activeObject = null;		
    		m_activeFrameIndex = 0;
    		m_timeOfLastUpdate = getTimer();		
    		
    		drawActiveFrame(  );
    		
    		addEventListener( Event.ENTER_FRAME, handleFrame, false, 0, true );				
    	}
    				
    	private function calculateDeltaTime() : int
    	{
    		var currentTime : int = getTimer();
    		var deltaTime : int = currentTime - m_timeOfLastUpdate;
    		m_timeOfLastUpdate = currentTime;
    		return deltaTime;
    	}		
    	
    	private function updateFrame()
    	{
    		undraw();
    		drawActiveFrame();
    	}
    	
    	private function undraw()
    	{
    		removeChild( m_activeObject );
    	}
    	
    	private function drawActiveFrame( )
    	{
    		m_activeObject = m_frames[ m_activeFrameIndex ];
    		addChild( m_activeObject );
    	}
    }
    
    }
    and the example code:

    Code:
    var frames : Array = new Array();
    
    frames.push( new Bitmap( new ex1() ) ); // Ex1 is a bitmapData
    frames.push( new Bitmap( new ex2() ) ); // Ex2 is a bitmapData
    frames.push( new Bitmap( new ex3() ) ); // Ex3 is a bitmapData
    frames.push( new Bitmap( new ex4() ) ); // Ex4 is a bitmapData
    
    var animatedSprite : AnimatedSprite = new AnimatedSprite( frames, 1000 );
    addChild( animatedSprite );
    animatedSprite.x = 150;
    animatedSprite.y = 150;
    Attached Files Attached Files
    gamedozer games
    Free multiplayer and singleplayer games

  2. #2
    Senior Member sand858's Avatar
    Join Date
    Aug 2001
    Posts
    327
    Anything I can do to encourage some feedback?
    gamedozer games
    Free multiplayer and singleplayer games

  3. #3
    half as fun, double the price senocular's Avatar
    Join Date
    Feb 2002
    Location
    San Francisco, CA (USA)
    Posts
    4,358
    I think people like quick access to URLs which show movies using the class in a cool way

  4. #4
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    Quote Originally Posted by senocular
    I think people like quick access to URLs which show movies using the class in a cool way
    I can see that you seem to belong to those people, too

    Some people apparently have a job and many other things to do including a family life


    Here is an alternative, I came up, since you don't like remove and add.

    Script has been edited from a former version:
    PHP Code:
    private function updateFrame()
        {
            for (var 
    i=0i<=m_frames.length-1;i++)
            {
                
    m_activeObject m_frames];
                
    addChildm_activeObject );
                
    m_activeObject.alpha 0;
            }
                
            
    undraw();
            
    drawActiveFrame();
        }
        
        private function 
    undraw()
        {
            
    myObject.alpha 0;
            
    trace(getChildIndex(myObject));
        }
        
        private function 
    drawActiveFrame( )
        {
            
    m_activeObject m_framesm_activeFrameIndex ];
            
    m_activeObject.alpha 100;
            
    myObject m_activeObject;
        } 
    Further needs to be added to the variable portion:

    private static var myObject:Object;
    Last edited by cancerinform; 09-05-2006 at 10:52 PM.
    - The right of the People to create Flash movies shall not be infringed. -

  5. #5
    Senior Member UnknownGuy's Avatar
    Join Date
    Jul 2003
    Location
    Canada
    Posts
    1,361
    I haven't looked at the zip file, but I don't understand the use of an animated sprite.

    Why not just use a movieclip?

    (I have been using AS3 some, like the speed )

  6. #6
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    My point to this class is that the objects should be loaded dynamically instead of being in the library. That topic was originally covered in Flash MX
    - The right of the People to create Flash movies shall not be infringed. -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center