0
votes

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?

1
what size sprite are you using, and also are you in landScape. Just so I can test for you. - DogCoffee
The sprite is h, w: 728, 1024, and the iPhone I am testing on is in portrait. - zeeple
ok, also have you tried using a native scrollview in iOS ? Works well.. is that a better fit for what you want? - DogCoffee
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

1 Answers

1
votes

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];