【问题标题】:Measuring collision between SKShapeNotes测量 SKShapeNotes 之间的碰撞
【发布时间】:2019-02-10 13:47:18
【问题描述】:

我想检测两个 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")
        }
    }
}
}

【问题讨论】:

    标签: sprite-kit swift4 skphysicsbody skshapenode


    【解决方案1】:

    您还需要定义contactTestBitMask 以通过SKPhysicsContactDelegate 获得通知。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-07
      • 2018-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多