The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Design
> Flash Help
|
ActionScript 3 - Problem with "swapping" instances
Discuss Problem with "swapping" instances in the Flash Help forum on Dev Shed. Problem with "swapping" instances Flash Help forum discussing all products originally created by Macromedia including DreamWeaver, Contribute, Flash, Fireworks, Freehand, Director, Authorware and HomeSite. Adobe bought Macromedia in 2005.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

June 7th, 2012, 06:46 PM
|
|
Contributing User
|
|
Join Date: Feb 2008
Posts: 60
Time spent in forums: 18 h 50 m 14 sec
Reputation Power: 6
|
|
|
ActionScript 3 - 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 
|

June 8th, 2012, 03:47 AM
|
|
Contributing User
|
|
Join Date: Feb 2008
Posts: 60
Time spent in forums: 18 h 50 m 14 sec
Reputation Power: 6
|
|
If you need any more information, feel free to ask. I'm kinda at a loss here 
|

June 10th, 2012, 06:22 PM
|
|
Gotta get to the next screen..
|
|
Join Date: Nov 2003
Location: Legion of Dynamic Discord
|
|
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.
__________________
Quis custodiet ipsos custodes?
|

June 11th, 2012, 03:44 AM
|
|
Contributing User
|
|
Join Date: Feb 2008
Posts: 60
Time spent in forums: 18 h 50 m 14 sec
Reputation Power: 6
|
|
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!
|

June 11th, 2012, 04:41 AM
|
|
Gotta get to the next screen..
|
|
Join Date: Nov 2003
Location: Legion of Dynamic Discord
|
|
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.
|

June 11th, 2012, 06:34 AM
|
|
Contributing User
|
|
Join Date: Feb 2008
Posts: 60
Time spent in forums: 18 h 50 m 14 sec
Reputation Power: 6
|
|
Thank you so much! Now it works, and I understand why  Love that you explain why the problem occurs so well.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|