NemoPS
NemoPS

Reputation: 411

SpriteKit spawn objects inside view frame across devices

I am creating my first game in spriteKit. I followed a few tutorial, so I am able to make the game work at a single size. The game has objects spawn from the top of the screen and fall towards the player, which is at the bottom.

The issue I am having is that both the player and objects' coordinates are relative to the scene, which is by default of the iPad pro 9.7 size. Now when I run the game on an iPhone 8, objects are spawn outside the view as well, and the player can also move a bit past the left/right (I am using aspectFill as scaling mode, so the sides get cut off).

What is the proper way to position both the player and objects as children of the current view, so that they are properly scaled and their coordinates are relative to it?

I was simply using this to give the player a starting position (that's a placeholder of course):

player = Player(color: .red, size: CGSize(width: 40, height: 80))
player.position = CGPoint(x: frame.midX, y: frame.size.height/5)
player.zPosition = ZPosition.player
addChild(player)

Edit: to clarify, the problem is that midX is 384, but the midX of the view is 160 on an iPhone8, thus the coordinate mismatch

Here is an image to clarify https://i.sstatic.net/Pe4hS.jpg The player is only supposed to be spawned in the center, that is not a concern, the problem is that at any given time a touch coordinate system is different from the player's system, making it impossible to be clicked.

Also, my center in gamescene.sks is 0,0

Upvotes: 0

Views: 105

Answers (3)

Knight0fDragon
Knight0fDragon

Reputation: 16837

You need to factor in the aspect ratio when you are developing in sprite kit. If you are truly using a "single size," then no matter what device you are on, your center should be the same. (0,0)

From what you are telling me with

the problem is that midX is 384, but the midX of the view is 160 on an iPhone8

is that you are reading from your view, not your scene.

This is bad, because your view and your scene are going to be two different sizes.

The first thing you need to do is define your playing area. If you want your paddle to hit the sides of all devices, then you need to develop in 18:39 aspect ratio (or 9:16 if you plan on using the safe areas on iphone x)

This means that on Ipads, the object will be cut off from the top of the screen because the clipping will happen at the top of the screen instead of the sides.

Now if you want to have the paddles to hit the sides of the screen and the object to spawn at the top of the screen, then you will need to use .resizeScale mode and normalize all of your units (usually between 0 and 1).

This means you are going to end up creating a different game experience across devices with different aspect ratios, as opposed to a different viewing experience from just clipping.

Upvotes: 1

NemoPS
NemoPS

Reputation: 411

I solved by adding margins and using them as variables

    guard let viewWidth = view?.frame.width else {return}

    if viewWidth < frame.width { // scaled device
        leftMargin = (frame.width - viewWidth) / 2
        rightMargin = leftMargin - viewWidth
    }

Upvotes: 0

Vishal Sharma
Vishal Sharma

Reputation: 1061

please share an image so that I can understand the problem properly now it looks like you are facing problems in constraints and you want your player position always on the middle of every screen which is quite simple you have to make the x value 0

player.position = CGPoint(x: 0, y: frame.size.height/5)

Upvotes: 0

Related Questions