6

I'm creating universal game for all iOS devices in portrait mode using Swift. In GameViewController I'm creating scene like this:

let scene = GameScene(size:CGSize(width: 1536, height: 2048))
scene.scaleMode = .AspectFill

Background image has resolution 1536x2048, and so with above scaleMode on iPad it's displayed in its full size, on iPhone 6 1152x2048 is displayed with sides trimmed. Works perfectly fine on all devices, and only one background image is needed. Problem is that if I call for size.width or self.frame.size.width it always returns 1536, even if the actual visible area is e.g. 1152.

How can I set SkSpriteNode's position relative to visible area, so that it'll be for example 50x50 from the corner on every device?

2 Answers 2

10

How can I set SkSpriteNode's position relative to visible area, so that it'll be for example 50x50 from the corner on every device?

The "visible area" is simply the view.

So you can make your positions relative to the view, and not the scene. I actually do this a lot in my game, which is universal and runs on both OS X and iOS.

In my case I typically do this for the user-interface, so I might have a scaled scene but I want to set some positions not relative to the scaled scene but relative to the visible area (i.e. the view).

To do this, you can write a function that converts view coordinates to the corresponding scene coordinates.

Here is my function that I use. Note that I subtract my desired y-position from height of view so that I can treat (0,0) as the bottom-left like sprite-kit does instead of the top-left like UIKit does.

func convert(point: CGPoint)->CGPoint {
    return self.view!.convert(CGPoint(x: point.x, y:self.view!.frame.height-point.y), to: self)
}

Here is an example of using this function:

self.node.position = convert(CGPoint(x: 50, y: 50)) 

This will always force the position of the node to be at (50,50) relative to the view (the visible portion of the screen) regardless of how your scene is scaled and sized.

5
  • So you're using this function in GameViewController and that's also place where you're adding UI? Commented Mar 20, 2015 at 16:31
  • @shargath Oops, it should say view, not sceneView. I'll edit it now. I'm using this method in scene class. And my user-interface is done entirely in sprite-kit.
    – Epic Byte
    Commented Mar 20, 2015 at 16:34
  • Great, because I did my entire UI also in spriteKit so scene class works for me :) Commented Mar 20, 2015 at 16:36
  • @shargath I edited the answer again to give you an example of using the method.
    – Epic Byte
    Commented Mar 20, 2015 at 16:42
  • Hi, can you tell me the version for Objective-C?I try to use it like you wrote, but it won't work for some reason. On iPad 2, I see the sprite correctly, but the same on iPhone 5, doesn't display the sprite at all: ....self.inventory.position = [self convert:CGPointMake(-20, 600)];}-(CGPoint) convert:(CGPoint) point {return [self.view convertPoint:CGPointMake(point.x, self.view.frame.size.height - point.y) toScene:self];} Commented Sep 18, 2016 at 21:33
1

I don't think this is really the best approach. You should creating the GameScene based on SKView's size

let scene = GameScene(size: self.skView.bounds.size)

I don't think you should be setting one universal size for every device. You need to let the device set the scenes dimensions based on the screen's resolution. Then you need to be creating different images based on the device. 2x, 3x, 2x~ipad etc..

This tutorial is a good place to start: http://www.raywenderlich.com/49695/sprite-kit-tutorial-making-a-universal-app-part-1

1
  • 1
    Well actually the approach I've used was in raywenderlich.com's book iOS games by tutorials... Commented Mar 20, 2015 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.