-
Accessing MovieClip on stage
Ok,
I have don't know how to do this...
I have created a movieclip (game_container_mc) in the main.as file and I want to add a child (enemy) to it from a different file (makeWave.as) how would I do this?
-
You need a reference to game_container_mc in order to call its addChild method.
Is makeWave a class? How do you get an instance of it? Give that instance of makeWave a reference to game_container_mc.
Post some code.
-
main.as
Actionscript Code:
package { import flash.display.Sprite; import flash.events.Event; import flash.events.KeyboardEvent; import flash.events.MouseEvent; import flash.geom.Point; import flash.ui.Mouse; public class main extends Sprite { private var game_container:game_container_mc; var lvl:Number = 10; public function main() { game_container=new game_container_mc(); addChild(game_container); makeWave(lvl); } } }
makeWave.as
Actionscript Code:
package { public function makeWave(noMonster):void { var game_container = MovieClip(game_container_mc); for(var i:Number = 0; i < noMonster; i++) { var enemy:monster_mc = new monster_mc(); game_container.addChild(enemy); } } }
What I need is to know how to access "game_container" from makeWave.as
I only gave you what I think is needed, if you need more ask me.
-
PHP Code:
game_container=new game_container_mc(); addChild(game_container); makeWave(lvl, game_container);
PHP Code:
package { public function makeWave(noMonster, game_container):void { for(var i:Number = 0; i < noMonster; i++) { var enemy:monster_mc = new monster_mc(); game_container.addChild(enemy); } } }
-
Since makeWave is just a function rather than a class, you should just pass it in as a parameter.
Code:
makeWave(lvl, game_container);
Code:
package {
public function makeWave(noMonster:int, game_container:DisplayObjectContainer):void {
for(var i:int = 0; i < noMonster; i++) {
var enemy:monster_mc = new monster_mc();
game_container.addChild(enemy);
}
}
}
Why would you put makeWave in a package level function rather than make it a method of main?
BTW, classes usually start with a capital letter, by convention.
-
This works thanks. How would I make it a method of the main?
-
Simply put the function in the main class. If you don't need to call it from anywhere else, it can be private. If you do, public.
Code:
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.events.MouseEvent;
import flash.geom.Point;
import flash.ui.Mouse;
public class main extends Sprite {
private var game_container:game_container_mc;
public var lvl:int = 10;
public function main() {
game_container=new game_container_mc();
addChild(game_container);
makeWave(lvl);
}
private function makeWave(noMonster:int):void {
for(var i:int = 0; i < noMonster; i++) {
var enemy:monster_mc = new monster_mc();
game_container.addChild(enemy);
}
}
}
}
-
Thanks this helps me so much!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|