PDA

View Full Version : (AS3)value from one function to another in same class



guitood
May 3rd, 2010, 12:55 PM
Hello, how do I get the value M (received from another class through the setter. I know I get it because the trace gives me the value) to the MovieLoader function. The answers probably in front of my nose, but I just can't see it.

package {

import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;

public class MovieLoader extends MovieClip {

public function MovieLoader() {
var M:String;
var imageReq:URLRequest=new URLRequest(M+".swf");
var imageLoader:Loader = new Loader();
imageLoader.contentLoaderInfo.addEventListener(Eve nt.COMPLETE, loadHandler);
imageLoader.load(imageReq);
}

public static function set scenePlay(M:String):void {
trace(M);
}

public function loadHandler(event:Event) {
var scene:MovieClip=event.target.content;
addChild(scene);
scene.stop();
}
}
}

Thanks for any help!!!

TOdorus
May 5th, 2010, 08:55 AM
You're looking for instance variables. You're only using local variables now. Local variables are temporary variables which get discarded when the function has been executed. Instance variables get discarded when the object gets discarded. If a instance variable is internal or public you can acces it from other classes as well. This is how properties work.



class SomeClass {
public var instanceVariable:int = 1;

public function SomeClass():void {
var localVariable:int = 2;

trace(instanceVariable); //traces 1
trace(localVariable); //traces 2
}
public function test():void {
trace(instanceVariable); //traces 1
trace(localVariable); //throws error as the variable isn't defined
}
}




class entryPoint extends Sprite {

public function SomeClass():void {
var localObject:SomeClass = new SomeClass(); //object in local variable, thus discarded when the constructer ends
trace("property "+ localObject.instanceVariable); //traces 1
}
}


You can store a local variable in a instance variable (instancevariable = localvariable; ), that way the local variable will get discarded, but it's content will still be referenced by the instance variable, so the content will stay accessible.

You really need to find a tutorial or get a book that explains these quite fundamental AS and OOP concepts. I can recommend Essential Actionscript 3.0 if you're looking for a good book.