Cocos2d iPhone v3.0 - Deslizar para cima para executar uma ação
-
21-12-2019 - |
Pergunta
Estou tendo alguns problemas ao configurar toques para deslizar para cima para executar ações.Estou tentando usar o código abaixo, mas ele não reconhecerá os toques.
-(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]];
}
}
Nem sequer diz "tocado" quando toco na tela e tenho as interações do usuário habilitadas no init.Qualquer ajuda será apreciada!
Solução
No cocos2d v3 você precisa substituir todos ccTouchesBegan/ccTouchesMoved/ccTouchesEnded
para
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
-(void) touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
-(void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event
A boa notícia é que você pode obter a localização atual do toque simplesmente usando a seguinte linha
CGPoint touchPos = [touch locationInNode:self];
tudo isso não é, portanto, necessário
/*
NSSet *allTouches = [event allTouches];
UITouch * touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
*/
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow