我有几个从 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
}
}
我是快速和面向对象编程的新手,所以目前它没有多大意义。我知道这是一个简单的解决方法,我很愚蠢......