Cocos2d iPhone v3.0 - 向上滑动以执行操作

时间:2014-04-13 19:21:17

标签: ios cocos2d-iphone swipe-gesture

我在设置触摸以滑动以执行操作时遇到一些问题。我正在尝试使用下面的代码,但它无法识别触摸。

-(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中启用了用户交互。任何帮助都会被提升!

1 个答案:

答案 0 :(得分:1)

在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];
*/