Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6822157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:37:48+00:00 2026-05-26T21:37:48+00:00

I’m trying to create a extended CCSprite class that can detect touches. I have

  • 0

I’m trying to create a extended CCSprite class that can detect touches. I have done some research and discovered an example at http://anny.fm/c/iphone/cocos2d/TouchableSprite/ which was created by Anny in this forum thread http://www.cocos2d-iphone.org/forum/topic/3196 (last post).

Using this I have setup my class like so:

Header

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface PianoKey : CCSprite <CCTargetedTouchDelegate> {

}

-(BOOL) tsTouchBegan:     (UITouch*)touch withEvent: (UIEvent*)event;
-(void) tsTouchMoved:     (UITouch*)touch withEvent: (UIEvent*)event;
-(void) tsTouchEnded:     (UITouch*)touch withEvent: (UIEvent*)event;
-(void) tsTouchCancelled: (UITouch*)touch withEvent: (UIEvent*)event;

@end

and implementation…

@implementation PianoKey

-(id)initWithKey:(int)key {
if((self = [super initWithFile:[NSString stringWithFormat:@"key_%i.png",key]])) {

}
return self;
}


 -(BOOL) tsTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog( @"tsTouchBegan"); 
return YES; 
}
 -(void) tsTouchMoved:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog( @"tsTouchMoved"); 

} 
-(void) tsTouchEnded:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog( @"tsTouchEnded");

}
-(void) tsTouchCancelled:(UITouch*)touch withEvent: (UIEvent*)event { 
NSLog( @"tsTouchCancelled");
}

//
// Utilities. Don't override these unless you really need to.
//

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

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

//
// The actual touch listener functions. Don't override these either.
//

-(BOOL) ccTouchBegan:(UITouch*)touch withEvent: (UIEvent*)event { 
    NSLog(@"attempting touch.");
    if([self didTouch: touch]) {
        return [self tsTouchBegan:touch withEvent: event]; 
    }
return NO; 
}
-(void) ccTouchMoved:     (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch])        [self tsTouchMoved:     touch withEvent: event]; }
-(void) ccTouchEnded:     (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch])        [self tsTouchEnded:     touch withEvent: event]; }
-(void) ccTouchCancelled: (UITouch*)touch withEvent: (UIEvent*)event { if([self didTouch: touch])        [self tsTouchCancelled: touch withEvent: event]; }

@end

I understand the methods above, such as detecting if the the touch was within the bounds of the sprite, however I’m stuck as to why I’m not getting any response when I click the key. I’m new to implementing CCTargetdTouchDelegate so I assume it might be something to do with that…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T21:37:49+00:00Added an answer on May 26, 2026 at 9:37 pm

    You don’t need the targeted touch delegate, it just splits the NSSet of touches into individual ccTouchXXX messages. Your implementation is simply lacking the registration and de-registration of the CCTargetedTouchHandler. This is usually done in onEnter, so that it works with any node type and not just CCLayer:

    -(void) onEnter
    {
        [super onEnter];
        [[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES];
    }
    
    -(void) dealloc
    {
        [[TouchDispatcher sharedDispatcher] removeDelegate:self];
        [super dealloc];
    }
    

    Btw, Kobold2D already has an extended CCNode class that can test if a touch was on a node (sprites, labels, etc) via this test:

    [node containsTouch:uiTouch];
    

    Just saying you wouldn’t have had to do all this work. 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create an if statement in PHP that prevents a single post
Basically, what I'm trying to create is a page of div tags, each has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.