Sprite-Kit场景的背景图片

时间:2015-07-09 02:42:19

标签: swift sprite-kit

我现在在Sprite-Kit场景中工作,我想将背景设置为名为" bgimage"例如。我将如何通过gamescene.swift以编程方式执行此操作?

import UIKit
import SpriteKit
import CoreGraphics
class gameScene: SKScene {


}

3 个答案:

答案 0 :(得分:9)

您可以将背景图像声明为SKSpriteNode,将其位置设置为屏幕中间并将其添加到场景中。

import UIKit
import SpriteKit

class gameScene: SKScene {
    var background = SKSpriteNode(imageNamed: "bgimage")

    override func didMoveToView(view: SKView) {
        background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)
        addChild(background)
    }
}

还要确保在images.xcassets中有图像。

答案 1 :(得分:3)

import UIKit
import SpriteKit
import CoreGraphics
class gameScene: SKScene {

var background = SKSpriteNode(imageNamed: "bgimage")

override func didMoveToView(to view: SKView) {
background.zPosition = 1
background.position = CGPoint(x: frame.size.width / 2, y: frame.size.height / 2)

addChild(background)


}
}

zPosition应该低于gameScene中所有其他节点

答案 2 :(得分:0)

全尺寸图片背景

override func didMove(to view: SKView) {

    super.didMove(to:view)

    DispatchQueue.main.async{

        self.background.zPosition = 0
        self.background.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
        self.background.size = CGSize(width: self.size.width, height: self.size.height)
        self.addChild(self.background)

    }
}