Flash Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignFlash Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old June 7th, 2012, 06:46 PM
stillwell stillwell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 60 stillwell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old June 8th, 2012, 03:47 AM
stillwell stillwell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 60 stillwell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #3  
Old June 10th, 2012, 06:22 PM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 11 m 52 sec
Reputation Power: 3163
Facebook MySpace
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?

Reply With Quote
  #4  
Old June 11th, 2012, 03:44 AM
stillwell stillwell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 60 stillwell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 18 h 50 m 14 sec
Reputation Power: 6
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!

Reply With Quote
  #5  
Old June 11th, 2012, 04:41 AM
Tann San Tann San is offline
Gotta get to the next screen..
Dev Shed God 4th Plane (6500 - 6999 posts)
 
Join Date: Nov 2003
Location: Legion of Dynamic Discord
Posts: 6,663 Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)Tann San User rank is General 33rd Grade (Above 100000 Reputation Level)  Folding Points: 14767 Folding Title: Novice Folder
Time spent in forums: 1 Month 1 Week 3 Days 20 h 11 m 52 sec
Reputation Power: 3163
Facebook MySpace
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.

Reply With Quote
  #6  
Old June 11th, 2012, 06:34 AM
stillwell stillwell is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2008
Posts: 60 stillwell User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignFlash Help > ActionScript 3 - Problem with "swapping" instances

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap