0

I have a SKSpriteNode as a child of an SKScene, but the sprite node is much bigger than the scene, and I need the user to be able to manually scroll the sprite node around in order to see all of it (it is a giant map). I am using the SKAction moveByX: y: Duration: method and the moveByX part is working as expected. The move by Y pat though, causes the map to shoot off the bottom of the screen very fast. Here is my code in touchesMoved:

    CGFloat xDiff = location.x - prevLocation.x;
    CGFloat yDiff = location.y - prevLocation.y;
    SKAction *moveBy = [SKAction moveByX:xDiff y:yDiff duration:0];
    if (YES) NSLog(@"%f, %f", xDiff, yDiff);
    [map runAction:moveBy];

So, this logic is spot on for the x axis but not so for the y axis. Furthermore, if I change the yDiff to be calculated in reverse (prevLocation.y - location.y) I notice in the NSLog output that the yDiff is cumulative in a single pan action, but the xDiff is not.

What am I missing?

zeeple
  • 5,509
  • 12
  • 43
  • 71
  • what size sprite are you using, and also are you in landScape. Just so I can test for you. – DogCoffee Oct 16 '13 at 03:51
  • The sprite is h, w: 728, 1024, and the iPhone I am testing on is in portrait. – zeeple Oct 16 '13 at 03:53
  • ok, also have you tried using a native scrollview in iOS ? Works well.. is that a better fit for what you want? – DogCoffee Oct 16 '13 at 03:54
  • I thought about how I might do that actually. What I am eventually aiming for is to have the scene be the exact size of whatever screen I am on (iPad or iPhone, and then the scene would have the giant map as a child node, which could scroll around, but also have a number of labelNodes with a higher zPosition, which would stay put, even when the map underneath it is being moved around by the user. – zeeple Oct 16 '13 at 04:00
  • try out this http://stackoverflow.com/questions/19082251/zooming-and-scrolling-in-spritekit – DogCoffee Oct 16 '13 at 04:03
  • @Smick: hmmm... seems awfully complex for the size of this task. Is it possible this is a bug is sprite kit? Why doesn't the y axis obey the same rules as the x axis?? – zeeple Oct 16 '13 at 04:20
  • use bobmoff answer, its actually very easy. – DogCoffee Oct 16 '13 at 04:32

1 Answers1

1

I tried you code and it does what you expect. Just double check the touches moved looks like this.

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    CGPoint currentPoint = [[touches anyObject] locationInNode:self];

    CGPoint previousPoint = [[touches anyObject] previousLocationInNode:self];

    CGFloat xDiff = currentPoint.x - previousPoint.x;
    CGFloat yDiff = currentPoint.y - previousPoint.y;

    NSLog(@"%f, %f", xDiff, yDiff);
    SKAction *moveBy = [SKAction moveByX:xDiff y:yDiff duration:0];
    [_bg runAction:moveBy];
}

Also I set the bg like this.

    _bg = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
    _bg.anchorPoint = CGPointZero;
    _bg.position = CGPointZero;
    [self addChild:_bg];
DogCoffee
  • 19,820
  • 10
  • 87
  • 120
  • 1
    OMG. I am almost too embarrassed to show my mistake.... But here: CGPoint location = [touch locationInNode:self]; CGPoint prevLocation = [touch previousLocationInView:self.view]; – zeeple Oct 16 '13 at 04:47