gpt4 book ai didi

cocos2d-iphone - 在Cocos2d中刷卡

转载 作者:行者123 更新时间:2023-12-04 06:47:16 24 4
gpt4 key购买 nike

我正在尝试为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方法中启用触摸
self.isTouchEnabled = YES;

3.将这些方法添加到您的类(class)中
-(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 - 在Cocos2d中刷卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9112791/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com