【问题标题】:Why isn't this SKAction moveByX: y: duration: moving this sprite at all?为什么这个 SKAction moveByX: y: duration: 根本不移动这个精灵?
【发布时间】:2013-12-29 13:49:18
【问题描述】:

我正在尝试响应场景中的滑动手势并移动精灵作为响应。精灵显示在屏幕的左侧。 handleSwipeRight 中的日志语句将进入日志。但是精灵根本不动。当然,我只是缺少 SKAction 的一些基本内容吗?

这是我的场景代码:

MFMMyScene.h:

#import <SpriteKit/SpriteKit.h>

@interface MFFMyScene : SKScene <UIGestureRecognizerDelegate>
@property (nonatomic) SKSpriteNode *player;
@property (nonatomic) UISwipeGestureRecognizer * swipeRightGesture;
@property (nonatomic) UISwipeGestureRecognizer * swipeLeftGesture;
@property (nonatomic) UISwipeGestureRecognizer * swipeUpGesture;
@property (nonatomic) UISwipeGestureRecognizer * swipeDownGesture;
@end

来自 MFFMyScene.m 的相关位:

-(id)initWithSize:(CGSize)size {    
    if (self = [super initWithSize:size]) {
        /* Setup your scene here */
        NSLog(@"Size: %@", NSStringFromCGSize(size));

        self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];

        SKTextureAtlas *atlas = [SKTextureAtlas atlasNamed:@"dungeon"];
        SKTexture *texture = [atlas textureNamed:@"hero_trans.png"];
        _player = [SKSpriteNode spriteNodeWithTexture:texture];
        _player.position = CGPointMake(10, 150);
        [self addChild:_player];
    }
    return self;
}

-(void) didMoveToView:(SKView *)view {

    _swipeRightGesture = [[UISwipeGestureRecognizer alloc]
                                        initWithTarget:self
                                        action:@selector(handleSwipeRight:)];
    _swipeRightGesture.direction = UISwipeGestureRecognizerDirectionRight;
    [view addGestureRecognizer:_swipeRightGesture];
}

- (void)handleSwipeRight:(UISwipeGestureRecognizer *)sender {
    if(sender.direction == UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"scene: SWIPE RIGHT");
        SKAction *movePlayerRight = [SKAction moveByX:10.0
                                                    y:0.0
                                             duration:100.0];
        [_player runAction:movePlayerRight];
    }
}

【问题讨论】:

    标签: ios ios7 sprite-kit


    【解决方案1】:

    让你的属性强大,例如使用setter让系统为你做正确的内存管理

    @property (nonatomic, strong) UISwipeGestureRecognizer *rightSwipe; 
    self.rightSwipe = /* init code here */
    

    检查滑动手势状态:

    - (void)swipeHandled:(UISwipeGestureRecognizer *)sender
    {
       if (sender.state == UIGestureRecognizerStateRecognized) {
          if (sender.direction == UISwipeGestureRecognizerDirectionRight) {
              /* moving code here */
          }
       }
    }
    

    【讨论】:

    • 感谢您的回答。我试过了,它也不起作用。
    【解决方案2】:

    我用错了这个 SKAction。我一直在寻找一个 SKAction 可以“每 z 秒移动 x/y”或“每帧移动 x/y z 秒”。按照我最初编写的方式,SKAction 将在 100 秒内总共移动精灵 10 点。

    所以我认为精灵毕竟在移动,但我的 X/Y/持续时间值是如此之小,以至于看起来它没有移动。在 x=10 和 duration=100 秒的情况下,它在 100 秒内移动了 10 个点。我什至不了解点与像素,但那是缓慢的运动。

    我把它改成 x=100,y=50,duration=1.0,它移动得很好。

    【讨论】:

    • SKAction moveTo 结束后,您是否遇到过精灵回到原来位置的问题?我正面临这样的问题。请帮我弄清楚skspritenode erratic swipe
    猜你喜欢
    • 2013-12-02
    • 1970-01-01
    • 2017-06-11
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 2015-02-23
    相关资源
    最近更新 更多