ActionScript animation efficincy (AS2.0)

andrewkhan
andrewkhan used Ask the Experts™
on
Hi, I currently have an animation using a .prototype function on a MovieClip (cannot use .as)

I'm using it as I find it more versatile than to use say, the tween class. As it sometimes gets called halfway through an animation. The problem I am having is it is causing a lot of processor lag to the rest of my animation while it is playing.

What is the most efficient way to animate objects dynamically?

(Note that I CANNOT use: .as files or any external libaries, apart from default ones such as the tween class)

Any help/ideas appreciated.

Thanks,
Andrew
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2009

Commented:
hi, explain why you think it's more versatile? there's no doubt doing a tween with code is must more efficient than doing a frame based animation.  why can't you use .as files?

Author

Commented:
Why is creating a tween with animation more efficient though? I'm looking for an explanation rather than a definite answer. Because the SWF is hosted on an external server out of my control and is embeded inside another SWF. On this server I cannot host .as files.
Top Expert 2009
Commented:
you realise that once you compile the file,the .as file is no more. when you compile it grabs the code form the .as files and sticks them into the swf.
you don't need it on the server.
a definitive answer as to why, i don't have one.
I remember back in the days they showed the physical evidence in terms of CPU usage.

usually timeline animations are based on loops of some sort, either a frame, timer or setinterval, i think its this loop that becomes cpu intensive. if i ever come across a definitive answer I"ll let you know.
Definitely agree with Blue on loops being inefficient.

Are you saying you are suffering from the problem that importing the tween class in an embedded swf conflicts with the class in the parent when loaded across domains?

In addition, running multiple Tweens simultaneously (whatever the method) will bog your movie down. The only workable solution I've ever found is to stagger my animations one after the other wherever possible.

Do you have an online example of the problem?
Commented:
the really important thing here is what blue-genie said:  "you realise that once you compile the file,the .as file is no more. when you compile it grabs the code form the .as files and sticks them into the swf."

an .as file is not a .js file.  a .swf file is compiled.  that means, if you use 1 or 100 .as files in your swiff, you can then delete those .as files entirely, move the .swf file to another computer, and it'll still work.

the only time a swiff uses an external file are those that are explicitly loaded using Loader, URLLoader, or similiar objects.

so, hopefully that makes sense - and any of the publicly available Tweening classes (Tweener, TweenLite, Tweensy, etc) are all very efficient and have been tested thoroughly.  there is always debate as to which is the "fastest" but unless you're doing huge amounts of tweens every frame, the difference is negligible (I've tested this personally, a LOT).

if you still absolutely don't want to use an external library, you can roll your own - the basic principle is to run ONE timer and have all the animation run through it.  i'll attach an example of what I believe to be the fastest possible frame-script for animating basic properties.

HTH


import fl.transitions.easing.*;

var list:Dictionary = new Dictionary();
var initted:Boolean = false;
var engine:Shape = new Shape();
 
function exec(event:Event):void{
	var working:Boolean = false;
	var key:Function;
	for each(key in list){
		key();
		working = true;
	}
	if(!working){
		engine.removeEventListener(Event.ENTER_FRAME, exec);
		initted = false;
	}
}

function animate(target:Object, steps:int, ease:Function, props:Object, overwrite:Boolean = true):void{

	if(!initted){
		engine.addEventListener(Event.ENTER_FRAME, exec, false, 0, true);
		initted = true;
	}

	var reference = overwrite ? target : { };
	var loop:uint = 0;
	var factor:Number;
	var prop:String;
	var old:Object = { };

	for (prop in props) {
		old[prop] = target[prop];
	}

	list[reference] = function():void{
		factor = ease(++loop, 0, 1, steps);
		for (prop in props) {
			target[prop] = old[prop] + (props[prop] - old[prop]) * factor;
		};
		if(loop == steps){
			delete list[reference];
		}
	}
}

// usage..
// animate(target (DisplayObject), duration (uint [frames]), easing (Function), destination (Object)
// the line below would tween someMC to x:500 over 30 frames using Strong.easeOut
animate(someMC, 30, Strong.easeOut, { x : 500 } );

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial