-
Global variable problem
I load an image and add it to the MC someMC. If "something" is true, the someVariable gets the someMC scaleX number. Let's say its 0.82.
What I need is to get that number into the s.value in my Slider object. Since I want the Slider value to be where my image scale is.
This of course doesn't work because of variable scope limitations.
I have tried setting the variable at the top of the code like this:
var someVariable:Number;
but that didn't work either.
I'm looking for a solution without having to use package and class, since I'm not that steady with AS3 yet.
Here's some illustration code, for the whole code see under this:
Code:
function completeHandler(event:Event):void{
if (something) {
var someVariable:Number = this.someMC.scaleX;
}
}
var s:Slider = new Slider();
s.maximum = 500;
s.minimum = 10;
s.value = someVariable;
I've uploaded all the code to Pastebin. Take a look ;)
Any thoughts?
-
Right now that code will not work because you are declaring the somVariable inside the scope of the completeHandler function. That means when the completeHandler is done, the variable someVariable will go away.
You will need to declare that variable outside of that function first, so that 's' can utilize it later.
I am surprised the code you posted at Pastebin doesn't throw any errors for you. I looked through the whole thing, and the variable sliderValue is never declared.
However, it seems like what I suggest should fix it for you. See what happens.
-
Hi, like I said in my original post, I have already tried to declare it. When I do, I only get a NaN when I trace it at the bottom where the s vars is. Any thoughts?
-
you need to put slider inside a function because the complete handler hasn't executed before your trace at the bottom executes try this.
PHP Code:
var someVariable:Number;
var s:Slider;
function completeHandler(event:Event):void{
if (something) {
someVariable = this.someMC.scaleX;
}
s = new Slider();
s.maximum = 500;
s.minimum = 10;
s.value = someVariable;
}