Cocos2d iphone v3.0 - 滑动执行动作
-
21-12-2019 - |
题
我在设置触摸以滑动以执行动作的情况下有一些问题。我正在尝试使用下面的代码,但它不会识别触摸。
-(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];
*/
. 不隶属于 StackOverflow