我如何将碰撞检测设置到我想要的位置?

how do i set collision detection to where i want it?

我正在制作一个球(家伙)必须穿过旋转圆圈(圆圈)中的开口并击中目标的游戏。我将碰撞检测设置为旋转的圆圈,然后我尝试让球(人)通过它而不是通过开口。我知道这与碰撞检测有关。我也将 circleOfRadius 设置为半径,但我似乎无法为球通过打开一个开口。如果你能帮忙,我很乐意听取你的意见。这是我的代码,它不是全部,它只是重要的。

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    var circle  = SKSpriteNode()
    var guy     = SKSpriteNode()
    let guyCategory     :UInt32 = 0x1 << 0
    let circleCategory  :UInt32 = 0x1 << 1

    override func didMove(to view: SKView) {


        self.physicsWorld.contactDelegate = self

        scene?.anchorPoint = CGPoint(x: 0, y: 0)
        backgroundColor = UIColor.lightGray

        circle = SKSpriteNode(imageNamed: "Circle1")
        circle.position = CGPoint(x: self.frame.size.width / 2, y: 
        self.frame.size.height / 2)
        circle.physicsBody = SKPhysicsBody(circleOfRadius: 298 )
        circle.physicsBody?.isDynamic = false
        circle.physicsBody?.allowsRotation = true
        circle.physicsBody?.affectedByGravity = false
        circle.run(SKAction.repeatForever(SKAction.rotate(byAngle: CGFloat.pi * 2.0, duration: 7.0)), withKey: "rotatecircle")
self.addChild(circle)

        guy = SKSpriteNode(imageNamed: "Guy")
        guy.position = CGPoint(x: self.frame.size.width/2 , y:40)
        guy.name = "guy"
        guy.physicsBody = SKPhysicsBody(circleOfRadius: guy.size.width/2)
        guy.physicsBody?.isDynamic = false
        guy.physicsBody?.affectedByGravity = false
        self.addChild(guy)

        circle.physicsBody?.categoryBitMask = circleCategory
        circle.physicsBody?.collisionBitMask = targetCategory
        circle.physicsBody?.contactTestBitMask = targetCategory

        guy.physicsBody?.categoryBitMask = guyCategory
        guy.physicsBody?.collisionBitMask = targetCategory
        guy.physicsBody?.contactTestBitMask = targetCategory
    }

    func didBegin(_ contact: SKPhysicsContact) {
        let collision1:UInt32 = contact.bodyA.categoryBitMask

        if collision1 == targetCategory {
            backgroundColor = UIColor.green
        }
        else {
            backgroundColor = UIColor.red
        }
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        self.physicsWorld.gravity = CGVector(dx: 0 ,dy: 10);
        guy.physicsBody = SKPhysicsBody(circleOfRadius: guy.size.width / 2)
    }
    override func update(_ currentTime: TimeInterval) {

    }
}

这里的问题是你用 SKPhysicsBody(circleOfRadius: 298 ) 为你的整个圆分配了一个物理体。这也包括您在圆圈中的孔,因此没有任何东西可以通过。您应该使用 SKPhysicsbody class 的纹理初始化程序来复制圆圈的形状并让孔保持不变。以下内容适合您:

circle = SKSpriteNode(texture: SKTexture(imageNamed: "Circle1"))
circle.physicsbody = SKPhysicsbody(texture: circle.texture!, size: CGSize(width: circle.frame.width, height: circle.frame.height))