SKSpriteNode 不稳定滑动?
SKSpriteNode erratic swipe?
我创建了以下精灵套件节点:
SKSpriteNode *megadeth;
megadeth = [SKSpriteNode spriteNodeWithImageNamed:@"megadeth_rocks.png"];
megadeth.name = @"awesome";
并添加滑动手势如下:
-(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];
}
}
}
当我滑动节点时,它滑动但又回到原来的位置,然后淡出以移除。
我想要的是当我滑动时,它应该移动到滑动的位置并移除。我在这里遗漏了什么吗?
而不是 [SKAction moveByX:y:]
,我使用 [SKAction moveToY:duration:]
使一切都像魅力
这是更新后的 handleSwipeUp:
方法:
- (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];
}
//}
}
希望对您有所帮助!!!
我创建了以下精灵套件节点:
SKSpriteNode *megadeth;
megadeth = [SKSpriteNode spriteNodeWithImageNamed:@"megadeth_rocks.png"];
megadeth.name = @"awesome";
并添加滑动手势如下:
-(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];
}
}
}
当我滑动节点时,它滑动但又回到原来的位置,然后淡出以移除。
我想要的是当我滑动时,它应该移动到滑动的位置并移除。我在这里遗漏了什么吗?
而不是 [SKAction moveByX:y:]
,我使用 [SKAction moveToY:duration:]
使一切都像魅力
这是更新后的 handleSwipeUp:
方法:
- (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];
}
//}
}
希望对您有所帮助!!!