#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    7

    Problem with "swapping" instances


    Hi all,

    I'm making a little, silly game, but I have come across a problem that I just can't seem to figure out. So now I'm hoping the pros in here can help me out

    Here is my code that is giving me problems:
    Code:
    var soft****:MovieClip = new Soft****();
    addChild(soft****);
    
    soft****.addEventListener(MouseEvent.MOUSE_OVER, checkSize);
    
    function checkSize(Event:MouseEvent):void
    {
    	trace(cumBar_mc.height);
    	if(cumBar_mc.height == 50)
    	{
    		soft****.removeEventListener(MouseEvent.MOUSE_OVER, checkSize);
    		removeChild(soft****);
    		
    		var semi****:MovieClip = new Semi****();
    		addChild(semi****);
    		semi****.addEventListener(MouseEvent.MOUSE_OVER, checkSize);
    		
    	}
    And here is the error I get:
    Code:
    ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. 	at flash.display::DisplayObjectContainer/removeChild() 	at WankerAS3_03_fla::MainTimeline/checkSize()
    Any help is much appreciated
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    7
    If you need any more information, feel free to ask. I'm kinda at a loss here
  4. #3
  5. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,675
    Rep Power
    3164
    Hi, sorry for the delay, haven't been checking in recently. The error is saying that this line is causing the problem:

    removeChild(soft****);

    The only reason that would give you that error is if soft**** (I'll just refer to it as "soft") was no longer a child of that container. What is happening here is that you first add "soft" and setup the roll over event. Inside the function you then remove the event listener from "soft" and then remove "soft" as a child. Then you assign the event to "semi" and add that as a child. All good so far. Now you roll over "semi" and it goes through the same process, this time though "soft" is already not a child since you removed it last time, boom, error!

    A simple way around this is to check if "soft" is a child:

    if(cumBar_mc.height == 50 && this.contains(soft****))

    Also, you might want to change to using ROLL_OVER and ROLL_OUT instead of MOUSE_OVER and MOUSE_OUT. The difference is explained here. It is very very rare that I would use MOUSE_OVER/OUT.

    Comments on this post

    • stillwell agrees
    Quis custodiet ipsos custodes?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    7

    Smile


    Thank you so much, that was very well explained, and it fixed it right up.

    I actually think I should use MOUSE_OVER though, because when I tested it out, ROLL_OVER seemed to make it skip the checkSize function when moving the cursor fast(which is the point of the game ). But very good to know for the future, so thanks again!

    However, when I add the hard instance to the mix, I get a whole new error that I do not understand:
    Code:
    TypeError: Error #2007: Parameter child must be non-null.
    	at flash.display::DisplayObjectContainer/contains()
    	at WankerAS3_03_fla::MainTimeline/checkSize()
    I don't understand it, because I have done it exactly like the previous if statement, which now seems to work perfectly:
    Code:
    var soft****:MovieClip = new Soft****();
    addChild(soft****);
    
    soft****.addEventListener(MouseEvent.MOUSE_OVER, checkSize);
    
    function checkSize(Event:MouseEvent):void
    {
    	trace(cumBar_mc.height);
    	if(cumBar_mc.height == 50 && this.contains(soft****))
    	{
    		soft****.removeEventListener(MouseEvent.MOUSE_OVER, checkSize);
    		removeChild(soft****);
    		
    		var semi****:MovieClip = new Semi****();
    		addChild(semi****);
    		semi****.addEventListener(MouseEvent.MOUSE_OVER, checkSize);
    	}
    	
    	if(cumBar_mc.height == 150 && this.contains(semi****))
    	{
    		semi****.removeEventListener(MouseEvent.MOUSE_OVER, checkSize);
    		removeChild(semi****);
    		
    		var hard****:MovieClip = new Hard****();
    		addChild(hard****);
    		hard****.addEventListener(MouseEvent.MOUSE_OVER, checkSize);
    	}
    }
    Hope you can help me out again, it is much appreciated!
  8. #5
  9. No Profile Picture
    Gotta get to the next screen..
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2003
    Location
    Legion of Dynamic Discord
    Posts
    6,675
    Rep Power
    3164
    This time it is because you have created "semi" inside the function which means it only has function scope as that reference. That may be a bit hard to follow. So originally, you create the reference to "soft" outside the function. That means it will exist until you manually clear it. Later on you create "semi" but you do this inside the function. That means the reference to the "semi" clip only exists inside that function for that one run through. Once the function has been run the first time that reference no longer exists. A simple way around it is to create the reference outside the function:

    Code:
    var soft****:MovieClip = new Soft****();
    var semi:MovieClip;
    var hard:MovieClip;
    addChild(soft****);
    Then later on change:

    var semi****:MovieClip = new Semi****();

    to:

    semi**** = new Semi****();

    Do the same with the "hard" version.
    Quis custodiet ipsos custodes?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    60
    Rep Power
    7
    Thank you so much! Now it works, and I understand why Love that you explain why the problem occurs so well.

IMN logo majestic logo threadwatch logo seochat tools logo