I have implemented zoom/pinch on my SKScene with the selected answer in this thread:
Zooming an SKNode inconsistent
I am now attempting to get the hero character to stay centered (smoothly) during a pinch or zoom action because if I don't, the heroNode will disappear off the screen quickly. I have a centerHero method which I can call when I want, and it works quite well, but when I call this method at the end of the pinch handler the effect is very, very jerky. Here is the pinch handler:
- (void)handlePinch:(UIPinchGestureRecognizer *) recognizer
{
[map runAction:[SKAction scaleBy:recognizer.scale duration:0]];
recognizer.scale = 1;
// While zooming/pinching, make sure hero is centered:
[self centerHero];
}
and here is the center hero method:
- (void)centerHero
{
CGFloat centerX = self.view.bounds.size.width/2;
CGFloat centerY = self.view.bounds.size.height/2;
CGFloat heroX = [self.heroNode parent].position.x;
CGFloat heroY = [self.heroNode parent].position.y;
CGPoint heroPoint = CGPointMake(heroX, heroY);
CGPoint newHeroPoint = [self convertPoint:heroPoint fromNode:map];
CGFloat newHeroX = newHeroPoint.x;
CGFloat newHeroY = newHeroPoint.y;
CGFloat xDiff = centerX - newHeroX;
CGFloat yDiff = centerY - newHeroY;
SKAction *moveBy = [SKAction moveByX:xDiff y:yDiff duration:5];
[map runAction:moveBy];
}
Any suggestions on how to make this "auto-centering" smooth?