Reputation: 331
In my main AS, I am using a MovieClip as a Container despite of the stage. In another AS file, I want to take the Container as a reference as well as addChild (such as bullets etc) to it, but I really don't know how to write the code.
If I only addChild in the current (sub)AS, it's working, but it's just a problem to removeChild.
Upvotes: 0
Views: 176
Reputation: 403
Don't know if I get it right but....
You have a container in your main class and you want to access it from another class, is that right?
I guess what you need is a Globals.as which would look something like this
Globals.as // you name it...
package {
public class Globals extends Object {
public static var YOUR_REFERENCE_VAR:MovieClip; // This is a static var
}
}
You need to assign YOUR_REFERENCE_VAR to your MovieClip in your main class. Then anywhere in your project you have access to that MovieClip.
package {
import Globals.as;
import flash.display.Sprite;
public class GameEngine extends Sprite {
public function GameEngine () {
addEventListener (Event.ADDED_TO_STAGE, _onAddedToStage);
}
private function _onAddedToStage (evt:Event):void {
removeEventListener (Event.ADDED_TO_STAGE, _onAddedToStage);
//When you to access that MovieClip you access it like this
Globals.YOUR_REFERENCE_VAR.alpha = 0.5;
}
}
}
Now you use addChild/removehild like this
var spr:Sprite = new Sprite();
Globals.YOUR_REFERENCE_VAR.addChild (spr);
//Remove the child
Globals.YOUR_REFERENCE_VAR.removeChild (spr);
I hope this helps.
Upvotes: 2
Reputation: 3548
You could pass your movie clip by reference. Here is a quick example :
package {
import flash.display.MovieClip;
import flash.display.Sprite;
public class Main extends Sprite {
public function Main() {
//create your movie clip
var movieClip:MovieClip = new MovieClip();
//if your MovieClip is in the Flash IDE library setup an export name and do
//var flasLibraryMovieClip : YourMovieClipExportName = new YourMovieClipExportName()
//add your movie clip to the display list
addChild(movieClip);
//instantiate your second class
//and pass the movie clip to the class constructor
var test : Test = new Test(movieClip);
}
}
}
package {
import flash.display.MovieClip;
import flash.display.Sprite;
public class Test extends Sprite {
public function Test(movieClip : MovieClip) {
//do stuff with the movie
}
}
}
Upvotes: 0