I tried to add an image to my Game Scene, but it won't appear. Below is my code. I hope you guys can help me
import SpriteKit
import GameplayKit
class GameScene: SKScene {
var Ground = SKSpriteNode()
var iceCream = SKSpriteNode()
override func didMove(to view: SKView) {
Ground = SKSpriteNode(imageNamed: "background")
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width / 2, y: self.Ground.frame.height / 2)
self.addChild(Ground)
iceCream = SKSpriteNode(imageNamed: "VanillaIceCream")
iceCream.size = CGSize(width: 60, height: 70)
iceCream.position = CGPoint(x: self.iceCream.frame.width, y: self.frame.height / 2)
self.addChild(iceCream)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
}
override func update(_ currentTime: CFTimeInterval) {
}
}
When you set position
tox: self.iceCream.frame.width
you position it using iceCream
.
I believe that changing it to x: self.frame.width / 2
will solve it.
If it is not mistake and you want to set the iceCream
on the left of the screen, position the sprite
after adding it to self.
It's also good to set zPosition
to each element, to make sure it will not hide behind the background
image.
EDIT: Here is the full code for your method (ready to copy/paste):
Ground = SKSpriteNode(imageNamed: "background")
self.addChild(Ground)
Ground.setScale(0.5)
Ground.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
Ground.zPosition = 1.0
iceCream = SKSpriteNode(imageNamed: "VanillaIceCream")
self.addChild(iceCream)
iceCream.size = CGSize(width: 60, height: 70)
iceCream.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2)
iceCream.zPosition = 2.0