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