PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
problem with add/remove child?
A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: problem with add/remove child?

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    9

    problem with add/remove child?

    First off, I have never had any instruction in flash programming. So anything I know has been the result of trial, error, tutorials, or helpful forums. So the problem I have may be due to the fact that I really don't get how flash works.

    I have a very. VERY basic game; where the user controls a square and tries to dodge randomly appearing blocks that go from left to right. But I'm having problems removing the blocks when they leave the screen.

    I have two classes: "Obstacle", which is basically the blocks, and "Maker", which creates them randomly. I also have some coding on the main timeline (sorry, I know it's bad coding to put it there).
    PHP Code:
    package{
            
    import flash.utils.Timer;   
            
    import flash.events.TimerEvent;  
            
    import flash.display.MovieClip;
            public class 
    Obstacle extends MovieClip
                
    {
                public var 
    xspeed:int;
                private var 
    timer:Timer;

                public function 
    Obstacle($x:int, $$x:int, $$y:int)
                    {
                    
    xspeed $x;
                    
    this.= $$y;
                    
    this.= $$x;
                    
    timer = new Timer(25);
                    
    timer.addEventListener(TimerEvent.TIMERupdate);
                    
    timer.start();
                    }
                private function 
    update(Event)
                    {
                     
    this.+= xspeed;
                    }
                }
    }

    package{
            
    import flash.utils.Timer;   
            
    import flash.events.TimerEvent;  
            
    import flash.display.MovieClip;
            public class 
    RandMaker extends MovieClip
                
    {
                public var 
    xspeed:int;
                public var 
    X; public var Y;
                private var 
    timer:Timer;
                public var 
    enemies:Array = new Array();

                public function 
    RandMaker($x:int)
                    {
                    
    xspeed $x;
                    
    600;
                    
    timer = new Timer(100);
                    
    timer.addEventListener(TimerEvent.TIMERupdate);
                    
    timer.start();
                    var 
    nOb = new Obstacle(xspeedXY);
                    
    enemies.push(nOb);
                    }            
                private function 
    update(Event)
                    {
                        var 
    rand:int Math.round(Math.random() * 1000);
                        if(
    rand <990)
                        {
                            
    Math.round(Math.random() * 450);
                         var 
    nOb = new Obstacle(xspeedXY);
                        
    enemies.push(nOb);
                        }
                    }
                }

    PHP Code:
    stop();
    var 
    timer = new Timer(500);
    timer.addEventListener(TimerEvent.TIMERgo);
    var 
    world = new RandMaker(-6);
    var 
    index:int 0;
    var 
    destroyed:int 0;
    var 
    started:Boolean false
    var blocks:Array = new Array();
    timer.start();
    function 
    go(e:Event)
    {
        var 
    w:int 0;
        while(
    4)
        {
        
    addChild(world.enemies[index]);
        
    index++;
        
    w++;
        }
        var 
    q:int 0;
            while(
    world.enemies.length 1)
                {
                    if(
    world.enemies[q].0)
                    {
                        
    removeChild(world.enemies[q]); 
                        
    trace(world.enemies.shift());
                        
    destroyed++;
                        
    -= 1;
                    }
                    
    q++;
                }
    }

    stage.addEventListener(KeyboardEvent.KEY_DOWNkeydown);
    stage.addEventListener(KeyboardEvent.KEY_UPkeyup);
    stage.addEventListener(Event.ENTER_FRAMEmovement);
    var 
    Right false; var Left false; var Up false; var Down false;
    function 
    keydown(event:KeyboardEvent) {
            if (
    event.keyCode==39) {Right=true;}
            if (
    event.keyCode==38) {Up=true;}
            if (
    event.keyCode==37) {Left=true;}
            if (
    event.keyCode==40) {Down=true;}
    }
    function 
    keyup(event:KeyboardEvent) {
            if (
    event.keyCode==39) {event.keyCode=0;Right=false;}
            if (
    event.keyCode==38) {event.keyCode=0;Up=false;}
            if (
    event.keyCode==37) {event.keyCode=0;Left=false;}
            if (
    event.keyCode==40) {event.keyCode=0;Down=false;}
        
    }
    function 
    movement(e:Event)
    {
        if(
    Up == true){dude.-= 5;}
        if(
    Down == true){dude.+= 5;}
        if(
    Left == true){dude.-= 3;}
        if(
    Right == true){dude.+= 3;}
        
    }
    addEventListener(Event.ENTER_FRAMEhit);
    function 
    hit(e:Event)
    {
            var 
    0;
            while(
    world.enemies.length)
            {
            if(
    dude.hitTestObject(world.enemies[b])){removeEventListener(Event.ENTER_FRAMEmovement);}
            
    b++;
            }


    When I run the code, I get the following in the output:
    [object Obstacle]
    [object Obstacle]
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    [object Obstacle]
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    [object Obstacle]
    [object Obstacle]
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    TypeError: Error #2007: Parameter child must be non-null.
    at flash.display::DisplayObjectContainer/addChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
    at flash.display::DisplayObjectContainer/removeChild()
    at side_fla::MainTimeline/go()
    at flash.utils::Timer/_timerDispatch()
    at flash.utils::Timer/tick()

    Any help getting rid of these errors, or just telling me why I get them, would be much appreciated. Thank you.

    ps: I attatched the .fla, if it helps.
    Attached Files Attached Files
    Last edited by 5TonsOfFlax; 05-27-2010 at 01:21 PM. Reason: formatting

  2. #2
    Junior Member
    Join Date
    May 2010
    Posts
    9
    edit:
    Last edited by dragdrag11; 05-27-2010 at 01:48 PM. Reason: Nevermind

  3. #3
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I reformatted your post to make it easier to read.

    Your code assumes that world.enemies has at least 4 entries every frame. There's no guarantee of that, and your error is caused because it doesn't have enough. Also, there is a big bug in the following code:
    Code:
            while(q < world.enemies.length - 1)
                {
                    if(world.enemies[q].x < 0)
                    {
                        removeChild(world.enemies[q]);
                        trace(world.enemies.shift());
                        destroyed++;
                        q -= 1;
                    }
                    q++;
                }
    You are removing world.enemies[q], but shifting out world.enemies[0] (shift always removes the first element). This means world.enemies[q] (which is now world.enemies[q-1] ) is still in the array, and will trigger another erroneous shift on the next time go executes because it is still left of 0.
    Additionally, since you never put a RandMaker on the stage, there's no need for it to be a DisplayObject, much less a MovieClip.

  4. #4
    Junior Member
    Join Date
    May 2010
    Posts
    9
    ahh. Much appreciated. i wasn't sure if removeChild would remove the element from the array or not, which is why the shift is in there. Also, the go method should be called twice a second, while the array increases every 0.1 seconds? Maybe? I wasn't sure.

    Ane as for the randmaker... thanks, but flash rejects my classes whenever they don't extend a base class, so I just used the one that worked.

    Anyway, thanks A Lot for the help.

  5. #5
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    removeChild does NOT remove the object from the array. You do need to do that manually, but you need to remove the correct object. Use the array's splice method to remove the correct element.

    What do you mean flash rejects your classes? What error? If you have RandMaker associated with a symbol, yes you need to make it some sort of DisplayObject. But there's no need to associate it with a symbol since it never needs to go on the stage.

  6. #6
    Junior Member
    Join Date
    May 2010
    Posts
    9
    i'm not sure if it's specifically rejecting my classes, but it freezes up whenever I try to run a class that doesn't extend a base class. I have not tried extending a class that extends a base class though.

  7. #7
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    If by "run a class", you mean make it the document class, then yes you need that to extend a DisplayObject such as Shape, Sprite, or MovieClip. If you don't mean that, I don't know what you mean by "run a class".

  8. #8
    Junior Member
    Join Date
    May 2010
    Posts
    9
    if i try and use a class that does not extend a base class, it freezes up. for example, i had a class that calculated the average of three numbers. I did not extend any base class, just gave it a constructor and one function. when I created an instance of the class, flash froze up. I didn't even call the function, because i still can't figure out how to call functions correctly... but thats a different story.

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Sounds like your installation of Flash has become corrupted somehow. This should be completely legal:
    Code:
    package {
    
      public class Averager {
        private var total:Number;
        private var numItems:int;
    
        public function Averager(){
          total = 0;
          numItems = 0;
        }
    
        public function addItem(n:Number):void{
          total += n;
          numItems++;
        }
    
        public function getAverage():Number{
          return total/numItems;
        }
    
      }
    }
    And you could use that in frame script or any other class like this:
    Code:
    var avgr:Averager = new Averager();
    
    avgr.addItem(1);
    avgr.addItem(2);
    avgr.addItem(3);
    avgr.addItem(4);
    avgr.addItem(5);
    
    trace(avgr.getAverage()); //3
    Edit: incidentally, if you do not specify what class your new class extends, it extends Object. So all classes have a base class, even if you don't explicitly set it.

  10. #10
    Junior Member
    Join Date
    May 2010
    Posts
    9
    huh. Funny, flash won't let me call any functions unless i call them inside the class. The only function i've been able to use was a private function, called by a timer that was instantiated inside the constructor.
    Actionscript Code:
    public function Randmaker(int a)
    {
    xspeed = a;
    var t:Timer = new Timer(100);
    t.addEventListener(*********, update); /*i'm writing this from memory and i forgot exactly what the code was... but it called "update" every time the timer "ticked".*/
    t.start();

    }
    private function update(e:Event)
    {
    this.x += xspeed;
    }

    I can call functiond from pre-existing classes, such as Array and Timer just fine. But no custom class functions.


    and maybe flash is corrupted? i'm using the one on my school computers and it doesn't seem to work on any of the 8 or 9 i've tried.

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