Question

I add a sprite on the background sprite and then zoom (pinch) the background and by that the sprite as well.

After the zoom i want to be able to click right on the "zoomed" sprite and then move (pan) it. However, to catch the sprite i have to click outside of the sprite. I guess that is due to the zoom and coordinates.

I would really need some help how to make it so that i always only need to click on the actual sprite if it is in it's original state or zoomed up or down.

I will also implement UIScrollView so i can move around the background but would like to fix this first.

Here is the code i use to test this:

- (void)didMoveToView:(SKView *)view {

_background = [SKSpriteNode spriteNodeWithImageNamed:@"background"];
_background.name = @"background";
[_background setAnchorPoint:CGPointZero];
//    _background.anchorPoint = CGPointMake(0.0, 0.1);
self.scaleMode = SKSceneScaleModeResizeFill;
[self addChild:_background];

SKLabelNode *texT = [SKLabelNode labelNodeWithFontNamed:@"Ariel"];
texT.text = @"X";
texT.position = CGPointMake(160, 260);
[_background addChild:texT];

_panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[self.view addGestureRecognizer:_panGesture];

_pinchGesture = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(handlePinch:)];
[self.view addGestureRecognizer:_pinchGesture];

/* Setup your scene here */

self.backgroundColor = [SKColor colorWithRed:0.15 green:0.15 blue:0.3 alpha:1.0];
SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithImageNamed:@"Spaceship"];
sprite.xScale = 0.2;
sprite.yScale = 0.2;
sprite.position = CGPointMake(160, 260);
sprite.name = @"sprite";
[_background addChild:sprite];
}

- (void)handlePan:(UIPanGestureRecognizer *)sender {
NSLog(@"handlePan:");
if (![_currentNode.name isEqualToString:@"background"]) {
    _touchLocation = [sender locationInView:sender.view];
    _touchLocation = [self convertPointFromView:_touchLocation];
    _currentNode.position = _touchLocation;
}
}

- (void)handlePinch:(UIPinchGestureRecognizer *) sender {
NSLog(@"handlePinch: %f",sender.scale);
[self runAction:[SKAction scaleBy:sender.scale duration:0]];
sender.scale = 1;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(@"touchesBegan");
//////DO THE FIRST TOUCH//////
_actualTouch = [touches anyObject];
_touchLocation = [_actualTouch locationInNode:self];
_currentNode = [self nodeAtPoint:_touchLocation];
NSLog(@"TouchBegan:_touchLocation:%@", NSStringFromCGPoint(_touchLocation));
NSLog(@"_currentNode.name: %@", _currentNode.name);
}

@end
Was it helpful?

Solution

I had this same problem and I found that the solution is to resize instead of scale. The physics seem to still work as expected and now the touch events will too. I came across this solution here. The scaleMode in your didMoveToView needs to be changed as well, see below.

self.scaleMode = SKSceneScaleModeAspectFill;


- (void)handlePinch:(UIPinchGestureRecognizer *) sender {
NSLog(@"handlePinch: %f",sender.scale);
//[self runAction:[SKAction scaleBy:sender.scale duration:0]];
self.size = CGSizeMake(self.size.width/sender.scale,self.size.height/sender.scale);
sender.scale = 1;
}

In my own implementation I had set upper and lower limits to the zoom level as well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top