如何在SpriteKit中检测联系人

问题描述 投票:0回答:1

我想让一个联络人通过 SpriteKit 在Swift 4中。我将一个翻转器设置为某个类别,一个球设置为另一个类别。然后,我将它们的碰撞和接触掩码设置为彼此,我添加一个 contactDelegatedidMove 到,并打印 "contact",以告知有一个联系人在一个 didEnterFonction. 然而,我根本无法检测到任何接触。即使按照所有的教程,并在类似的问题中搜索,我也不明白我做错了什么。我不明白我做错了什么。我的最终目标是让球在击中翻转器时停止下落,但我试图在第一时间检测到任何接触,而这是行不通的。

import SpriteKit
import GameplayKit

struct PhysicsCategory {
  static let none      : UInt32 = 0
  static let all       : UInt32 = UInt32.max
  static let flip   : UInt32 = 0b1
  static let ball: UInt32 = 0b10  // 2
}
class GameScene: SKScene, SKPhysicsContactDelegate {

var entities = [GKEntity]()
var graphs = [String : GKGraph]()

private var lastUpdateTime : TimeInterval = 0
private var label : SKLabelNode?
private var flip1: SKSpriteNode?
private var flip2: SKSpriteNode?
private var spinnyNode : SKShapeNode?
private var balle:SKSpriteNode?

override func sceneDidLoad() {

    self.lastUpdateTime = 0
    self.flip1 = self.childNode(withName:"//flip1") as? SKSpriteNode
    self.flip2 = self.childNode(withName: "//flip2") as? SKSpriteNode
    self.balle = self.childNode(withName: "//ball") as?SKSpriteNode
    self.flip1?.physicsBody = SKPhysicsBody()
    self.flip1?.physicsBody?.affectedByGravity = false
    self.flip1?.physicsBody?.allowsRotation = true
    self.balle?.physicsBody = SKPhysicsBody();
    //self.balle?.physicsBody?.affectedByGravity = true
    self.flip2?.physicsBody = SKPhysicsBody()
    self.flip2?.physicsBody?.affectedByGravity = false
    self.flip2?.physicsBody?.allowsRotation = true
    self.balle?.physicsBody?.allContactedBodies()
    self.flip1?.physicsBody?.usesPreciseCollisionDetection
        = true
    self.flip1?.physicsBody?.categoryBitMask = PhysicsCategory.flip
    self.flip1?.physicsBody?.contactTestBitMask = PhysicsCategory.ball
    self.flip1?.physicsBody?.collisionBitMask = PhysicsCategory.ball

    self.flip2?.physicsBody?.categoryBitMask = PhysicsCategory.flip
    self.flip2?.physicsBody?.contactTestBitMask = PhysicsCategory.ball
    self.flip2?.physicsBody?.collisionBitMask = PhysicsCategory.ball


    // Get label node from scene and store it for use later
    self.label = self.childNode(withName: "//helloLabel") as? SKLabelNode
    if let label = self.label {
        label.alpha = 0.0
        label.run(SKAction.fadeIn(withDuration: 2.0))
    }

    // Create shape node to use during mouse interaction
    let w = (self.size.width + self.size.height) * 0.05
    self.spinnyNode = SKShapeNode.init(rectOf: CGSize.init(width: w, height: w), cornerRadius: w * 0.3)

    if let spinnyNode = self.spinnyNode {
        spinnyNode.lineWidth = 2.5

        spinnyNode.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat(Double.pi), duration: 1)))
        spinnyNode.run(SKAction.sequence([SKAction.wait(forDuration: 0.5),
                                          SKAction.fadeOut(withDuration: 0.5),
                                          SKAction.removeFromParent()]))
    }
}
override func keyDown(with event: NSEvent) {
    switch event.keyCode {
    case 0x31:
        if let label = self.label {
            label.run(SKAction.init(named: "Pulse")!, withKey: "fadeInOut")
        }
    case 123:
        print("left")
        self.flip1?.run(SKAction.rotate(byAngle: -1.5, duration: 0.2))
       // self.flip1?.physicsBody?.applyAngularImpulse(600)
        self.flip1?.run(SKAction.rotate(byAngle: 1.5, duration: 0.1))
    case 124:
      self.flip2?.run(SKAction.rotate(byAngle: 1.5, duration: 0.2))
        self.flip2?.run(SKAction.rotate(byAngle: -1.5, duration: 0.1))
    case 15:
        self.balle?.run(SKAction.move(to: CGPoint(x: 50,y: 50), duration: 1))
    default:
        print("keyDown: \(event.characters!) keyCode: \(event.keyCode)")
    }
}





override func update(_ currentTime: TimeInterval) {
    // Called before each frame is rendered

    // Initialize _lastUpdateTime if it has not already been
    if (self.lastUpdateTime == 0) {
        self.lastUpdateTime = currentTime
    }

    // Calculate time since last update
    let dt = currentTime - self.lastUpdateTime

    // Update entities
    for entity in self.entities {
        entity.update(deltaTime: dt)
    }

    self.lastUpdateTime = currentTime
}
override func didMove(to view: SKView) {
    physicsWorld.contactDelegate = self
}
func didEnter(_ contact: SKPhysicsContact) {
    print("contact")

    }
}
swift sprite-kit
1个回答
0
投票

你没有给物理体一个形状或大小。看一下 此处 并阅读'从_____中创建一个体'部分。

© www.soinside.com 2019 - 2024. All rights reserved.