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 660129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:07:19+00:00 2026-05-13T23:07:19+00:00

Alright, so I’m starting to learn more about Coco2D, but I’m kinda frusterated. A

  • 0

Alright, so I’m starting to learn more about Coco2D, but I’m kinda frusterated. A lot of the tutorials I have found are for outdated versions of the code, so when I look through and see how they do certain things, I can’t translate it into my own program, because a lot has changed. With that being said, I am working in the latest version of Coco2d, version 0.99.

What I want to do is create a sprite on the screen (Done) and then when I touch that sprite, I can have “something” happen. For now, let’s just make an alert go off. Now, I got this code working with the help of a friend. Here is the header file:

// When you import this file, you import all the cocos2d classes
#import "cocos2d.h"

// HelloWorld Layer
@interface HelloWorld : CCLayer
{
 CGRect spRect;
}

// returns a Scene that contains the HelloWorld as the only child
+(id) scene;

@end

And here is the implementation file:

//
// cocos2d Hello World example
// http://www.cocos2d-iphone.org
//

// Import the interfaces
#import "HelloWorldScene.h"
#import "CustomCCNode.h"

// HelloWorld implementation
@implementation HelloWorld

+(id) scene
{
 // 'scene' is an autorelease object.
 CCScene *scene = [CCScene node];

 // 'layer' is an autorelease object.
 HelloWorld *layer = [HelloWorld node];

 // add layer as a child to scene
 [scene addChild: layer];

 // return the scene
 return scene;
}

// on "init" you need to initialize your instance
-(id) init
{
 // always call "super" init
 // Apple recommends to re-assign "self" with the "super" return value
 if( (self=[super init] )) {

  // create and initialize a Label
  CCLabel* label = [CCLabel labelWithString:@"Hello World" fontName:@"Times New Roman" fontSize:64];

  // ask director the the window size
  CGSize size = [[CCDirector sharedDirector] winSize];

  // position the label on the center of the screen
  label.position =  ccp( size.width /2 , size.height/2 );

  // add the label as a child to this Layer
  [self addChild: label];

  CCSprite *sp = [CCSprite spriteWithFile:@"test2.png"];

  sp.position = ccp(300,200);
  [self addChild:sp];
  float w = [sp contentSize].width;
  float h = [sp contentSize].height;
  CGPoint aPoint = CGPointMake([sp position].x - (w/2), [sp position].y - (h/2));
  spRect = CGRectMake(aPoint.x, aPoint.y, w, h);






  CCSprite *sprite2 = [CCSprite spriteWithFile:@"test3.png"];
  sprite2.position = ccp(100,100);
  [self addChild:sprite2];



  //[self registerWithTouchDispatcher];
  self.isTouchEnabled = YES;



 }
 return self;
}


// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
 // in case you have something to dealloc, do it in this method
 // in this particular example nothing needs to be released.
 // cocos2d will automatically release all the children (Label)

 // don't forget to call "super dealloc"
 [super dealloc];
}

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  UITouch *touch = [touches anyObject];
  //CGPoint location = [[CCDirector sharedDirector] convertCoordinate:[touch locationInView:touch.view]];
 CGPoint location = [touch locationInView:[touch view]];

 location = [[CCDirector sharedDirector] convertToGL:location];
 if (CGRectContainsPoint(spRect, location)) {
  UIAlertView *alert = [[UIAlertView alloc]
         initWithTitle:@"Win"
         message:@"testing"
         delegate:nil cancelButtonTitle:@"okay"
         otherButtonTitles:nil];

  [alert show];
  [alert release];
  NSLog(@"TOUCHES");
 }
 NSLog(@"Touch got");

}

However, this only works for 1 object, the sprite which I create the CGRect for. I can’t do it for 2 sprites, which I was testing. So my question is this: How can I have all sprites on the screen react to the same event when touched?

For my program, the same event needs to be run for all objects of the same type, so that should make it a tad easier. I tried making a subclass of CCNode and over write the method, but that just didn’t work at all… so I’m doing something wrong. Help would be appreciated!

Going through the “Touches” project in cocos2D and seeing if I see how they did it. It looks like they made a subclass and overwrote the methods:

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event

So now I get to figure out why mine doesn’t work… hmm…

  • 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-13T23:07:20+00:00Added an answer on May 13, 2026 at 11:07 pm

    I got it. I had to add some more code to the custom class:

    Header File:

    //
    //  CustomCCNode.h
    //  Coco2dTest2
    //
    //  Created by Ethan Mick on 3/11/10.
    //  Copyright 2010 Wayfarer. All rights reserved.
    //
    
    #import "cocos2d.h"
    
    
    @interface CustomCCNode : CCSprite <CCTargetedTouchDelegate> {
    
    }
    
    @property (nonatomic, readonly) CGRect rect;
    
    
    @end
    

    Implementation:

    //
    //  CustomCCNode.m
    //  Coco2dTest2
    //
    //  Created by Ethan Mick on 3/11/10.
    //  Copyright 2010 Wayfarer. All rights reserved.
    //
    
    #import "CustomCCNode.h"
    #import "cocos2d.h"
    
    
    @implementation CustomCCNode
    
    - (CGRect)rect
    {
        CGSize s = [self.texture contentSize];
        return CGRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
    }
    
    
    
    - (BOOL)containsTouchLocation:(UITouch *)touch
    {
        return CGRectContainsPoint(self.rect, [self convertTouchToNodeSpaceAR:touch]);
    }
    
    - (void)onEnter
    {
        [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
        [super onEnter];
    }
    
    
    - (void)onExit
    {
        [[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
        [super onExit];
    }   
    
    
    
    
    
    - (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
    {
        NSLog(@"ccTouchBegan Called");
        if ( ![self containsTouchLocation:touch] ) return NO;
        NSLog(@"ccTouchBegan returns YES");
        return YES;
    }
    
    - (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
    {
        CGPoint touchPoint = [touch locationInView:[touch view]];
        touchPoint = [[CCDirector sharedDirector] convertToGL:touchPoint];
    
        NSLog(@"ccTouch Moved is called");
    }
    
    - (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
    {
        NSLog(@"ccTouchEnded is called");
    }
    
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright so I have no idea how to even begin doing this But basically
Alright. This may be difficult but I have been struggling for quite a bit
Alright, so I'm about 90% sure that this isn't possible, but I thought I'd
Alright, this is driving me nuts because my regex is working on Rubular, but
Alright so I have been looking around (on SO and Google) to see if
Alright, i have read many different views on how to do this with no
Alright guys, I have looked on the internet for ages and simply could not
Alright, here is my problem i'm trying to solve. I have an index page
Alright here is what I am attempting to do. I have a form written
Alright, I have some questions... When you have a moment, can you take a

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.