I have already implemented multitouch image rotation using standard iOS graphics library (Core Graphics).
It looks like this:
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if ([touches count] == 2) {
NSArray *twoTouches = [touches allObjects];
UITouch *first = [twoTouches objectAtIndex:0];
UITouch *second = [twoTouches objectAtIndex:1];
CGFloat currentAngle = angleBetweenLinesInRadians([first previousLocationInView:self.view], [second previousLocationInView:self.view], [first locationInView:self.view], [second locationInView:self.view]);
pic1.transform = CGAffineTransformRotate(pic1.transform, currentAngle);
}
}
Now I'm trying to implement this solution in my Cocos2开发者_Go百科d project. First I registered the CCTouchDispacher in my init method.
- (id) init
{
...
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
...
}
In my ccTouchMoved function I changed (UITouch *) to (NSSet *) and built the project just to be sure everything was correct. As a result I got following warning: "Incompatible Objective-C types initializing 'struct NSSet *', expected 'struct UITouch *'".
As everything still seemed to be working correctly I moved on and tried to extract first and second touch objects from (NSSet *) touch.
- (void)ccTouchMoved:(NSSet *)touch withEvent:(UIEvent *)event {
if ([touch count] == 2) {
NSArray *twoTouches = [touch allObjects];
UITouch *first = [twoTouches objectAtIndex:0];
UITouch *second = [twoTouches objectAtIndex:1];
}
}
Now I tried to compile it, which worked, but after I triggered ccTouchMoved through iPhone simulator, I got following error message: "-[UITouch count]: unrecognized selector sent to instance 0x542d6a0'".
Can somebody please explain, how to get this Cocos2d ccTouchMoved method to recognize and process multitouch events?
Thanks.
You've used wrong CC2D method.
you should use
- (BOOL)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
rather then
- (void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
as you want to capture multiple touches whereby ccTouchMoved capturs only one
精彩评论