My game in cocos2d
is handling touches from the user in a way that just get touches
, but not slide/drag
movements. How can I hlandle slide/drag
movements in cocos2d/objective-c
?
E.g. if the user slides the finger in any point of the screen above the sprite _player, I want to move the sprite _player up.
This is my function that is handling touches now:
-(void) touchBegan:(UITouch *)touch withEvent:(UIEvent *)event {
CGPoint touchLocation = [touch locationInNode:self];
positionY1 = self.contentSize.height*.2;
positionY2 = self.contentSize.height*.40;
positionY3 = self.contentSize.height*.60;
float distY1Y2 = (positionY1 + positionY2)/2;
float distY2Y3 = (positionY2 + positionY3)/2;
float playerMove;
if (touchLocation.y <= distY1Y2) {
if(_player.position.y <= distY1Y2) {
playerMove = positionY1;
} else if(_player.position.y > distY1Y2 && _player.position.y < distY2Y3) {
playerMove = positionY1;
} else if (_player.position.y >= distY2Y3) {
playerMove = positionY2;
}
} else if (touchLocation.y > distY1Y2 && touchLocation.y < distY2Y3) {
if(_player.position.y <= distY1Y2) {
playerMove = positionY2;
} else if(_player.position.y > distY1Y2 && _player.position.y < distY2Y3) {
playerMove = positionY2;
} else if (_player.position.y >= distY2Y3) {
playerMove = positionY2;
}
} else if (touchLocation.y >= distY2Y3) {
if(_player.position.y <= distY1Y2) {
playerMove = positionY2;
} else if(_player.position.y > distY1Y2 && _player.position.y < distY2Y3) {
playerMove = positionY3;
} else if (_player.position.y >= distY2Y3) {
playerMove = positionY3;
}
}
//play audio
[[OALSimpleAudio sharedInstance] playEffect:@"pew-pew-lei.caf"];
//Disable another touch movement
//[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
// Move our sprite to touch location
CCActionMoveTo *actionMove = [CCActionMoveTo actionWithDuration:.2f position:CGPointMake(_player.position.x, playerMove)];
[_player runAction:actionMove];
//Enable another touch movement
//[[UIApplication sharedApplication] endIgnoringInteractionEvents];
/*
NSMethodSignature *sgn = [self methodSignatureForSelector:@selector(enableTouch:)];
NSInvocation *inv = [NSInvocation invocationWithMethodSignature: sgn];
[inv setTarget: self];
[inv setSelector:@selector(enableTouch:)];
[NSTimer scheduledTimerWithTimeInterval:.7f
invocation:inv
repeats:NO];
*/
}