Executing frameScript code in MovieClip from Starling

Share Button

framescriptHi anyone! I’m porting a game from default DisplayList to Starling for high rendering performance. During this task I meet some inconvenience – game logic required frame script execution inside starling’s MovieClip. If this could be done in default DisplayList – we could use Flash Professional or addFrameScript for this. Starling don’t have this functionality. So I done this manually. Also want to warning you that my code not the best but it works for me pretty nice :)

Let’s create out MovieClipExtended class:

package gamespoweredby.starling.display {

	import starling.display.MovieClip;
	import starling.textures.Texture;
	import starling.animation.IAnimatable;

	public class MovieClipExtended extends MovieClip implements IAnimatable{

		private var frameScripts : Vector.<Function>;

		public function MovieClipExtended(textures:Vector.<Texture>, fps:int = 12){

			frameScripts = new Vector.<Function>(textures.length,true);
			removeFrameScript();

			super(textures, fps);				
		}

		public function addFrameScript(...args):void{			
			for ( var i : int = 0; i < args.length; i++ ) {
				frameScripts[args[i]] = args[i+1];
				i++;
			}
		}
		// calling removeFrameScript() without arguments will clear all scripts
		public function removeFrameScript(frame:int=-1):void{
			if ( frame == -1 ) {				
				for ( var i : int = 0; i < frameScripts.length; i++ ) {
					frameScripts[i] = dummyFrame;
				}
			} else {
				frameScripts[frame] = dummyFrame;
			}
		}

		override public function advanceTime(passedTime:Number):void{
			frameScripts[ currentFrame ] (currentFrame);
			super.advanceTime(passedTime);
		}
		private function dummyFrame(frame:int):void{

		}

	}

}

As you can see – we extends MovieClip and don’t change it super class. This is really useful for anyone who use swc and not the sources.

Usage:

var myMC: MovieClipExtended = new MovieClipExtended(Backpack.assetsManager.getTextures("b1_gun_"), 60);
myMC.addFrameScript(0, script0, 20, rest, 25, rest );

starling.juggler.add(_myMC);

private function script0(frame:int):void{
	trace('first frame');				
}

private function rest(frame:int):void{
	trace('frame:', frame);				
}

// first frame
// frame: 20
// frame: 25

Really hope that someone could find this helpful!

Share Button

This Post Has Been Viewed 921 Times

One thought on “Executing frameScript code in MovieClip from Starling

Leave a Reply

Your email address will not be published.

Blue Captcha Image Refresh

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>