- Attended a Community Meetup
- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 50 and 99 items
- Exclusive Author
- Gold Mo Grower
- Has been a member for 5-6 years
I am going crazy here. I have a couple simple tween functions in as3. They usually work fine, but sometimes they only partially tween and stop. When I open it in the html page it always breaks the first time. When I refresh it works fine.
function tweenAlpha(getMcName, endTween:Number, tweenTime:Number, killAfter:Boolean):void{
var tweenAlphaTween = new Tween(getMcName, "alpha", easeType, getMcName.alpha, endTween, tweenTime, true);
//if this is true the child gets removed when the motion is finished
if(killAfter == true){
tweenAlphaTween.addEventListener("motionFinish", finishedTween);
function finishedTween(event:Event):void {
main_mc.removeChild(getMcName);
}
}
}
Thanks!
Are there any tweens being created while that object is tweening?
The problems you describe are the main reason I ditched the native tween classes. They seem to have a lot of issues with overwriting themselves.
- Community Superstar
- Item was Featured
- Author was Featured
- Has been a member for 5-6 years
- Won a Competition
- Sold between 50 000 and 100 000 dollars
- Bought between 10 and 49 items
- Referred between 50 and 99 users
- Europe
heh… that is a bug in actionscript 3…
The garbage collector engine who’s job is to delete unused stuff sometimes deletes the Tweens instances and stops the animations… since i discovered this bug i use only Tweener animation class…
That bug is very annoying expecially when you have listeners added to the Tweens, like you have there
is not about the number of the tweens… i saw some files on flashden that freeze right on the fade-in of the logo at the beginning 
Hi dxc381,
Seems like you store your tweens in local function variables. In that way they might be garbage collected before they finish their motions. Declare them as class variables and this is the best practice for scripting tweens.
- Attended a Community Meetup
- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 50 and 99 items
- Exclusive Author
- Gold Mo Grower
- Has been a member for 5-6 years
Thanks guys. That all makes sense now. I thought I was going to punch holes in my computer. I am still pretty new to as3 so your help is very appreciated.
A function within a function is not good for AS3 , put your second function out as a separate one.
you have some scooping issues
first you are creating the tweenAlphaTween inside the function this isnt always bad but in your care it is because you are calling on the function before the first tween is done which will give some very odd and unexpected results.
second your callback function is a nested function this is also bad. this is a form of closure and if not implemented correctly can be devestating to your code and garbage collection. you can not assume the flash garbage collection will work the way you want when you use closure.
a quick google search turned up info on javascript closure but it still applies
Closure
and thirdly you may want to test if the tweenAlphaTween.isPlaying( ) and not let it rerun the tween if it is
the following code is corrected but it is not tested and also without knowing more of your code i can not tell if it will give the effect you are looking for.
var tweenAlphaTween:Tween
function tweenAlpha(getMcName, endTween:Number, tweenTime:Number, killAfter:Boolean):void{
if ( ! tweenAlphaTween.isPlaying( ) ){
tweenAlphaTween = new Tween(getMcName, “alpha”, easeType, getMcName.alpha, endTween, tweenTime, true);
//if this is true the child gets removed when the motion is finished
if(killAfter == true){
tweenAlphaTween.addEventListener(“motionFinish”, finishedTween);
}
}
}
function finishedTween(event:Event):void {
main_mc.removeChild(getMcName);
}
- Attended a Community Meetup
- Community Moderator
- Has been a member for 5-6 years
- United Kingdom
- Contributed a Tutorial to a Tuts+ Site
- Won a Competition
- Contributed a Blog Post
- Beta Tester
- Bought between 50 and 99 items
@dxc381 dude use Tweener rather than the built in stuff, SO much nicer, far more control 
- Attended a Community Meetup
- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 50 and 99 items
- Exclusive Author
- Gold Mo Grower
- Has been a member for 5-6 years
Wow. In reference to the Tweener, how on earth did I do this stuff without it? That thing is 100x better than what I was trying to do. My code is so much cleaner now. I’m sure you guys just made my life a lot easier on many a project. Thanks!
