Wie man Objekte reagieren auf Berührungen in Cocos2D?

Okay, ich bin also angefangen zu lernen, mehr über Coco2D, aber ich bin irgendwie frusterated. Viele der tutorials, die ich gefunden habe sind für veraltete Versionen der code, also wenn ich einen Blick durch und sehen, wie Sie bestimmte Dinge tun, kann ich nicht übersetzen, in meinem eigenen Programm, da hat sich eine Menge verändert. Mit, dass gesagt wird, ich arbeite in der neuesten version von Coco2d, version 0.99.

Was ich will zu tun ist, erstellen Sie ein sprite auf dem Bildschirm (Fertig) und wenn ich dann Tippen, dass sprite, ich kann "etwas" passieren. Für jetzt, lasst uns einfach stellen Sie eine Warnung Los. Nun, ich habe diese code funktioniert mit der Hilfe eines Freundes. Hier ist die header-Datei:

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

Und hier ist die Umsetzung Datei:

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

}

Allerdings funktioniert dies nur für 1 Objekt, die sprite, die ich erstellen die CGRect für. Ich kann es nicht für 2 sprites, die ich testete. Also meine Frage ist: Wie kann ich alle sprites auf dem Bildschirm reagieren auf das gleiche Ereignis, wenn Sie berührt werden?

Für mein Programm, das gleiche Ereignis ausgeführt werden muss, die für alle Objekte des gleichen Typs, so machen es ein bisschen einfacher. Ich habe versucht, eine Unterklasse von CCNode und schreiben über die Methode, aber das ging gar nicht... also mache ich etwas falsch. Hilfe würde geschätzt!

Werde durch die "Berührt" - Projekt in cocos2D und sehen, wenn ich sehe, wie Sie es getan hat. Es sieht aus wie Sie eine Unterklasse und überschrieb die Methoden:

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

So, jetzt komme ich, um herauszufinden, warum meins nicht funktioniert... hmm...

InformationsquelleAutor Ethan Mick | 2010-03-13
Schreibe einen Kommentar