A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: Accessing MovieClip on stage

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    13

    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?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  3. #3
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    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.

  4. #4
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    PHP Code:
    game_container=new game_container_mc();
    addChild(game_container);
    makeWave(lvlgame_container); 
    PHP Code:
    package {
        public function 
    makeWave(noMonstergame_container):void {
            for(var 
    i:Number 0noMonsteri++) {
                var 
    enemy:monster_mc = new monster_mc();
                
    game_container.addChild(enemy); 
            }
        }


  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  6. #6
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    This works thanks. How would I make it a method of the main?

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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); 
              }
            }
    
    
        }
    }

  8. #8
    Junior Member
    Join Date
    Feb 2010
    Posts
    13
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center