Jump to content
Search Community

Tween doesn't work in a function?

Hulio test
Moderator Tag

Recommended Posts

What am I doin' wrong here? Anyone please? The tween stops working inside this init-function..? What should I use as a target?

 

function init() {

var current_sum : Number = 120.20;
var count : Number = 0.00;

TweenLite.to(this, 10, {count: current_sum, ease: Expo.easeOut, onUpdate: function () {

	txt_counter.text = format_decimals(count, 2) + ' \u20AC';

}});

}

Link to comment
Share on other sites

You defined your "count" variable as a LOCAL variable inside the parent function. Local variables are always deleted as soon as the parent function finishes running. You're trying to access that local variable long after the parent function finished. Define the variable outside your function and you should be fine.

 

var count : Number = 0.00;
function init() {
  var current_sum : Number = 120.20;
  TweenLite.to(this, 10, {count: current_sum, ease: Expo.easeOut, onUpdate: function () {
     txt_counter.text = format_decimals(count, 2) + ' \u20AC';
  }});
}

 

For the record, I'm not a fan of anonymous functions like the one you're using for onUpdate. It's generally considered a bad practice in ActionScript - it can cause garbage collection problems.

Link to comment
Share on other sites

Are u talking about this format_decimals -function there btw?

Nope, I'm talking about this:

 

BAD:

TweenLite.to(this, 10, {count: current_sum, ease: Expo.easeOut, onUpdate: function () {
     txt_counter.text = format_decimals(count, 2) + ' \u20AC';
  }});

 

GOOD:

TweenLite.to(this, 10, {count: current_sum, ease: Expo.easeOut, onUpdate:myUpdateFunction});
function myUpdateFunction():void {
   txt_counter.text = format_decimals(count, 2) + ' \u20AC';
}

Link to comment
Share on other sites

Ok, now I'm having another problem here. I tried to convert this case to as3, but...

 


           (THE VARIABLES AT THE TOP OF THE DOCUMENT)

           private var mc_counter : Sprite;
           private var counter_txt : TextField;
           private var count : Number = 0; 

           (AND THE FUNCTION AFTER LOADING PROCESS)

           function preload_complete(event : LoaderEvent) : void {

               mc_counter = new Sprite();
               addChild(mc_counter);

               counter_txt = create_textfield(mc_counter, null, null, format_basic, false, false, 150, "center", 1);

               var current_sum : Number = 1000.20;

               TweenLite.to(this, 10, {count: current_sum, ease: Expo.easeOut, onUpdate: update_counter});

               function update_counter() : void {

                   counter_txt.htmlText = '
' + count + ' \u20AC';

               }

           }

 

...the counter doesn't do anything and I can't find out what's the problem. Thanks again!

 

Where is "this" referring to? What should I use instead of it?

Link to comment
Share on other sites

This is why I always recommend avoiding nested functions. Nested functions are deleted when the parent function finishes running. So in your case, the onUpdate was calling a nested function which was deleted.

 

BAD:

function myFunction():void {
   TweenLite.to(this, 10, {count: 1000.20, ease: Expo.easeOut, onUpdate: update_counter});
   function update_counter() : void {
           counter_txt.htmlText = '
' + count + ' \u20AC';
   }
}

 

GOOD:

function myFunction():void {
   TweenLite.to(this, 10, {count: 1000.20, ease: Expo.easeOut, onUpdate: update_counter});
}
function update_counter() : void {
   counter_txt.htmlText = '
' + count + ' \u20AC';
}

Link to comment
Share on other sites

Nested or not, it loops only number zero, so I can't get the count increase. I'm sure it has something to do with the instance "this", I don't know where it is referring to. Everything is okay if I put this to the timeline on the same frame, but in the class it doesn't work...

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...