简体   繁体   中英

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

I'm trying to respond to Swipe Gestures in a scene and move a sprite in response. The sprite is displaying on the left side of the screen. The logging statement in the handleSwipeRight is going to the log. But the sprite isn't moving at all. Surely I'm just missing something basic on the SKAction?

Here's my Scene code :

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

Relevant bits from 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];
    }
}

Make your properties strong, use setters to let system do right memory management for you, for instance

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

check for swipe gesture state:

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

I was using this SKAction wrong. I was looking for an SKAction that would do 'move x/y every z seconds' or 'move x/y every frame for z seconds'. The way I originally had it written, the SKAction will move the sprite 10 points total over a period of 100 seconds.

So I think the sprite was moving after all, but my X/Y/duration values were such that the movement was so small that it looked like it wasn't moving. With x=10 and duration=100 seconds, it was moving 10 points over 100 seconds. I don't even understand point vs pixel, but that was slow movement.

I changed it to x=100, y=50, duration=1.0, and it moved just fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM