<?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: New Touch system issues</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/555</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:28:32 +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/555" rel="self" type="application/rss+xml" />

		<item>
			<title>remover on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-25991</link>
			<pubDate>Mon, 08 Feb 2010 18:52:20 +0000</pubDate>
			<dc:creator>remover</dc:creator>
			<guid isPermaLink="false">25991@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>could somebody please tell me why this bit of code identifies the correct rectangle (the origin is a minus number!?)</p>
<p>- (CGRect) rect {// rectangle that contains the sides coords of each sprite.<br />
	float s = self.spriteSize;<br />
	return CGRectMake(-s / 2, -s / 2, s, s);<br />
}</p>
<p>surely we want the origin to be (0,0)??
</p></description>
		</item>
		<item>
			<title>dnsolo on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3549</link>
			<pubDate>Sat, 11 Jul 2009 08:48:03 +0000</pubDate>
			<dc:creator>dnsolo</dc:creator>
			<guid isPermaLink="false">3549@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi Justin,</p>
<p>That's very helpful, many thanks.</p>
<p>I will implement this code and post back my results.</p>
<p>cheers,</p>
<p>dan
</p></description>
		</item>
		<item>
			<title>shivaz on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3528</link>
			<pubDate>Sat, 11 Jul 2009 04:43:52 +0000</pubDate>
			<dc:creator>shivaz</dc:creator>
			<guid isPermaLink="false">3528@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/80'>dnsolo</a>: If you grab the demo from this post: <a href="http://www.cocos2d-iphone.org/forum/topic/364" rel="nofollow">http://www.cocos2d-iphone.org/forum/topic/364</a></p>
<p>You can change it in the following ways - so that it uses the new touch system.<br />
In the MotionLayer.m - delete all the touch methods.<br />
In the MySprite.h change the interface to the following:</p>
<pre><code>typedef enum tagSpriteState {
	kSpriteStateGrabbed,
	kSpriteStateUngrabbed
} SpriteState;

