在我制作的程序中,我试图让两种不同纹理的作品属于同一类。
由于这个类是一个子类,我必须将纹理设置为super.init中的默认纹理。我确实改变了纹理,但是在程序运行时只显示默认纹理。
我试图打印纹理,并说纹理已经改变。发生了什么事?
注意:我正在使用棋子作为支架。红色的棋子是一体的(和红队的一个)。图像应显示黑色检查件
问题在于:
这是打印输出。
以下是产卵和步枪兵类的代码:
func spawnBlueRiflemen(at: CGPoint) {
let newBlueRifle = rifleman()
newBlueRifle.texture = textureBlueRifle
newBlueRifle.position = at
newBlueRifle.team = "Blue"
print("\(newBlueRifle.texture)")
self.addChild(newBlueRifle)
}
class rifleman: Character, pTargetable{
var health = 10
init() {
super.init(tag: 0, team: "generic", currentAction: 0, texture: textureRedRifle)
var xSize = texture.size().width // Create The texture for the top ( visible sprite )
var ySize = texture.size().height
var size = CGSize(width: xSize, height: ySize)
self.physicsBody = SKPhysicsBody(texture: texture, size: size)
self.physicsBody?.isDynamic = false
self.physicsBody?.affectedByGravity = false // ( physical body stuff )
self.physicsBody?.mass = 1.0
self.name = "\(tag)"
var top = SKSpriteNode(texture: texture, size: size)
top.zPosition = layers.characters
top.color = SKColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
top.colorBlendFactor = 1.0
self.addChild(top)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func takeDamage(damage: Int) {
health -= damage
print("\(tag) lost \(damage) hit points")
if health <= 0 {
die()
print("\(tag) is dead now")
}
}
}
答案 0 :(得分:0)
你必须初始化&#34;纹理&#34;在子类内部&#34;步枪兵&#34;使纹理可以改变。
E.g。
...
class rifleman: Character, pTargetable{
var health = 10
init(texture: SKTexture) {
super.init(tag: 0, team: "generic", currentAction: 0, texture: texture)
...