How to get objects to react to touches in Cocos2D?

Posted by Wayfarer on Stack Overflow See other posts from Stack Overflow or by Wayfarer
Published on 2010-03-13T02:14:41Z Indexed on 2010/03/13 2:17 UTC
Read the original article Hit count: 651

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!

© Stack Overflow or respective owner

Related posts about cocos2d

Related posts about cocos2d-iphone