如何处理多点触控
我正在使用 box2d 在 xcode 中编写一个应用程序。现在我正在使用下面的代码。问题是它只能处理一个触摸事件。如何使我的代码处理所有触摸事件,在本例中检查每次触摸的位置。我还想存储触摸,以便当它们结束时我可以使用正确的代码来结束各个触摸开始的任何内容。
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (locationWorld.x >= screenSize.width*2/5/PTM_RATIO && locationWorld.x <= screenSize.width*3.25/5/PTM_RATIO) {
//do something
}
else if (0 && locationWorld.x <= screenSize.width*2/5/PTM_RATIO) {
//do something else
}
}
I am writing an app in xcode using box2d. Right now I am using the code below. The problem is that it will only handle one touch event. How can I make my code handle all of the touch events, in this case check the location of each touch. I also want to store the touches so that when they end I can use the proper code to end whatever the individual touches started.
-(void) ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *myTouch = [touches anyObject];
CGPoint location = [myTouch locationInView:[myTouch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
b2Vec2 locationWorld = b2Vec2(location.x/PTM_RATIO, location.y/PTM_RATIO);
CGSize screenSize = [CCDirector sharedDirector].winSize;
if (locationWorld.x >= screenSize.width*2/5/PTM_RATIO && locationWorld.x <= screenSize.width*3.25/5/PTM_RATIO) {
//do something
}
else if (0 && locationWorld.x <= screenSize.width*2/5/PTM_RATIO) {
//do something else
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
它应该是这样的:
It should be something like this:
您可以通过以下方式获取触摸屏幕的手指数量:
您可以使用 for 循环和单步遍历 touchEvents 来获取每个触摸的单独位置、多次点击等。
You can get the number of fingers touching the screen with:
You can get each touches individual location, multi-taps, etc., using and enumerated for loop and stepping through touchEvents.
除了迭代触摸集之外,您还需要确保视图启用了多点触摸。这可以在 Interface Builder/Xcode 4 中完成
In addition to iterating through the set of touches, you'll need to make sure that the view is multi-touch enabled. This can be done in Interface Builder/Xcode 4
在 COCOS2D-X 中
In COCOS2D-X