簡體   English   中英

為什么此SKAction moveByX:y:持續時間:根本不移動此精靈?

[英]Why isn't this SKAction moveByX: y: duration: moving this sprite at all?

我正在嘗試對場景中的“滑動手勢”做出響應,並以響應方式移動精靈。 該精靈顯示在屏幕的左側。 handleSwipeRight中的日志記錄語句將轉到日志。 但是精靈根本沒有動。 當然,我只是缺少一些關於SKAction的基本知識嗎?

這是我的場景代碼:

MFFMyScene.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];
    }
}

增強您的屬性,例如使用設置器讓系統為您完成正確的內存管理

@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 */
      }
   }
}

我使用這個SKAction錯誤。 我一直在尋找一種SKAction,它可以“每z秒移動x / y”或“在z秒內每幀移動x / y”。 按照我最初編寫的方式,SKAction將在100秒的時間內將精靈總計移動10點。

所以我認為精靈畢竟還是在移動,但是我的X / Y /持續時間值是如此之小,以至於看起來好像沒有移動。 在x = 10且持續時間= 100秒的情況下,它在100秒內移動了10點。 我什至不了解點對像素,但這是緩慢的運動。

我將其更改為x = 100,y = 50,持續時間= 1.0,並且移動得很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM