Hello All,
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.
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.
I'd appreciate any directions.
Here's my class:
//
// Dolphin.h
// CCDolphinsGame
//
// Created by freelancer on 2/27/10.
// Copyright 2010 Apple Inc. All rights reserved.
//
#import "cocos2d.h"
#import "Ball.h"
typedef enum tagDolphinState {
kDolphinStateGrabbed,
kDolphinStateUngrabbed
} DolphinState;
@interface Dolphin : CCSprite <CCTargetedTouchDelegate>
{
@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.
//
#import "Dolphin.h"
#import "cocos2d.h"
#import "Ball.h"
#import "SimpleAudioEngine.h"
#import "CocosDenshion.h"
#import "CDAudioManager.h"
@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:@"dolphin3.plist"];
animation = [[CCAnimation alloc] initWithName:@"dolphin" delay:1/15.0];
for (int i=1; i<20; ++i)
{
// frame name format: blob_idle_01.png .. blob_idle_24.png
//dolphin0001
NSString * fname = [NSString stringWithFormat:@"dolphin00%02i.png", i];
//NSLog(@"%@",fname);
[animation addFrame:[cache spriteFrameByName: fname]];
}
action = [CCRepeatForever actionWithAction:[CCAnimate actionWithAnimation:animation]];
//[self addAnimation: animation];
SimpleAudioEngine *sae = [SimpleAudioEngine sharedEngine];
if (sae != nil) {
[sae preloadBackgroundMusic:@"dolphin.caf"];
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 > 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 < 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(@"self.rect: %@", NSStringFromCGRect(self.rect));
return YES;
}
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
// If it weren'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'd need to loop through the set
// in each touchXXX method.
NSAssert(state == kDolphinStateGrabbed, @"Dolphin - Unexpected state!");
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, @"Dolphin - Unexpected state!");
//[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"mario-theme.mp3"];
//[[SimpleAudioEngine sharedEngine] stopBackgroundMusic];
state = kDolphinStateUngrabbed;
self.velocity = CGPointMake(0, 100.0f);
[[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"dolphin.caf"];
//[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