<?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: CCSpriteSheets and Targeted Touches [.99]</title>
		<link>http://www.cocos2d-iphone.org/forum/topic/4795</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:52:45 +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/4795" rel="self" type="application/rss+xml" />

		<item>
			<title>dotcomlabs on "CCSpriteSheets and Targeted Touches [.99]"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4795#post-59514</link>
			<pubDate>Sat, 16 Oct 2010 00:31:30 +0000</pubDate>
			<dc:creator>dotcomlabs</dc:creator>
			<guid isPermaLink="false">59514@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>I would love to know the best solution for this too!<br />
So the touch registers just at the png's frame, not the sheet. Strange, but I'm sure there's gotta be an easy solution.<br />
Thanks!
</p></description>
		</item>
		<item>
			<title>davewise on "CCSpriteSheets and Targeted Touches [.99]"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4795#post-50196</link>
			<pubDate>Thu, 12 Aug 2010 19:39:52 +0000</pubDate>
			<dc:creator>davewise</dc:creator>
			<guid isPermaLink="false">50196@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Was this issue resolved? It looks like the proposed solution was off. At least it is for me. I have the same issue. For me the problem is that [self convertTouchToNodeSpaceAR:touch] is returning a point relative to the entire png size as stated in the question. @<a href='http://www.cocos2d-iphone.org/forum/profile/7318'>ozhanlion</a> were you able to solve this problem?
</p></description>
		</item>
		<item>
			<title>Zaid Crouch on "CCSpriteSheets and Targeted Touches [.99]"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4795#post-29802</link>
			<pubDate>Fri, 12 Mar 2010 13:07:11 +0000</pubDate>
			<dc:creator>Zaid Crouch</dc:creator>
			<guid isPermaLink="false">29802@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hey man! First, thanks for your code, it rapidly sped up some of my own work. In return, I think I can help you with your problem.</p>
<p>You need to change your rect method from your current implementation:</p>
<pre><code>- (CGRect)rect
{
	CGSize s = [self.texture contentSize];
	return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}</code></pre>
<p>to:<br />
<pre><code>- (CGRect)rect
{
	CGSize s = self.textureRect.size;
	return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}</code></pre>
<p>As I understand it, your current method is using the size of the entire texture to determine it's rect. Using textureRect should allow it to work off the size of your current frame. Hope that helps!
</p></description>
		</item>
		<item>
			<title>ozhanlion on "CCSpriteSheets and Targeted Touches [.99]"</title>
			<link>http://www.cocos2d-iphone.org/forum/topic/4795#post-28539</link>
			<pubDate>Tue, 02 Mar 2010 17:38:40 +0000</pubDate>
			<dc:creator>ozhanlion</dc:creator>
			<guid isPermaLink="false">28539@http://www.cocos2d-iphone.org/forum/</guid>
			<description><p>Hello All,</p>
<p>I am subclassing CCSprite to create my class and passing a CCTexture2D on the init method. The object shows up on the stage. I also    use CCTargetedTouchDelegate to make sure it's conforming to touches in a targeted manner.  Still, all is fine.<br />
NOW: What I want to do is to animate this object when I get back the CCTouchEnded notification, so I create a CCAnimation which gets its files through a CCSpriteFrameCache which I already passed my plist  and png file. I add my action and the idea is to call the action when the touchesEnded gets called. Now all this is working fine except one problem.  The area that's registered for the touch extends from my object and actually it registers the whole png file that I exported from the Zwoptex. It's 1024x1024.  How can I make it so that it only looks for touches when it's on the actual object? Should I just do this old school way of checking the bounding box of my object? It shows 1024x1024.</p>
<p>I'd appreciate any directions.</p>
<p>Here's my class:</p>
<pre><code>//
//  Dolphin.h
//  CCDolphinsGame
//
//  Created by freelancer on 2/27/10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;cocos2d.h&#34;
#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;Ball.h&#34;

typedef enum tagDolphinState {
	kDolphinStateGrabbed,
	kDolphinStateUngrabbed
} DolphinState;

@interface Dolphin : CCSprite &#60;CCTargetedTouchDelegate&#62;
{
@private
	DolphinState state;
	CGPoint velocity;
	id action;
	CCAnimation *animation;
	float width;
}

@property(nonatomic, readonly) CGRect rect;
@property(nonatomic) CGPoint velocity;
@property(nonatomic, readonly) float width;
@property(nonatomic, readonly) float height;
@property(nonatomic, readonly) float x;
@property(nonatomic, readonly) float y;

+ (id)dolphinWithTexture:(CCTexture2D *)texture;

- (void)move:(ccTime)delta inRelationTo:(Ball*)ball;

@end

