Class 纹理改变,但不显示

Class textures change, but do not show up

在我正在制作的程序中,我想尝试使用两种具有不同纹理的不同片段,它们具有相同的 class。

由于这个 class 是子 class,我必须将纹理设置为 super.init 中的默认纹理。我确实更改了纹理,但是当程序为 运行.

时仅显示默认纹理

我试过打印纹理,它说纹理已更改。这是怎么回事?

注意:我使用方格棋子作为站立棋子。红色方格棋子是一体的(也是红队的)。图片应显示黑色方格

这是问题所在:

这是打印出来的。

这是重生和步枪手的代码Class:

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")
        }
    }

}

您必须在子类 "riflemen" 中初始化 "texture" 才能使纹理可变。

例如

.....

class rifleman: Character, pTargetable{
    var health = 10
    init(texture: SKTexture) {
        super.init(tag: 0, team: "generic", currentAction: 0, texture: texture)

.....