<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="bbPress/1.1" -->
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>cocos2d for iPhone &#187; Topic: Check if child exists?</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/4033</link>
		<description>A fast, easy to use, free, and community supported 2D game engine</description>
		<language>en-US</language>
		<pubDate>Fri, 10 Feb 2012 01:53:59 +0000</pubDate>
		<generator>http://bbpress.org/?v=1.1</generator>
		<textInput>
			<title><![CDATA[Search]]></title>
			<description><![CDATA[Search all topics from these forums.]]></description>
			<name>q</name>
			<link>http://www.cocos2d-iphone.org/forum/search.php</link>
		</textInput>
		<atom:link href="http://www.cocos2d-iphone.org/forum/rss/topic/4033" rel="self" type="application/rss+xml" />

		<item>
			<title>Tone on "Check if child exists?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4033#post-139391</link>
			<pubDate>Fri, 13 Jan 2012 16:06:01 +0000</pubDate>
			<dc:creator>Tone</dc:creator>
			<guid isPermaLink="false">139391@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>You can check if the parent of the node is nil</p>
<pre><code>if(this.parent == nil) {
    // this is not added to any other node
}</code></pre></description>
		</item>
		<item>
			<title>IronMike on "Check if child exists?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4033#post-66701</link>
			<pubDate>Sun, 12 Dec 2010 08:59:36 +0000</pubDate>
			<dc:creator>IronMike</dc:creator>
			<guid isPermaLink="false">66701@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>If you use a tag on the child when you create it, you can then access that child by that tag. You would have to know the tag for each child. This may not be practical for bullets but without seeing code it's hard to say.</p>
<p><code>[self addChild:mySprite z:1 tag:kTagBullet1]</code></p>
<p>Then you might call</p>
<p><code>CCSprite *Bullet = (CCSprite*) [self getChildByTag:kTagBullet1];</code>
</p></description>
		</item>
		<item>
			<title>michaelm on "Check if child exists?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4033#post-24219</link>
			<pubDate>Mon, 25 Jan 2010 04:07:42 +0000</pubDate>
			<dc:creator>michaelm</dc:creator>
			<guid isPermaLink="false">24219@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi javawizkid I was gone all day but here goes.  This should solve both of your posts.<br />
Subclass CCSprite to make a bullet class add a boolean<br />
@interface Bullet:CCSprite<br />
{<br />
 bool active;<br />
}<br />
@property bool active;<br />
synthesize the property in implementation</p>
<p>then when you check if out of bounds pseudocode<br />
 for(int i = 0; i &#60; 10; i++)<br />
{</p>
<p>if(((CCSprite *)[bulletsBuilder objectAtIndex:i]).position.x &#62; 490)<br />
{<br />
// not this<br />
//[self removeChild:((CCSprite *)[bulletsBuilder objectAtIndex:i]) cleanup:YES];<br />
bulletNumber--;<br />
// do this<br />
((CCSprite *)[bulletsBuilder objectAtIndex:i]).active=false;<br />
((CCSprite *)[bulletsBuilder objectAtIndex:i]).visible = false;<br />
}<br />
}</p>
<p>Then when you need a new bullet loop through the array and look for the first that is !active and reset that one to the position you want set visible to true and your set.</p>
<p>This removes your problem with removing from an array in the loop in the other post to.
</p></description>
		</item>
		<item>
			<title>JavaWizKid on "Check if child exists?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4033#post-24163</link>
			<pubDate>Sun, 24 Jan 2010 17:43:38 +0000</pubDate>
			<dc:creator>JavaWizKid</dc:creator>
			<guid isPermaLink="false">24163@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Can you give me some sample code because I'm struggling, thanks.
</p></description>
		</item>
		<item>
			<title>michaelm on "Check if child exists?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4033#post-24158</link>
			<pubDate>Sun, 24 Jan 2010 17:03:05 +0000</pubDate>
			<dc:creator>michaelm</dc:creator>
			<guid isPermaLink="false">24158@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Easier to just create a variable I use "active" in the bullet class.  Then if inactive setVisible false and check if active for collisions.  If you need a bullet, loop through and grab the first not active one and reset.  Adding and removing  from an nsmutable array has cost, not a lot but this is faster and easier.
</p></description>
		</item>
		<item>
			<title>JavaWizKid on "Check if child exists?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4033#post-24150</link>
			<pubDate>Sun, 24 Jan 2010 15:57:44 +0000</pubDate>
			<dc:creator>JavaWizKid</dc:creator>
			<guid isPermaLink="false">24150@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Basically it's a shooting mechanism and I'm checking if the bullet is still on screen or if its off screen it will have been removed and when the gun i shot it is re added to the layer
</p></description>
		</item>
		<item>
			<title>MikeSz on "Check if child exists?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4033#post-24149</link>
			<pubDate>Sun, 24 Jan 2010 15:56:12 +0000</pubDate>
			<dc:creator>MikeSz</dc:creator>
			<guid isPermaLink="false">24149@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>well, you should just write your code in such a way that such a scenario will not occur ;)</p>
<p>but if you absolutely have to:</p>
<p>for(CCNode * node in children)</p>
<p>will look through all the children added to current node, just check if it's already there
</p></description>
		</item>
		<item>
			<title>JavaWizKid on "Check if child exists?"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4033#post-24148</link>
			<pubDate>Sun, 24 Jan 2010 15:52:59 +0000</pubDate>
			<dc:creator>JavaWizKid</dc:creator>
			<guid isPermaLink="false">24148@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>How can I check if a child is already on the layer before I add it?<br />
[self addChild: this];</p>
<p>Thanks.
</p></description>
		</item>

	</channel>
</rss>

