Я хотел бы обнаружить коллизию между двумя SKShapeNotes, но я не могу найти ошибку в моем коде.
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")
}
}
}
}