在 swift 中的另一个 classes 初始值设定项中使用 class(或实例)属性
Using a class (or instance) property inside of another classes initilizer in swift
我对 Swift 比较陌生,我想知道是否有办法在单独的 class 中引用 class 的 属性 ] 初始值设定项?例如:如果我有一个 class Person
和 属性 position
,有没有办法初始化 Pants
class 这样它position
和 Person
的一样吗?这是我的代码:
class Pants:SKSpriteNode{
init(){
let pants = SKTexture(imageNamed: "Sprites/pants.jpg")
pants.setScale(0.5)
super.init(texture: pants, color: UIColor.clearColor(), size: pants.size())
//self.position.x = aPerson.position.x + (aPerson.size.width / 2)
//self.position.y = aPerson.position.y - (aPerson.size.height * 0.04)
self.position = Person.getPos()//CGPoint(x: 200,y: 200)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
}
起初我尝试引用 aPerson
,它是 Person
的一个实例,但我收到错误:Instance member aPerson cannot be used on type GameScene
。我想明白为什么在这种情况下引用一个实例没有多大意义——因为在引用时该实例可能不存在?但我真的不知道这个错误消息是什么意思——任何澄清都会很好。然后我想在 Person
class 中使用 static
getter 方法,它刚刚返回它的位置 属性。这似乎也行不通。任何建议都会很棒!
一个解决方案是向您的初始化程序添加一个参数(正如 Paul Griffiths 在上面的评论中所建议的):
class Pants: SKSpriteNode {
init(aPerson: Person) {
let pants = SKTexture(imageNamed: "Sprites/pants.jpg")
pants.setScale(0.5)
super.init(texture: pants, color: UIColor.clearColor(), size: pants.size())
self.position.x = aPerson.position.x + (aPerson.size.width / 2)
self.position.y = aPerson.position.y - (aPerson.size.height * 0.04)
self.position = aPerson.getPos()//CGPoint(x: 200,y: 200)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
那么无论你想在哪里创建一个Pants
实例,你都必须通过一个人:
let somePerson = Person()
let pants = Pants(aPerson: somePerson)
我假设裤子是人穿的?所以相反,工作是相对的,而不是绝对的。
让Pants成为person的子节点,那么你只需要关心Person的中心到Pant线的距离。如果这将始终是一个常数(例如中心下方 10 个像素),则对其进行硬编码。如果裤线发生变化,则像@Santa Claus 建议的那样传入裤线
====请在这里假设一些代码======
person.pantline = -10;
person.addChild(Pants(pantline:person.pantline))
=====================================
class Pants: SKSpriteNode {
convenience init(pantline: Int) {
self.init(imageNamed: "Sprites/pants.jpg")
self.setScale(0.5) //Why?
self.anchorPoint = CGPointMake(0.5,1)
self.position.y = pantline
}
required init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
}
override init (texture: SKTexture, color: UIColor, size: CGSize)
{
super.init(texture: texture, color: color, size: size)
}
}
我对 Swift 比较陌生,我想知道是否有办法在单独的 class 中引用 class 的 属性 ] 初始值设定项?例如:如果我有一个 class Person
和 属性 position
,有没有办法初始化 Pants
class 这样它position
和 Person
的一样吗?这是我的代码:
class Pants:SKSpriteNode{
init(){
let pants = SKTexture(imageNamed: "Sprites/pants.jpg")
pants.setScale(0.5)
super.init(texture: pants, color: UIColor.clearColor(), size: pants.size())
//self.position.x = aPerson.position.x + (aPerson.size.width / 2)
//self.position.y = aPerson.position.y - (aPerson.size.height * 0.04)
self.position = Person.getPos()//CGPoint(x: 200,y: 200)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
}
起初我尝试引用 aPerson
,它是 Person
的一个实例,但我收到错误:Instance member aPerson cannot be used on type GameScene
。我想明白为什么在这种情况下引用一个实例没有多大意义——因为在引用时该实例可能不存在?但我真的不知道这个错误消息是什么意思——任何澄清都会很好。然后我想在 Person
class 中使用 static
getter 方法,它刚刚返回它的位置 属性。这似乎也行不通。任何建议都会很棒!
一个解决方案是向您的初始化程序添加一个参数(正如 Paul Griffiths 在上面的评论中所建议的):
class Pants: SKSpriteNode {
init(aPerson: Person) {
let pants = SKTexture(imageNamed: "Sprites/pants.jpg")
pants.setScale(0.5)
super.init(texture: pants, color: UIColor.clearColor(), size: pants.size())
self.position.x = aPerson.position.x + (aPerson.size.width / 2)
self.position.y = aPerson.position.y - (aPerson.size.height * 0.04)
self.position = aPerson.getPos()//CGPoint(x: 200,y: 200)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
那么无论你想在哪里创建一个Pants
实例,你都必须通过一个人:
let somePerson = Person()
let pants = Pants(aPerson: somePerson)
我假设裤子是人穿的?所以相反,工作是相对的,而不是绝对的。
让Pants成为person的子节点,那么你只需要关心Person的中心到Pant线的距离。如果这将始终是一个常数(例如中心下方 10 个像素),则对其进行硬编码。如果裤线发生变化,则像@Santa Claus 建议的那样传入裤线
====请在这里假设一些代码======
person.pantline = -10;
person.addChild(Pants(pantline:person.pantline))
=====================================
class Pants: SKSpriteNode {
convenience init(pantline: Int) {
self.init(imageNamed: "Sprites/pants.jpg")
self.setScale(0.5) //Why?
self.anchorPoint = CGPointMake(0.5,1)
self.position.y = pantline
}
required init?(coder aDecoder: NSCoder) {
super.init(coder:aDecoder)
}
override init (texture: SKTexture, color: UIColor, size: CGSize)
{
super.init(texture: texture, color: color, size: size)
}
}