Comment obtenir des objets de réagir à des touches de Cocos2D?

Bon, alors je commence à en apprendre plus sur Coco2D, mais je suis un peu frusterated. Beaucoup de tutoriels que j'ai trouvé sont pour les anciennes versions du code, donc quand je regarde à travers et de voir comment ils le font certaines choses, je ne peux pas le traduire dans mon propre programme, parce que beaucoup de choses ont changé. Avec cela étant dit, je travaille dans la dernière version de Coco2d, version 0.99.

Ce que je veux faire est de créer un sprite à l'écran (Fait) et puis quand je touche que sprite, je peux avoir "quelque chose" se passe. Pour l'instant, nous allons simplement faire une alerte. Maintenant, j'ai eu ce code de travail avec l'aide d'un ami. Voici le fichier d'en-tête:

//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

Et voici le fichier de mise en oeuvre:

//
//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");
}

Toutefois, cela ne fonctionne que pour 1 objet, le sprite que je crée le CGRect pour. Je ne peux pas le faire pour les 2 sprites, dont j'ai été le tester. Donc ma question est: Comment puis-je avoir tous les sprites à l'écran réagissent de la même manifestation, quand on la touche?

Pour mon programme, le même événement doit être exécuté pour tous les objets du même type, ce qui devrait le rendre un peu plus facile. J'ai essayé de faire une sous-classe de CCNode et l'écriture de la méthode, mais qui ne fonctionne pas du tout... donc je suis en train de faire quelque chose de mal. De l'aide serait apprécié!

En passant par le "Touche" projet de cocos2D et de voir si je vois comment ils ont fait. On dirait qu'il fait une sous-classe, et a remplacé les méthodes:

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

Donc maintenant j'arrive à comprendre pourquoi le mien ne fonctionne pas... hmm...

OriginalL'auteur Ethan Mick | 2010-03-13