有谁能解释一下如何向gameScene中添加节点吗?
我对我的老板类进行了子类化,但我不知道如何在GameScene上显示我的GameScene
class Boss: GameScene {
var gameScene : GameScene!
var Boss1 = SKSpriteNode(imageNamed: "boss1")
override func didMove(to view: SKView) {
Boss1.position = CGPoint(x: size.width * 0.1, y: size.height * 0.5)
Boss1.zPosition = 2
self.gameScene.addChild(Boss1)
}
}我正在使用4和xcode 9
发布于 2018-03-13 14:29:22
对于这样的实例,您通常不会对场景进行子类化。更有可能的是,您打算让老板成为SKSpriteNode的子类,并将其添加到您的场景中。虽然可能有很多种方法可以子类这个,但这只是一个方法。
还值得注意的是,使变量名大写为大写、类是、变量否是通常不能接受的做法。
class Boss: SKSpriteNode {
init() {
let texture = SKTetxure(imageNamed: "boss1")
super.init(texture: texture , color: .clear, size: texture.size())
zPosition = 2
//any other setup such as zRotation coloring, additional layers, health etc.
}
}...meanwhile回到GameScene
class GameScene: SKScene {
let boss1: Boss!
override func didMove(to view: SKView) {
boss1 = Boss()
boss1.position = CGPoint(x: size.width * 0.1, y: size.height * 0.5)
self.gameScene.addChild(boss1)
}
}在GameScene中,您创建一个新类的实例,并将其定位并添加到GameScene中。
编辑
init函数有一个快捷的捷径。
Boss()是相同的
Boss.init()您还可以向init中添加自定义参数,以便进一步澄清或向类添加更多特性。例如..。
class Boss: SKSpriteNode {
private var health: Int = 0
init(type: Int, health: Int, scale: CGFloat) {
let texture: SKTetxure!
if type == 1 {
texture = SKTetxure(imageNamed: "boss1")
}
else {
texture = SKTetxure(imageNamed: "boss2")
}
super.init(texture: texture , color: .clear, size: texture.size())
self.health = health
self.setScale(scale)
zPosition = 2
//any other setup such as zRotation coloring, additional layers, health etc.
}
}https://stackoverflow.com/questions/49258231
复制相似问题