Skip to main content
Participant
February 1, 2010
Answered

Cant get function to use variable!

  • February 1, 2010
  • 1 reply
  • 401 views

hi I have created an actionscript function which stops an animation on a specific frame which works fine. I have then loaded in a php file with a variable which will contain the number for the frame i want the animation to stop on. This has loaded in fine and i have loaded it in a function. what i cant seem to do is to get the variable into the function which tells the animation to stop playing.

here is my code:

//load variables
varReceiver = new LoadVars(); // create an object to store the variables
varReceiver.load("http://playground.nsdesign6.net/percentage/external.php");
//load variables

//function1
varReceiver.onLoad = function() {
    //value is the var that is created.   
    var paul = this.percentage;
}
//function1
   
   
//function2
this.onEnterFrame = function() {   
if(this._currentframe==(paul)) {
this.stop();
this.onEnterFrame = undefined;
}
}
play();
//function2

This topic has been closed for replies.
Correct answer Ned Murphy

One problem lies in that you are declaring the variable within a function.  This limits the scope of that variable to within the function. So your enterFrame function cannot see it.  One other thing you should consider is not activating the enterFrame function until the variable is read in.  So while I would have suggested you move the variable declaration out of the function, maybe moving the enterFrame into the function will have a similar effect...

varReceiver.onLoad = function() {
    //value is the var that is created.   
    var paul = this.percentage;

    this.onEnterFrame = function() {   
        if(this._currentframe==(paul)) {
          this.stop();
          this.onEnterFrame = undefined;
        }
     }

}
   
play();

 

If that doesn't work, then just try moving the variable declaration outside of the function and only assign its value within it.  That way paul is visible to the second function...

var paul;

//function1
varReceiver.onLoad = function() {
    //value is the var that is created.   
    paul = this.percentage;
}
//function1

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
February 1, 2010

One problem lies in that you are declaring the variable within a function.  This limits the scope of that variable to within the function. So your enterFrame function cannot see it.  One other thing you should consider is not activating the enterFrame function until the variable is read in.  So while I would have suggested you move the variable declaration out of the function, maybe moving the enterFrame into the function will have a similar effect...

varReceiver.onLoad = function() {
    //value is the var that is created.   
    var paul = this.percentage;

    this.onEnterFrame = function() {   
        if(this._currentframe==(paul)) {
          this.stop();
          this.onEnterFrame = undefined;
        }
     }

}
   
play();

 

If that doesn't work, then just try moving the variable declaration outside of the function and only assign its value within it.  That way paul is visible to the second function...

var paul;

//function1
varReceiver.onLoad = function() {
    //value is the var that is created.   
    paul = this.percentage;
}
//function1