实现多点触控...需要帮助存储第二触摸

时间:2011-04-22 15:02:05

标签: cocoa-touch cocos2d-iphone touch

需要能够存储2个触摸,我如何去做这个我怎么不知道...

这就是我一触即发的方式

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

/////checks whether the screen has been touched and stores its location and converts the coordinates to be avaible for use////
UITouch* myTouch = [touches anyObject];
CGPoint locationLeft = [myTouch locationInView: [myTouch view]];
locationLeft = [[CCDirector sharedDirector]convertToGL:locationLeft];

我怎样才能存储第二次触摸?

提前致谢

2 个答案:

答案 0 :(得分:2)

当您需要处理多点触控时,您应该使用ccTouchBegan(注意单数'触摸'而不是'触摸')。 (国际海事组织人员应该放弃ccTouchesBegan / Moved / Ended,只使用ccTouchBegan / Moved / Ended)。

每次触摸都会调用ccTouchBegan / Moved / Ended中的每一个,这意味着您可以轻松区分多个触摸。例如:

- (void)registerWithTouchDispatcher {
   [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:YES]; 
}

- (BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
   if (self.firstTouch == nil) {
      // we got the first touch
      self.firstTouch = touch;
   }
   else if (self.secondTouch == nil) {
      // we got the second touch
      self.secondTouch = touch;
   }
   // return YES to consume the touch (otherwise it'll cascade down the layers)
   return YES;
}

- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event {
   if (touch == self.firstTouch) {
      // we got the first touch
      // do stuff
   }
   else if (touch == self.secondTouch) {
      // we got the second touch
      // do stuff
   }
}

- (void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event {
   if (touch == self.firstTouch) {
      // first touch ended so remove both touches
      self.firstTouch = nil;
      self.secondTouch = nil;
   }
   else if (touch == self.secondTouch) {
      // second touch ended so remove touch only
      self.secondTouch = nil;
   }
}

答案 1 :(得分:0)

您是否尝试过迭代这样的触摸?

for (UITouch *touch in touches){
    CGPoint location = [touch locationInView:[touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];
    location = [self convertToNodeSpace:location];