我想检测两个SKShapeNotes之间的冲突,但是在代码中找不到该bug。
import SpriteKit
import GameplayKit
class GameScene: SKScene {
struct PhysicsCategory {
static let Player: UInt32 = 1
static let Obstacle: UInt32 = 2
static let Edge: UInt32 = 4
}
let player = SKShapeNode(circleOfRadius: 40)
let colors = [UIColor.white, UIColor.clear, UIColor.white, UIColor.clear]
override func didMove(to view: SKView) {
setupPlayerAndCircle()
let playerBody = SKPhysicsBody(circleOfRadius: 30)
playerBody.mass = 1.5
playerBody.categoryBitMask = PhysicsCategory.Player
playerBody.collisionBitMask = 4
player.physicsBody = playerBody
let ledge = SKNode()
ledge.position = CGPoint(x: frame.midX, y: frame.midY - 450)
let ledgeBody = SKPhysicsBody(rectangleOf: CGSize(width: frame.width, height: 10))
ledgeBody.isDynamic = false
ledgeBody.categoryBitMask = PhysicsCategory.Edge
ledge.physicsBody = ledgeBody
self.addChild(ledge)
physicsWorld.gravity.dy = -22
physicsWorld.contactDelegate = self
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
player.physicsBody?.velocity.dy = 650.0
}
func setupPlayerAndCircle() {
addCircle()
addPlayer()
}
func addCircle() {
let path = UIBezierPath()
path.addArc(withCenter: CGPoint.zero,
radius: 160,
startAngle: CGFloat(100),
endAngle: CGFloat(0),
clockwise: true)
path.addArc(withCenter: CGPoint.zero,
radius: 200,
startAngle: CGFloat(0.0),
endAngle: CGFloat(100),
clockwise: false)
let section = SKShapeNode(path: path.cgPath)
section.position = CGPoint(x: frame.midX, y: frame.midY)
section.fillColor = .white
section.strokeColor = .white
addChild(section)
let rotateAction = SKAction.rotate(byAngle: 45, duration: 15.0)
section.run(SKAction.repeatForever(rotateAction))
}
func addPlayer() {
player.fillColor = .white
player.strokeColor = .white
player.position = CGPoint(x: frame.midX, y: frame.midY - 440)
addChild(player)
}
}
扩展程序应该检测到冲突。
extension GameScene: SKPhysicsContactDelegate {
func didBegin(_ contact: SKPhysicsContact) {
if let nodeA = contact.bodyA.node as? SKShapeNode, let nodeB = contact.bodyB.node as? SKShapeNode {
if nodeA.strokeColor == nodeB.fillColor {
print("Got Hit")
}
}
}
}
答案 0 :(得分:0)
您还需要定义contactTestBitMask
才能通过SKPhysicsContactDelegate
接收通知。