为什么不能使用SKShapenode的子类?

时间:2017-01-26 17:20:10

标签: swift inheritance sprite-kit

我在这里使用了一个名为Enemy的子类,它继承自SKShapeNode,这里的问题是当我检查bulletenemy之间的碰撞时,我抓住敌人&# 39; s health它说found nil while unwrapping

这是我的代码:

private func drawEnemy() {
        let (color, edge) = randomColorandEdge()
        let nextEnemyPosition = determineNextEnemyPosition(edge: Int(edge))
        let enemyPosition = spawnAtRandomPosition(edge: nextEnemyPosition)
        let enemyCircle = Enemy(scene: self, color: color, radius: globalRadius, position: enemyPosition)
        let enemy = enemyCircle.drawEnemy()
        world.addChild(enemy)
        numberOfEnemies += 1
        enemy.name = String(numberOfEnemies)
        runToCenter(enemy: enemy)
    }

func didBegin(_ contact: SKPhysicsContact) {
if (contact.bodyB.categoryBitMask == bodyType.bullet.rawValue) && (contact.bodyA.categoryBitMask == bodyType.enemy.rawValue) {
            let hitter = contact.bodyB.node as? SKShapeNode
            let enemy = contact.bodyA.node as? Enemy
            hitter?.removeFromParent()
            let health = enemy?.getHealth()
            if health! > 0 {
                enemy?.setHealth(amount: health! - 1)
            }else {
                enemy?.removeFromParent()
            }
        }
    }

这是我的Enemy课程:

class Enemy: SKShapeNode {

    private var health: Int = 5
    private var color: SKColor!
    private var radius: CGFloat!
    private var s: SKScene!
    private var pos: CGPoint!
    private var enemy: SKShapeNode!

    init(scene: SKScene, color: SKColor, radius: CGFloat, position: CGPoint) {
        self.color = color
        self.radius = radius
        self.health = 5
        self.s = scene
        self.pos = position
        super.init()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setHealth(amount: Int) {
        self.health = amount
    }

    func getHealth() -> Int {
        return self.health
    }

    func drawEnemy() -> SKShapeNode {
        enemy = SKShapeNode(circleOfRadius: radius)
        enemy.fillColor = color
        enemy.glowWidth = 0.5
        enemy.strokeColor = color
        enemy.physicsBody = SKPhysicsBody(circleOfRadius: radius)
        enemy.physicsBody?.affectedByGravity = false
        enemy.physicsBody?.isDynamic = true
        enemy.physicsBody?.collisionBitMask = GameScene.bodyType.player.rawValue
        enemy.physicsBody?.categoryBitMask = GameScene.bodyType.enemy.rawValue
        enemy.physicsBody?.contactTestBitMask = GameScene.bodyType.player.rawValue
        enemy.physicsBody?.usesPreciseCollisionDetection = true
        enemy.position = pos
        return enemy
    }

}

当我在health函数

中检查0是否比didBegin更重要时,会出现错误

1 个答案:

答案 0 :(得分:4)

您没有正确地进行子类化,您将Enemy对象用作工厂和实例,将2分开。

厂:

public class EnemyFactory
{
    public func createEnemy(color: SKColor, radius: CGFloat, position: CGPoint) -> Enemy{
        let enemy = Enemy(circleOfRadius: radius)
        enemy.setup(color:color, radius: radius, position: position)
        return enemy
    }
}

实例类:

public class Enemy: SKShapeNode {
    static let Factory = EnemyFactory()
    public var health: Int = 5

    func setup (color: SKColor, radius: CGFloat, position: CGPoint) {
        self.position = position
        fillColor = color
        glowWidth = 0.5
        self.health = 5
        strokeColor = color
        let physicsBody = SKPhysicsBody(circleOfRadius: radius)
        physicsBody.affectedByGravity = false
        physicsBody.isDynamic = true
        physicsBody.collisionBitMask = GameScene.bodyType.player.rawValue
        physicsBody.categoryBitMask = GameScene.bodyType.enemy.rawValue
        physicsBody.contactTestBitMask = GameScene.bodyType.player.rawValue
        physicsBody.usesPreciseCollisionDetection = true
        self.physicsBody = physicsBody
    }
    override init(){
        super.init()

    }

    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

我采取了一些自由:我删除了局部冗余变量,如颜色和场景,你可以直接从对象中获取这些,不需要另外一个变量来保存它们。你也不需要getter和setter,Swift已经为你做了这件事。