触摸速度

Ale*_*eue 0 iphone objective-c cocos2d-iphone ios

我正在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);
}
Run Code Online (Sandbox Code Playgroud)

但这给了我每次时间戳0的差异.它似乎用最近的触摸取代了firstTouch

我是否做错了指针,这被替换了?

也许我可以从ccTouchesMoved那里接受最后两次接触?

ser*_*gio 6

执行您要执行的操作的一种更简单的方法是向UIPanGestureRecognizer您添加一个[CCDirector sharedDirector].openGLView,然后使用velocityInView手势识别器的属性.

UIPanGestureRecognizer* gestureRecognizer = [[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)] autorelease];
[[CCDirector sharedDirector].openGLView addGestureRecognizer:gestureRecognizer];
Run Code Online (Sandbox Code Playgroud)

然后:

- (void)handleSwipe:(UIPanGestureRecognizer*)recognizer {
    ... recognizer.velocity...
}
Run Code Online (Sandbox Code Playgroud)

如果你想按照自己的方法进行操作ccTouches...,在我看来可能存在对滑动手势的误解,因为它不是由2个触摸构成的:它只是一个触摸,有一个开始,一些动作,和结束.因此,您需要跟踪手指的移动ccTouchesMoved:; 然后在touchesEnded确定它是水平还是垂直滑动,方向,并发出子弹.

希望这些建议可能有所帮助.