我正在 cocos2d 中制作一个简单的物理游戏,并希望以滑动的速度在滑动时发射粒子。为了获得速度,我需要进行两次触摸并确定(位置差异)/(时间戳差异)。我的问题是我无法触及两次。我尝试了几种方法,但都没有奏效。
我尝试存储第一次触摸
@property (nonatomic, strong) UITouch *firstTouch;
...
- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
[self setFirstTouch:touch];
NSLog(@"first touch time 1: %f", self.firstTouch.timestamp);
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [touches anyObject];
NSLog(@"touch ended");
NSLog(@"first touch time 2: %f", self.firstTouch.timestamp);
NSLog(@"end of touch time: %f", touch.timestamp);
}
但这每次都给我一个 0 的时间戳差异。好像是把firstTouch换成了最近的触摸
我是不是用指针做错了什么,它被替换了?
也许我可以从 ccTouchesMoved 获取最后两次触摸?
最佳答案
一种更简单的方法是将 UIPanGestureRecognizer
添加到您的 [CCDirector sharedDirector].openGLView
然后使用 velocityInView
手势识别器的属性。
UIPanGestureRecognizer* gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)] autorelease];
[[CCDirector sharedDirector].openGLView addGestureRecognizer:gestureRecognizer];
然后:
- (void)handleSwipe:(UIPanGestureRecognizer*)recognizer {
... recognizer.velocity...
}
如果您想遵循基于 ccTouches...
的方法,在我看来,可能存在对滑动手势的误解,因为它不是由 2 次触摸组成的:它只是一次触摸,有开始、一些 Action 和结束。因此,您需要在 ccTouchesMoved:
中跟踪手指的移动;然后在touchesEnded判断是横扫还是竖扫,方向,发射子弹。
希望这些建议中的任何一个能有所帮助。
关于iphone - 触摸速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14162755/