我在设置触摸以向上滑动以执行操作时遇到了一些问题。我正在尝试使用下面的代码,但它无法识别触摸。
-(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;
CCLOG(@"touched");
}
-(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);
CCActionJumpTo *jump1 = [CCActionJumpTo actionWithDuration:2 position:ccp(self.contentSize.width*.4,self.contentSize.height*.1) height:self.contentSize.height*.5 jumps:1];
[_fufunaken runAction:[CCActionRepeat actionWithAction:jump1 times:1]];
//Check if the swipe is an up swipe and long enough
if (firstTouch.y > lastTouch.y && swipeLength > 60) {
[_fufunaken runAction:[CCActionRepeatForever actionWithAction:jump1]];
}
}
当我触摸屏幕时,它甚至没有说“触摸”,而且我确实在 init 中启用了用户交互。我们将不胜感激!
最佳答案
在cocos2d v3中你需要将所有的ccTouchesBegan/ccTouchesMoved/ccTouchesEnded
替换成
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
-(void) touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
-(void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event
好消息是你可以通过简单地使用以下行来获取触摸的当前位置
CGPoint touchPos = [touch locationInNode:self];
因此,所有这些都不是必需的
/*
NSSet *allTouches = [event allTouches];
UITouch * touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
*/
关于ios - Cocos2d iPhone v3.0 - 向上滑动执行 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23047314/