set SKNode position in spriteKit - swift

how works constraints in spriteKit? I have a global var
let screenSize: CGRect = UIScreen.main.bounds
then, i am trying to add my sknode.
personage = SKSpriteNode(imageNamed: "Bro")
personage.position.y = CGFloat(-screenSize.height)
personage.position.x = CGFloat(-screenSize.width)
personage.size = CGSize(width: screenSize.width/3, height: screenSize.height/3)
addChild(personage)
It is perfect on small size iphone, but when i open 13 pro max i cant see my personage, whats wrong? How do i change my code to see the same sizes of personage on all devices

Related

Sprite Kit iOS 11 | Place nodes only in safe area

I want to program a simple Sprite Kit game in which I have to place some nodes randomly somewhere in the scene.
To ensure that these nodes are not placed outside the safe area I programmed this function:
func updatesafearea() {
let frame = self.size
let sets = view!.safeAreaInsets
let rect = CGRect(x: -(frame.width/2)-sets.left,
y: -(frame.height/2)-sets.top,
width: frame.width-sets.right-sets.left,
height: frame.height-sets.top-sets.bottom)
//Visualize the area
let node = SKShapeNode(rect: rect)
node.fillColor = .red
addChild(node)
}
This should be a rect in which all nodes should be presented safely, but unfortunately this doesn't work on iPhone X (Simulator):
iPhone 8 and iPhone X simulator screenshot below:

SKLabelNode extremely blurry [screenshot included]

I'm trying to add a label to an ARKit project, but it's rendering extremely blurry. See image below:
Here's my code:
let shapeNode = SKShapeNode(rectOf: CGSize(width: 30, height: 30))
shapeNode.name = "bar"
shapeNode.fillColor = UIColor.white
let labelNode = SKLabelNode(text: "Hello world")
labelNode.horizontalAlignmentMode = .left
labelNode.verticalAlignmentMode = .top
labelNode.fontColor = UIColor.black
labelNode.fontSize = 3
When you create a SKScene for display, you have to give it a size. This is the resolution of what will be rendered. It will then be scaled to the SKSceneView it appears in, according to how you set its scaleMode property. If the resolution of your SKScene is lower than the point size of the view it appears in, the output will be adjusted to fit using a standard scaling algorithm and will therefore be blurry.
Try increasing the size of your SKScene by a little bit and see if that helps. Note that you will likely also have to adjust the size and position of your nodes as these will appear to shrink as the scene gets larger.

Why are the positions of my nodes different on every device?

Ok, so I've been trying to position a line on my iPhone 6s+ and my iPad 2. The line was added in an SKCamera (I don't know if that effects it or not). Here is the code for the line:
var leftWall = SKSpriteNode()
leftWall.size = CGSize(width: 1, height: 10000)
leftWall.position = CGPoint(x: 0, y: 0)
leftWall.color = UIColor.red
leftWall.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: leftWall.size.width, height: leftWall.size.height))
leftWall.physicsBody?.affectedByGravity = false
leftWall.physicsBody?.isDynamic = false
leftWall.physicsBody?.categoryBitMask = groundGroup
leftWall.physicsBody?.collisionBitMask = ballGroup
leftWall.physicsBody?.contactTestBitMask = 0
theCamera.addChild(leftWall)
Okay, so the x position is 0,0 right? Here is how it looks on my iPhone 6s+:
As you see, the line is in the middle?
Here is how it looks on my iPad 2:
I just don't understand why the position of the line is completely different.. Please help me!
You are not taking into account the cropping that happens on the iPad, or your scaleMode is all wrong
What you need to do is set all nodes anchor points (Including the scene) to (0.5,0.5), make sure your GameScene has a static size and not based on view.frame (750,1334 is a good one) and that your scale mode is set to .aspectFill. This makes (0,0) the center of the screen, and you will notice everything line up on all devices

How to scale to be at the same distance in all devices?