//
//  Dolphin.m
//  CCDolphinsGame
//
//  Created by freelancer on 2/27/10.
//  Copyright 2010 Apple Inc. All rights reserved.
//

#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;Dolphin.h&#34;
#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;cocos2d.h&#34;
#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;Ball.h&#34;

#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;SimpleAudioEngine.h&#34;
#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;CocosDenshion.h&#34;
#<a href='http://www.cocos2d-iphone.org/forum/tags/import'>import</a> &#34;CDAudioManager.h&#34;

@implementation Dolphin
@synthesize velocity;

+ (id)dolphinWithTexture:(CCTexture2D *)aTexture
{
	return [[[self alloc] initWithTexture:aTexture] autorelease];
}

- (id)initWithTexture:(CCTexture2D *)aTexture
{
	if ((self = [super initWithTexture:aTexture]) ) {

		//-(id) initWithSpriteSheet:(CCSpriteSheet*)spritesheet rect:(CGRect)rect

		state = kDolphinStateUngrabbed;

		CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache];
		[cache addSpriteFramesWithFile:@&#34;dolphin3.plist&#34;];

		animation = [[CCAnimation alloc] initWithName:@&#34;dolphin&#34; delay:1/15.0];
		for (int i=1; i&#60;20; ++i)
		{
			// frame name format: blob_idle_01.png .. blob_idle_24.png
			//dolphin0001
			NSString * fname = [NSString stringWithFormat:@&#34;dolphin00%02i.png&#34;, i];
			//NSLog(@&#34;%@&#34;,fname);
			[animation addFrame:[cache spriteFrameByName: fname]];
		}
		action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];

		//[self addAnimation: animation];

		SimpleAudioEngine *sae = [SimpleAudioEngine sharedEngine];
		if (sae != nil) {
			[sae preloadBackgroundMusic:@&#34;dolphin.caf&#34;];
			if (sae.willPlayBackgroundMusic) {
				sae.backgroundMusicVolume = 0.5f;
			}
		}
		[animation retain];
		[action retain];

	}

	return self;
}

- (void)move:(ccTime)delta inRelationTo:(Ball*)ball
{
	self.position = ccpAdd(ccpMult(self.velocity, delta), self.position );
	//[self runAction:action];

	if (self.position.y &#62; 768 - self.contentSize.height/2 - ball.height/2-3) {
		[self setPosition: ccp( self.position.x, 768 - self.contentSize.height/2 - ball.height/2-3)];
		//velocity.y *= -1;

		[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
		[self stopAction:action];
		//[self setDisplayFrame:animation.name index:0];

	} else if (self.position.y &#60; 0) {
		[self setPosition: ccp(self.position.x, self.contentSize.height/2)];
		//velocity.y *= -1;
	}

}

/*
-(void)draw{
	//  ccDrawLine( ccp(768, self.position.y), ccp(0, 200) );
}    

 */

- (void)onEnter
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
	[super onEnter];
}

- (void)onExit
{
	[[CCTouchDispatcher 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 != kDolphinStateUngrabbed) return NO;
	if ( ![self containsTouchLocation:touch] ) return NO;

	state = kDolphinStateGrabbed;
	[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
	[self stopAction:action];

	NSLog(@&#34;self.rect: %@&#34;, NSStringFromCGRect(self.rect));

	return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
	// If it weren&#39;t for the TouchDispatcher, you would need to keep a reference
	// to the touch from touchBegan and check that the current touch is the same
	// as that one.
	// Actually, it would be even more complicated since in the Cocos dispatcher
	// you get NSSets instead of 1 UITouch, so you&#39;d need to loop through the set
	// in each touchXXX method.

	NSAssert(state == kDolphinStateGrabbed, @&#34;Dolphin - Unexpected state!&#34;);	

	CGPoint touchPoint = [touch locationInView:[touch view]];
	touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
	self.velocity = CGPointMake(0, 0);
	self.position = CGPointMake(self.position.x, touchPoint.y);
}

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

	//[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@&#34;mario-theme.mp3&#34;];
	//[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];

	state = kDolphinStateUngrabbed;

	self.velocity = CGPointMake(0, 100.0f);

	[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@&#34;dolphin.caf&#34;];

	//[self stopAction:action];
	[self stopAllActions];
	[self runAction:action];
}

- (void) dealloc
{
	[animation release];
	[action release];
	[super dealloc];
}

- (CGRect)rect
{
	CGSize s = [self.texture contentSize];
	return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}

- (float)width
{
	CGSize s = [self.texture contentSize];
	return s.width;
}

- (float)height
{
	CGSize s = [self.texture contentSize];
	return s.height;
}

- (float)x
{
	return self.position.x;
}

- (float)y
{
	return self.position.y;
}

@end</code></pre></description>
		</item>

	</channel>
</rss>

