Вопрос

I have done research on how to work with multiple touches in cocos2d v3, and I realize that it is necessary to do

self.multipleTouchEnabled = YES;

in order to get more than one touch. I have already done that, I just am not sure how to access the positions of all the touches at a time. Before you could isolate the NSSet or something like that and get touch 1, touch 2, etc; however now I am unsure how to do this.

If it helps, I am trying to make a pinch/zoom thing, and I know how to do it but I do not know how to access the positions of the touches.

I know that for just 1 touch you can get the position easily, yet for both I don't see a way to do it. Sorry for rambling basically the same thing!

I also heard that you can have a counter var to see how many touches are happening at the time, yet this seems inefficient and you still wouldn't be able to get all the touch's positions.

Thanks in advance, I really appreciate the help.

Это было полезно?

Решение

Maybe my way it's not the best way, but it works. Simply make a NSMutableArray where you store the touches, and then you can get both positions.

NSMutableArray *array = [[NSMutableArray alloc]init];

In your touchBegan and touchEnded method:

-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
[array addObject:touch]; //add new touch to array

}
-(void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event{
 [array removeObject:touch]; //remove touch that ended
}

In your method to get position:

//EXAMPLE.

if (array.count > 1){ //check that there are two touches
CGPoint point1 = [array[0] locationInNode:self];
CGPoint point2 = [array[1] locationInNode:self];
}else{ //I suppose there are ONE or TWO touches, not more.
CGPoint point1 = [array[0] locationInNode:self];
}

I think this is correct. Tell me if you have any problem.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top