问题
I have created the following sprite kit node :
SKSpriteNode *megadeth;
megadeth = [SKSpriteNode spriteNodeWithImageNamed:@"megadeth_rocks.png"];
megadeth.name = @"awesome";
and added the swipe gesture as follows:
-(void)didMoveToView:(SKView *)view{
UISwipeGestureRecognizer *recognizerUp = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeUp:)];
recognizerUp.direction = UISwipeGestureRecognizerDirectionUp;
[[self view] addGestureRecognizer:recognizerUp];
UISwipeGestureRecognizer *recognizerDn = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeDn:)];
recognizerDn.direction = UISwipeGestureRecognizerDirectionDown;
[[self view] addGestureRecognizer:recognizerDn];
}
- (void)handleSwipeUp:(UISwipeGestureRecognizer *)sender{
NSLog(@"Node Swiped Up");
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint touchLocation = [sender locationInView:sender.view];
touchLocation = [self convertPointFromView:touchLocation];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
if([touchedNode.name isEqualToString:@"awesome"]){
NSLog(@"Perform Action on AWESOME node");
SKAction *moveUp = [SKAction moveByX:0.0 y:(0-touchedNode.position.y) duration:0.5];
SKAction *fade = [SKAction fadeOutWithDuration:0.25];
//SKAction *remove = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[moveUp, fade]];
[touchedNode runAction:sequence];
}
}
}
When I swipe the node, its swiping but it comes back to its original position and then fades to remove.
What I want is that when I swipe, it should move to the swiped position and remove. Am I missing something here ?
回答1:
Instead of [SKAction moveByX:y:]
, I used [SKAction moveToY:duration:]
which made everything work like charm
Here's the updated handleSwipeUp:
method:
- (void)handleSwipeUp:(UISwipeGestureRecognizer *)sender{
NSLog(@"Node Swiped Up");
//if (sender.state == UIGestureRecognizerStateEnded)
//{
CGPoint touchLocation = [sender locationInView:sender.view];
touchLocation = [self convertPointFromView:touchLocation];
SKSpriteNode *touchedNode = (SKSpriteNode *)[self nodeAtPoint:touchLocation];
if([touchedNode.name isEqualToString:@"awesome"]){
NSLog(@"Perform Action on AWESOME node");
**SKAction *moveUp = [SKAction moveToY:(touchedNode.position.y+200) duration:0.5];**
//SKAction *fade = [SKAction fadeOutWithDuration:0.25];
SKAction *remove = [SKAction removeFromParent];
SKAction *sequence = [SKAction sequence:@[moveUp, remove]];
[touchedNode runAction:sequence];
}
//}
}
Hope this helps !!!
来源:https://stackoverflow.com/questions/29065278/skspritenode-erratic-swipe