views:

361

answers:

3

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.

Here's the 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;

Any thoughts?


Update
I'm looking for a solution without having to use package and class, since I'm not that steady with AS3 yet.

Update 2
I've uploaded all the code to Pastebin. Take a look ;)

+1  A: 

The problem is your handler function doesn't have any information about where your Slider is. The s variable you have there is not really global either, it is local within your class or movieclip. When the handler is executed by code outside your movieclip, there is no this context unless you use a method from a class.

The best way I can think of doing this is to use a Class. Create s as a member variable on the class, then your handler will be able to be referenced it using this. Something like the code below should do it. Be sure to link it up to a library movieclip also.

package {
    import flash.display.MovieClip;

    class MyMovieClip extends MovieClip {
        public var s:Slider;

        public function MyMovieClip {
            var s:Slider = new Slider();
            s.maximum = 500;
            s.minimum = 10;

        }

        function completeHandler(event:Event):void{
            if (something) {
                var someVariable:Number = this.someMC.scaleX;
                this.s.value = someVariable
            }
        }
    }
}
Kekoa
kekoav: See the Updates ;)
mofle
A: 

Well accidentally I see your question again. (remember me?)

Well instead of scaling the image from another domain, you can scale the Loader, so there won't be any cross domain security problem

a working sample here: http://matrixoft.infunity.com/agent/calvin/stackoverflow/getScale.swf

source code here: http://matrixoft.infunity.com/agent/calvin/stackoverflow/getScale.rar

[p.s. see the commented code inside the buttonAction layer, the commented code fails cause it is trying to manipulate the scale of an image from another domain. The code after commented code manipulate the scale of the Loader, not image]

Unreality
I can't get it to work. Can you show me using my code? http://pastebin.com/m7c37b3cf
mofle
perhaps try changing line 52 from this.container.addChild(event.target.content); to this.container.addChild(event.target);so that you are changing the scaleX of Loader instead of Loader content just like my example
Unreality
so does it work?
Unreality
Unfortunately, no.
mofle
+1  A: 

The this in your completeHandler is not the document. Try just someMC.scaleX, and assuming you declared it on your main timeline or wherever, it should have access to it. The scaleX property also only ranges from 0 to 1, so if your Slider is from 10:500 -- that's not going to work. I put * 100 in there for you, but do whatever math is required to fix the scale to what you were intending.

Also, you need to explicitly set the Slider's value in the handler, rather then just changing the var. Variables aren't passed like that in AS3.

function completeHandler(event:Event):void{
         if (something) {
                  var someVariable:Number = someMC.scaleX;
                  s.value = someVariable * 100;
         }
}   


var s:Slider = new Slider();
s.maximum = 500;
s.minimum = 10;
UltimateBrent