2011-04-22 94 views
0

需要能够存储2个触动,我如何去帮助这个我怎么不知道......实现多点触摸......需要存储的第二触摸

这就是我正在做一个单touch

- (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]; 

我该如何存储第二次触摸?

在此先感谢

回答

0

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

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

您应该使用ccTouchBegan(请注意,而不是“触摸”奇异“触摸”)当你需要处理多点触摸。 (国际海事组织的人应该放弃ccTouchesBegin/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; 
    } 
} 
+0

要做我的触摸检测和存储我用了一个教程,并修改它的abit,但是这已经让我失去了:S。我如何处理UITouch * myTouch = [touch anyObject]; \t CGPoint locationLeft = [myTouch locationInView:[myTouch view]]; \t locationLeft = [[CCDirector sharedDirector] convertToGL:locationLeft]; – michael 2011-04-22 15:30:45

+0

将'UITouch * myTouch = [touch anyObject]'更改为'UITouch * myTouch = touch' .. – Lukman 2011-04-22 16:06:40