COCOS2D iPhone V3.0 - Swiping Up per eseguire un'azione
-
21-12-2019 - |
Domanda
Sto avendo dei problemi con la configurazione dei tocchi per far scorrere su per eseguire un'azione.Sto cercando di usare il codice qui sotto, ma non riconoscerà i tocchi.
-(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]];
}
}
.
Non sta nemmeno dicendo "toccato" quando tocco lo schermo e ho le interazioni utente abilitate in init.Qualsiasi aiuto sarà apprezzato!
Soluzione
In COCOS2D V3 è necessario sostituire tutto ccTouchesBegan/ccTouchesMoved/ccTouchesEnded
a
-(void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event
-(void) touchMoved:(UITouch *)touch withEvent:(UIEvent *)event
-(void) touchEnded:(UITouch *)touch withEvent:(UIEvent *)event
.
La buona notizia è che è possibile ottenere la posizione corrente del tatto usando semplicemente la seguente riga
CGPoint touchPos = [touch locationInNode:self];
.
Tutto questo è quindi, non richiesto
/*
NSSet *allTouches = [event allTouches];
UITouch * touch = [[allTouches allObjects] objectAtIndex:0];
CGPoint location = [touch locationInView: [touch view]];
location = [[CCDirector sharedDirector] convertToGL:location];
*/
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow