I'm having scope problems in onMotionFinished when I set a bunch of tweens in a for loop: (AS2)
for (var i:Number=0; i<navSectionArray.length; i++){ var scope:MovieClip = mcNav["mc"+navSectionArray[i]]; if (navSectionArray[i] == selectedNav){ // selected this["bgTween"+i] = new Tween(scope.mcBG, "_width", Strong.easeOut, scope.mcBG._width, 100, .5, true); this["bgTween"+i].onMotionFinished = function(){ trace ("open: "+scope); }; } else { // others this["bgTween"+i] = new Tween(scope.mcBG, "_width", Strong.easeOut, scope.mcBG._width, 1, .5, true); this["bgTween"+i].onMotionFinished = function(){ trace ("close: "+scope); }; } }
The problem, obviously, is that they ALL trace the same scope, because the for loop has completed before any of the onMotionFinished functions have fired. I can't figure out how to be able to pass a target or a var or something through to the onMotionFinished function(s) that will persist until those functions actually fire.
Is there a way in onMotionFinished to get the target of the original tween?
There is a DelegateWithArgs class that you could use.
Most tween engines like 'tweener' allow you to pass scope to the finished function, and you can always dump an object on the tweened objects scope as well
Probably the OOP way is to create a class
for (var i:Number=0; i<navSectionArray.length; i++){ var bgTweener: BgTweener = new BgTweener( mcNav["mc"+navSectionArray[i]], this["bgTween"+i] ); }
in haXe there is a callback that makes life very simple.
Thanks, I guess I'll have to create a class for it or something.
Well depends but it might be overkill...
For dealing with lots of buttons special Delegates are useful, quick google found this.. http://www.ultrashock.com/forums/actionscript/pass-movieclip-instance-name-95292.html
But for tweening built in tweens are really not the best way to go...
Tweener is the common approach for example see here...http://hosted.zeh.com.br/tweener/docs/en-us/parameters/onCompleteScope.html
Many are switching to tweenlite... //taken from examples page import gs.; import gs.easing.; TweenLite.to(clip_mc, 5, {_alpha:0.5, _x:120, ease:Back.easeOut, delay:2, onComplete:onFinishTween, onCompleteParams:[5, clip_mc]}); function onFinishTween(parameter1:Number, parameter2:MovieClip):void { trace("The tween has finished! parameters: " + parameter1 + ", and " + parameter2); } but its not strictly opensource so it may not fit you need, but prob will.