碰撞后转换自定义SKSpriteNode类

时间:2016-08-06 15:12:34

标签: swift xcode sprite-kit xcode8-beta2

我有几个从SKSpriteNode派生的自定义类,当它们发生碰撞时,我想从自定义“Treat”类调用一个方法,但它会因为错误而影响崩溃。“在打开一个可选项时,意外地发现了Nil值“

这是Treat类...

import UIKit
import SpriteKit

class Treat: SKSpriteNode {

var isActive: Bool!

override init(texture: SKTexture!, color: SKColor, size: CGSize) {
    self.isActive = true

    super.init(texture: texture, color: color, size: size)

    self.texture = SKTexture(imageNamed: "treat1")
    self.zPosition = 3
    self.size = CGSize(width: 25, height: 25)
    self.physicsBody = SKPhysicsBody(rectangleOf: self.size)

    self.physicsBody?.friction = 0.1
    self.physicsBody?.restitution = 0.8
    self.physicsBody?.mass = 0.01
    self.physicsBody?.affectedByGravity = true
    self.physicsBody?.allowsRotation = true
    self.physicsBody?.isDynamic = true
    self.physicsBody?.categoryBitMask = PhysicsCategory.Treat
    self.physicsBody?.contactTestBitMask = PhysicsCategory.Player
    self.physicsBody?.collisionBitMask = PhysicsCategory.Ground
    self.physicsBody?.usesPreciseCollisionDetection = true
    self.name = "Treat";

    let rotateDuration = Double(arc4random()) / 0xFFFFFFFF
    let rotateAction = SKAction.rotate(byAngle: CGFloat(M_PI), duration: rotateDuration)
    let rotateActionRepeatingForever = SKAction.repeatForever(rotateAction)
    self.run(rotateActionRepeatingForever)

}

convenience init(color: SKColor, isActive: Bool = false) {
    let size = CGSize(width:0, height: 0);
    self.init(texture:nil, color: color, size: size)
    self.isActive = isActive
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
}

func throwTreat() {
    NSLog("Throwing Treat")
    let arc4randoMax:Double = 0x100000000
    let upper = 2.0
    let lower = 4.4
    let throwableAngle = Float32((Double(arc4random()) / arc4randoMax) * (upper - lower) + lower)

    self.physicsBody?.applyImpulse(CGVector(dx: CGFloat(-throwableAngle),dy: CGFloat(throwableAngle)))
}

func removeTreat(showEffect: Bool) {
    if self.isActive != false {
        self.isActive = false
        if showEffect != false {

        }
        self.removeFromParent()
    }

}

func onImpactWithPlayer() {
    self.removeFromParent()
}


}

这是物理碰撞检测..

 func didBegin(_ contact: SKPhysicsContact) {

    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    if ((firstBody.categoryBitMask & PhysicsCategory.Player != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.Ground != 0)) {
        self.playerNode.isJumping = false
    }


    if ((firstBody.categoryBitMask & PhysicsCategory.Player != 0) &&
        (secondBody.categoryBitMask & PhysicsCategory.Treat != 0)) {
        levelScore = levelScore + 1
        (secondBody.node as! Treat).onImpactWithPlayer() // This line crashes

    }

}

我是swift和面向对象编程的新手,所以它目前没有多大意义。我知道这是一个简单的解决方案而且我很蠢......

1 个答案:

答案 0 :(得分:0)

您的代码似乎大致正确。

我相信这里发生了什么

当2个物理实体发生碰撞时,didBegin(_ contact: SKPhysicsContact)可以在同一帧上called several times

从这里

(secondBody.node as! Treat).onImpactWithPlayer()

您要从场景图中删除Treat,下次在同一帧上调用didBegin(_ contact:)时会因为节点已被删除而崩溃。

解决方案

只需替换此

(secondBody.node as! Treat).onImpactWithPlayer()

用这个

 (secondBody.node as? Treat)?.onImpactWithPlayer()