我一直在写一个游戏,它有一个SKSpriteNode的子类,有一些额外的函数和变量。我想在创建对象时设置一些变量,例如
let mySprite = MySubclass (width: 24, height 33)
我不确定这是否可能,这意味着我可能不得不调用子类的 methos 才能将 var 设置在一个单独的阶段,这有点笨拙:
let mySprite = MySubclass ()
mySprite.setSize(24, height: 33)
有什么想法可以以更优雅的方式做到这一点吗?
非常感谢,千瓦
这是
非常基本的OOP。以下是你在 Swift 中操作的方法:
class MySubClass: SKSpriteNode {
var width: CGFloat // Declare your own properties
var height: CGFloat // ...
init(width: CGFloat, height: CGFloat) {
self.width = width // set up your own properties
self.height = height // ...
super.init() // call up to the super-class's init to set up its properties
}
}
你读过苹果的书《The Swift Programming Language》吗?它是免费的,清楚地涵盖了这个...