I'm having problems opening my game in different divices , in the 6s iphone plus looks much bigger the circle the center, also the small circle that is on the line changes position , I would like that the center circle was the same size and that the small circle always this half on the line.
import SpriteKit
struct Circle {
var position:CGPoint
var radius:CGFloat
}
class GameScene: SKScene {
let node = SKNode()
let sprite = SKShapeNode(circleOfRadius: 6)
var rotation:CGFloat = CGFloat(M_PI)
var circles:[Circle] = []
var circuloFondo = SKSpriteNode()
var orbita = SKSpriteNode()
let padding2:CGFloat = 26.0
let padding3:CGFloat = 33.5
let padding5:CGFloat = 285.5
var circulo = SKSpriteNode()
override func didMoveToView(view: SKView) {
scaleMode = .ResizeFill
backgroundColor = UIColor(red: 0.3, green: 0.65, blue: 0.9, alpha: 1)
orbita = SKSpriteNode(imageNamed: "orbita2")
orbita.size = CGSize(width:view.frame.size.width - padding2 , height: view.frame.size.width - padding2)
orbita.color = UIColor.whiteColor()
orbita.colorBlendFactor = 1
orbita.alpha = 1
orbita.position = view.center
self.addChild(orbita)
orbita.zPosition = 3
circuloFondo = SKSpriteNode(imageNamed: "circuloFondo")
circuloFondo.size = CGSize(width:view.frame.size.width - padding5 , height: view.frame.size.width - padding5)
circuloFondo.color = UIColor.whiteColor()
circuloFondo.alpha = 1
circuloFondo.position = view.center
self.addChild(circuloFondo)
circuloFondo.zPosition = 0
let radius1:CGFloat = (view.frame.size.width - padding3)/2 - 1
let radius2:CGFloat = (view.frame.size.width - padding5)/2 + 6.5
circles.append(Circle(position: view.center, radius: radius1))
circles.append(Circle(position: view.center, radius: radius2))
addChild(node)
node.addChild(sprite)
if let circle = nextCircle() {
node.position = circle.position
sprite.fillColor = SKColor.whiteColor()
sprite.zPosition = 4.0
sprite.position = CGPoint(x:circle.radius, y:0)
rotate()
}
You can get the width of the screen like this:
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
and then set elements in your UI to be a proportion of the screenWidth. For instance:
let radius1:CGFloat = screenWidth/4
//this would always give you a radius that is one quarter of the screen width
I've used this method a few times with success, hope it works for you.
Your main problem is your scene mode is set to ResizeFill. This will cause you so many headaches as you will have to do all the scaling yourself, hence the circles are different sizes on different devices. Having scale mode resizeFill will also affect things such as the physics engine or fontSizes which you will need to adjust for on every device.
I would recommend you use scene scale mode .AspectFill with the default scene size of 1024*768 (landscape) or 768*1024 (portrait). This is the same as the xCode default game template.
This way everything will look exactly the same on all iPhones. On iPads there will be slightly more screen space at the top and bottom which you simply cover with your background. The main trick is that you position your stuff from the center.
Furthermore you can use the universal assets in the asset catalogue and everything will look great and not blurry.
The only thing that you might have to adjust for this way is that on iPads you might need to move some buttons up/down if you want them on the top/bottom edge.
I strongly recommend you consider this as I can talk from experience that using scale mode ResizeFill is really bad. I have been through this pain with 2 games before I rewrote them because they were so inconsistent on all devices causing me so many bugs in the process. Lets not talk about the time I wasted testing on all devices, adjusting values until it felt right.
Hope this helps.

Swift random point on screen for SKNode

I am trying to generate a random point on the screen to create an SKSpriteNode:
let px : CGFloat = CGFloat(drand48())
let py : CGFloat = CGFloat(drand48())
let screenSize : CGRect = self.frame
let location = CGPoint(x: screenSize.width*px,y: screenSize.height*py)
let sprite = SKSpriteNode(imageNamed: "BlackCircle.png")
Although this generates points correctly within the frame, sometimes the node itself is not created because the location in the frame does not correctly translate to a location within the GameScene. I tested positions using:
print(touch.locationInNode(self))
print(touch.locationInView(self.view))
I know there should be some offset for the x and y coordinates, but I cannot figure them out. Am I using the wrong CGRect? I tried using:
UIApplication.sharedApplication().keyWindow?.frame
and
view!.frame
as screenSize instead, but I had the same problem. What CGRect should I use?