@interface MySprite : AtlasSprite &#60;TargetedTouchDelegate&#62;
{
	bool ready;
	BOOL canTrack; //tell us if a Sprite in the Array can be tracked.
	float spriteSize;
	CGPoint touchStartPosition;
	@private SpriteState state;
}</code></pre>
<p>Then in MySprite.m do the following:<br />
Replace rect method with:</p>
<pre><code>- (CGRect) rect {// rectangle that contains the sides coords of each sprite.
	float s = self.spriteSize;
	return CGRectMake(-s / 2, -s / 2, s, s);
}</code></pre>
<p>Then add the following methods:<br />
<pre><code>- (void)onEnter
{
	[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
	[super onEnter];
}

- (void)onExit
{
	[[TouchDispatcher sharedDispatcher] removeDelegate:self];
	[super onExit];
}	

- (BOOL)containsTouchLocation:(UITouch *)touch
{
	return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
	if (state != kSpriteStateUngrabbed) return NO;
	if ( ![self containsTouchLocation:touch] ) return NO;
	state = kSpriteStateGrabbed;
	return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
	NSAssert(state == kSpriteStateGrabbed, @&#34;Unexpected state!&#34;);	

	CGPoint touchPoint = [touch locationInView:[touch view]];
	touchPoint = [[Director sharedDirector] convertCoordinate:touchPoint];

	self.position = CGPointMake(touchPoint.x, self.position.y);
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
	NSAssert(state == kSpriteStateGrabbed, @&#34;Unexpected state!&#34;);	

	state = kSpriteStateUngrabbed;
}</code></pre>
<p>That should do it. All basically straight from the TouchDemo. Only difference is that I use a uniform size for the sprite - spriteSize - which is set when the sprite is created. You could be less general in your rect function.</p>
<p>Justin
</p></description>
		</item>
		<item>
			<title>riq on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3483</link>
			<pubDate>Fri, 10 Jul 2009 17:20:40 +0000</pubDate>
			<dc:creator>riq</dc:creator>
			<guid isPermaLink="false">3483@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>@<a href='http://www.cocos2d-iphone.org/forum/profile/80'>dnsolo</a>: see cocos2d/menu.m to see how it detects if a touch is inside a menuitem.</p>
<p>also, tests/TouchDemo uses a similar approach.
</p></description>
		</item>
		<item>
			<title>dnsolo on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3479</link>
			<pubDate>Fri, 10 Jul 2009 16:22:30 +0000</pubDate>
			<dc:creator>dnsolo</dc:creator>
			<guid isPermaLink="false">3479@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi Riq,</p>
<p>I've tried this approach and can now successfully receive touches, thanks.</p>
<p>But how can I detect when a certain sprite is touched using the new touch system and avoid<br />
the messy way I used to do this as shown in my previous post?</p>
<p>- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event<br />
{<br />
	// How can I detect which sprite was touched?<br />
	return YES;<br />
}</p>
<p>Thanks,</p>
<p>Dan
</p></description>
		</item>
		<item>
			<title>riq on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3463</link>
			<pubDate>Fri, 10 Jul 2009 14:49:20 +0000</pubDate>
			<dc:creator>riq</dc:creator>
			<guid isPermaLink="false">3463@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>The easiest way to use the new touch system is to create a subclass of <code>Layer</code> (like in the old touch system) and add this method:<br />
<pre><code>-(void) registerWithTouchDispatcher
     {
        [[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES];
     }</code></pre>
<p>Now, instead of receiving multiple touches, you will receive just 1 touch.</p>
<p>See cocos2d/menu.m to see how it handle the touches by overriding <code>registerWithTouchDispatcher</code>
</p></description>
		</item>
		<item>
			<title>dnsolo on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3456</link>
			<pubDate>Fri, 10 Jul 2009 13:34:43 +0000</pubDate>
			<dc:creator>dnsolo</dc:creator>
			<guid isPermaLink="false">3456@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi all,</p>
<p>I've literally tried everything advised but cannot get the new Touch system to work.</p>
<p>I have decided to use the 'old' touch system as follows:</p>
<p>- (BOOL)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {<br />
    UITouch * touch = [touches anyObject];<br />
    CGPoint location = [[Director sharedDirector] convertCoordinate: [touch locationInView:touch.view]];<br />
    CGRect spriteRect = CGRectMake(mySprite1.position.x, mySprite1.position.y,<br />
                                         mySprite1.contentSize.width, mySprite1.contentSize.height);</p>
<p>    if(CGRectContainsPoint(mySprite1, location)) {<br />
		// do stuff<br />
        return kEventHandled;<br />
    }<br />
	return kEventIgnored;<br />
}</p>
<p>This code works fine and will enable me to finish my game.  It's not ideal though and I really wanted to use<br />
the new Touch system which I know would make things much easier to manage.</p>
<p>I think what is needed here is another version of the Pong demo, one that uses 'AtlasSprite' for the 'Paddle' instead of a 'TextureNode' ... and one where several sprites are placed into an NSArray and where each sprite can receive touch events using<br />
the new TouchDispatcher system.</p>
<p>cheers,</p>
<p>dan
</p></description>
		</item>
		<item>
			<title>emalamisura on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3350</link>
			<pubDate>Thu, 09 Jul 2009 16:16:16 +0000</pubDate>
			<dc:creator>emalamisura</dc:creator>
			<guid isPermaLink="false">3350@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>don't know what to tell you, everything mentioned should solve the issue so now its up to you to find it...
</p></description>
		</item>
		<item>
			<title>dnsolo on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3347</link>
			<pubDate>Thu, 09 Jul 2009 15:42:14 +0000</pubDate>
			<dc:creator>dnsolo</dc:creator>
			<guid isPermaLink="false">3347@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi,</p>
<p>I've done everything suggested but still no joy.</p>
<p>I've based my own code on the Pong demo supplied in v0.8 and so there cannot be too much wrong with it.</p>
<p>To recap, I've done all of the following:</p>
<p>// Added in my AppDelegate class<br />
[[TouchDispatcher sharedDispatcher] setDispatchEvents: YES]; </p>
<p>// Implemented the correct method in my main scene and layer class<br />
- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event<br />
{<br />
	if(mySprite1.state == kMenuOptionStateGrabbed) {<br />
		NSLog(@"SPRITE TOUCHED");<br />
		return kEventHandled;<br />
	}<br />
	return kEventIgnored;<br />
}</p>
<p>// In my class that sub classes TextureNode (like the Pong demo does) the relevant handling methods are:</p>
<p>- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event<br />
{<br />
	state = kMenuSpriteStateGrabbed;<br />
	return kEventIgnored;<br />
}</p>
<p>- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event<br />
{<br />
	state = kMenuSpriteStateUngrabbed;<br />
}</p>
<p>Additional notes...</p>
<p>I've debugged the app and the 'onEnter' method is definitely being called before each sprite is added to the layer.<br />
My implementation for this method is:</p>
<p>- (void)onEnter<br />
{<br />
	[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:NO];<br />
	[super onEnter];<br />
}</p>
<p>Note, it doesn't matter if 'swallowsTouches' is set to YES or NO in this method, it still doesn't work.</p>
<p>Dan
</p></description>
		</item>
		<item>
			<title>jd on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3340</link>
			<pubDate>Thu, 09 Jul 2009 14:27:43 +0000</pubDate>
			<dc:creator>jd</dc:creator>
			<guid isPermaLink="false">3340@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>You still have enable touches on each layer too, right?
</p></description>
		</item>
		<item>
			<title>emalamisura on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3338</link>
			<pubDate>Thu, 09 Jul 2009 14:19:45 +0000</pubDate>
			<dc:creator>emalamisura</dc:creator>
			<guid isPermaLink="false">3338@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>The other issues I ran into was that I had multiple layers and one of my upper layers was return kEventHandled which prevent further delegation, or one of my upper layers was swallowingTouches...</p>
<p>In addition make sure you are using the proper method names for the TargetedTouchDelegate, they do not end with 'es' so it would be ccTouchBegan versus the untargeted touch delegate (forget exact name), which end with 'es' such as ccTouchesBegan...</p>
<p>Also make sure in your ApplicationDelegate (entry point) that you are enabling touches for the entire application...</p>
<p>That's all I can think of atm...Hope one of those helps!
</p></description>
		</item>
		<item>
			<title>dnsolo on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3335</link>
			<pubDate>Thu, 09 Jul 2009 13:58:25 +0000</pubDate>
			<dc:creator>dnsolo</dc:creator>
			<guid isPermaLink="false">3335@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi,</p>
<p>Yes, I'm doing both those things and it's still not detecting any touches.</p>
<p>Any other ideas?</p>
<p>Thanks,</p>
<p>Dan
</p></description>
		</item>
		<item>
			<title>emalamisura on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3329</link>
			<pubDate>Thu, 09 Jul 2009 13:28:37 +0000</pubDate>
			<dc:creator>emalamisura</dc:creator>
			<guid isPermaLink="false">3329@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>You need to register the sprite for a targeted touch:</p>
<p>[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];</p>
<p>Make sure you subscribe to the Touch Delegate protocol as well...
</p></description>
		</item>
		<item>
			<title>dnsolo on "New Touch system issues"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/555#post-3319</link>
			<pubDate>Thu, 09 Jul 2009 11:28:31 +0000</pubDate>
			<dc:creator>dnsolo</dc:creator>
			<guid isPermaLink="false">3319@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hi all,</p>
<p>I'm having problems detecting sprite touches using the new TouchDispatcher system.</p>
<p>The code below doesn't work and no touch is detected, am I doing something wrong here?</p>
<p>[self schedule:@selector(tick:)];</p>
<p>- (void)tick:(ccTime)delta<br />
{<br />
	[self touchHandler];<br />
}</p>
<p>- (BOOL)touchHandler {<br />
	if(mySprite.state == kMenuOptionStateGrabbed) {<br />
	  NSLog(@"Sprite has been Touched");<br />
    }<br />
    return kEventHandled;<br />
}</p>
<p>In my ccTouchBegan method code I have...</p>
<p>- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event<br />
{<br />
	state = kMenuOptionStateGrabbed;<br />
	return kEventHandled;<br />
}</p>
<p>Thanks,</p>
<p>Dan
</p></description>
		</item>

	</channel>
</rss>

