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
positiontox: self.iceCream.frame.widthyou position it usingiceCream. I believe that changing it tox: self.frame.width / 2will solve it. If it is not mistake and you want to set theiceCreamon the left of the screen, position thespriteafter adding it to self. It's also good to setzPositionto each element, to make sure it will not hide behind thebackgroundimage.EDIT: Here is the full code for your method (ready to copy/paste):