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

  • Home
  • SEARCH
  • 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 3398728
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:41:05+00:00 2026-05-18T04:41:05+00:00

Hey all. I just started looking into the cocos2d library. I’ve heard that it’s

  • 0

Hey all. I just started looking into the cocos2d library. I’ve heard that it’s an easy library to get into if you’re used to programming in ActionScript and I’ve found that a lot of the concepts are indeed similar.

I started looking through sample projects (the sample games linked here were especially helpful) and I saw that handling of touches usually isn’t done in a CCSprite. Rather, the CCLayer that instantiates CCSprites reacts to a touch event and iterates through the sprites it created to detect which CCSprite was touched (if any).

I want CCSprites to handle whether they’ve been touched themselves, and call up to notify that it’s been touched (if needed). The Paddle class found under /tests/TouchesTest does just this – it handles touches by itself.

So, the question I have is: What’s considered best practice for this? Is it better to have touches handled in a central location and iterate through children to see what’s been touched? Or should each child handle its own touch events? Or does it not matter?

I’d prefer each child handling its own touch events but I’d like to follow best practices on this (if they exist). Thanks!

  • 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-18T04:41:05+00:00Added an answer on May 18, 2026 at 4:41 am

    I think it is a matter of preference but I like to have the sprite detect if it was touched by subclassing CCSprite. I make a getter method in my CCSprite subclass that retrieves the state variable from the subclass and then the main program can act accordingly.

    Here is an example header file for my CCSprite subclass “spuButton”:

        #import "cocos2d.h"
    
        typedef enum tagButtonState {
            kButtonStatePressed,
            kButtonStateNotPressed
        } ButtonState;
    
        typedef enum tagButtonStatus {
            kButtonStatusEnabled,
            kButtonStatusDisabled
        } ButtonStatus;
    
        @interface spuButton : CCSprite <CCTargetedTouchDelegate> {
        @private
            ButtonState state;
            CCTexture2D *buttonNormal;
            CCTexture2D *buttonLit;
            ButtonStatus buttonStatus;
    
        }
    
        @property(nonatomic, readonly) CGRect rect;
    
        + (id)spuButtonWithTexture:(CCTexture2D *)normalTexture;
    
        - (void)setNormalTexture:(CCTexture2D *)normalTexture;
        - (void)setLitTexture:(CCTexture2D *)litTexture;
        - (BOOL)isPressed;
        - (BOOL)isNotPressed;
    
        @end
    

    and here is an example of the .m file:

        #import "spuButton.h"
        #import "cocos2d.h"
    
        @implementation spuButton
    
        - (CGRect)rect
        {
            CGSize s = [self.texture contentSize];
            return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
        }
    
        + (id)spuButtonWithTexture:(CCTexture2D *)normalTexture
        {
            return [[[self alloc] initWithTexture:normalTexture] autorelease];
        }
    
        - (void)setNormalTexture:(CCTexture2D *)normalTexture {
            buttonNormal = normalTexture;
        }
        - (void)setLitTexture:(CCTexture2D *)litTexture {
            buttonLit = litTexture;
        }
    
        - (BOOL)isPressed {
            if (state == kButtonStateNotPressed) return NO;
            if (state == kButtonStatePressed) return YES;
            return NO;
        }
    
        - (BOOL)isNotPressed {
            if (state == kButtonStateNotPressed) return YES;
            if (state == kButtonStatePressed) return NO;
            return YES;
        }
    
        - (id)initWithTexture:(CCTexture2D *)aTexture
        {
            if ((self = [super initWithTexture:aTexture]) ) {
    
                state = kButtonStateNotPressed;
            }
    
            return self;
        }
    
        - (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 == kButtonStatePressed) return NO;
            if ( ![self containsTouchLocation:touch] ) return NO;
            if (buttonStatus == kButtonStatusDisabled) return NO;
    
            state = kButtonStatePressed;
            [self setTexture:buttonLit];
    
            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.
    
            if ([self containsTouchLocation:touch]) return;
            //if (buttonStatus == kButtonStatusDisabled) return NO;
    
            state = kButtonStateNotPressed;
            [self setTexture:buttonNormal];
    
        }
    
        - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
        {
    
            state = kButtonStateNotPressed;
            [self setTexture:buttonNormal];
    
    
        }
    
    @end
    

    Hope this helps and Happy coding!

    ADDITION of tick method and explanation (for Stephan’s question below):

    To check the status of the buttons I have a tick: method that basically fires every frame and checks the status of all my buttons.

        -(void)tick:(ccTime)dt {
    
    do my button checks here....
    
    }
    

    I check the status of my buttons by calling a isPressed or isNotPressed function that is part of my spuButton class.

    for (spuButton *aButton in _fourButtonsArray) {
         if ([aButton isNotPressed]) continue; //this button is not pressed
         .....otherwise record that it is pressed.....
    }
    

    Then I do the same kind of check to see if it has been released and respond accordingly. I do it this way because I want to be able to react to multiple button press combos plus I want to do something when it gets pressed down and then something else when it gets released. I use the ccTouchBegan and ccTouchEnded to change the textures(the sprite image) and to change the state variable accordingly.

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

Sidebar

Related Questions

hey all, just getting started on hadoop and curious what the best way in
hey all, I'm just getting started with c3p0 for database connection pooling. It's attaching
Hey, I've just started learning JavaScript and I'm making a little script that generates
So I've just started digging into Objective-C, Cocoa, and iPhone development. I've ran into
Hey all, just wondering what the best way to add this capability was. I
Hey all, I've created a loading/splash screen that loads at the beginning of my
Hey all, weird question. My company has an application from another company that records
Hey all, I have an array that holds all of the .aif file paths
Hey all, my Computational Science course this semester is entirely in Java. I was
Hey all. Newbie question time. I'm trying to setup JMXQuery to connect to my

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.