cocos2d-iphone – 在Coco2d中滑动

发布时间:2019-10-14 发布网站:大佬教程
大佬教程收集整理的这篇文章主要介绍了cocos2d-iphone – 在Coco2d中滑动大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图让刷卡工作为Cocos2d最新版本这里是我的代码:
-(void) setupGestureRecognizers 
{
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)];

    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];

    [swipeLeft setNumberOfTouchesRequired:1];

    [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeLeft];


}

它完全没有检测到滑动!

更新1:

我将代码更新为以下内容,但仍然没有检测到滑动.

-(void) setupGestureRecognizers 
{
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeLeft)];

    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];

    [swipeLeft setNumberOfTouchesRequired:1];

    [[[[CCDirector sharedDirector] openGLView] window] setUserInteractionEnabled:YES];

    [[[CCDirector sharedDirector] openGLView] setUserInteractionEnabled:YES];
    [[[CCDirector sharedDirector] openGLView] addGestureRecognizer:swipeLeft];


}
我也尝试过这项工作,但我发现了一种更简单,也更好的控制方法.

所以例如,如果你想要检测左边的滑动我会这样跟随.

在你的类的接口中声明两个变量

CGPoint firstTouch;
CGPoint lastTouch;

在init方法中执行你的类启用touches

self.isTouchEnabled = YES;

3.将这些方法添加到您的班级

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    UITouch * touch = [[allTouches allObjects] objectAtIndex:0];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    //Swipe Detection Part 1
    firstTouch = location;
}

-(void) ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    NSSet *allTouches = [event allTouches];
    UITouch * touch = [[allTouches allObjects] objectAtIndex:0];
    CGPoint location = [touch locationInView: [touch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];

    //Swipe Detection Part 2
    lastTouch = location;

    //Minimum length of the swipe
    float swipeLength = ccpDistance(firstTouch,lastTouch);

    //Check if the swipe is a left swipe and long enough
    if (firstTouch.x > lastTouch.x && swipeLength > 60) {
        [self doStuff];
    }

}

如果发生左滑动,则调用方法“doStuff”.

大佬总结

以上是大佬教程为你收集整理的cocos2d-iphone – 在Coco2d中滑动全部内容,希望文章能够帮你解决cocos2d-iphone – 在Coco2d中滑动所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ群:277859234,请注明来意。