2

I have an empty SKScene which needs to be horizontally centered, so I set it's anchorPoint:

self.anchorPoint = CGPoint(0.5f, 0);

I also need to scale it, per SpriteKit - Set Scale and Physics, so I run an action on it like this:

[self runAction:[SKAction scaleTo:0.5f duration:0.0f]];

My problem is that when I detect touches, I get locations that seem strangely off. I detect touches using the usual (locationInNode:self) method, and I'm currently adding a little blue square where the touches are, but when I touch the 4 corners of my device, I see a frame that is a quarter of my screen (correctly) but is moved to the left by a seemingly arbitrary amount

Here are some things I've already checked:

  • scene is initialized in viewWillLayoutSubviews, I know it has the correct initial dimensions
  • scene's scaleMode is set to SKSceneScaleModeAspectFill, but I've tried all of them to no avail
Community
  • 1
  • 1

2 Answers2

2

I was struggling with the same issue for awhile, but I think I finally got it figured out. You're not supposed to scale the scene (like it hints if you try setScale). You're supposed to resize it.

Try this:

myScene.scaleMode = SKSceneScaleModeAspectFill;

And then while zooming:

myScene.size = CGSizeMake(newX, newY);

JKallio
  • 903
  • 5
  • 15
  • Good tip. I found that if you rescale the scene, touching outside the boundaries of the rescaled scene gets translated to new coords within the new scene area. With resize, though, that problem went away. Although, I was caught off guard at first that resizing the scene to a larger number actually reduces the size of the scene's contents. I suppose that you're cramming in more pixels within the same "volume" when you resize, so everything inside gets reduced accordingly. – Thunk Dec 31 '13 at 18:51
0

Set Anchor Point as

self.anchorPoint = CGPoint(0.5f, 0);

And set Scene Scale Mode as ASPECT FIT, not aspect fill.

SKSceneScaleModeAspectFit
Programmer
  • 408
  • 1
  • 5
  • 15