微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何检测盒子物体和球之间的碰撞

如何解决如何检测盒子物体和球之间的碰撞

override func touchesBegan(_ touches: Set<UITouch>,with event: UIEvent?) {
    
    guard let touch = touches.first else { return }
    
    let location = touch.location(in: self) // locates touches on the screen
    
    let objects = nodes(at: location) // shows all objects were screen was touched
    
    if objects.contains(editLabel) { // checking if appropriate array of objects contains the editLabel
        editingMode.toggle()
        
        
        
    } else {
        if editingMode {
            let size = CGSize(width: Int.random(in: 16...128),height: 16)
            let Box = SKSpriteNode(color: UIColor(red: CGFloat.random(in: 0...1),green: CGFloat.random(in: 0...1),blue: CGFloat.random(in: 0...1),alpha: 1),size: size)
            Box.zRotation = CGFloat.random(in: 0...3)
            Box.position = location // add the Box ob ject were the user taps
            Box.name = "Box"
            
            Box.physicsBody = SKPhysicsBody(rectangleOf: Box.size)
            Box.physicsBody!.contactTestBitMask = Box.physicsBody!.collisionBitMask
            Box.physicsBody?.isDynamic = false // Box object will not move when impact happens
            addChild(Box) // Box object will be added to the screen
            
        } else if usedBalls != 0 {
            let ball = SKSpriteNode(imageNamed: allBalls.randomElement() ?? "ballRed.png") // pulls out the red ball from app bundle
            ball.physicsBody = SKPhysicsBody(circleOfRadius: ball.size.width / 2.0) // animates the ball object
            ball.physicsBody!.contactTestBitMask = ball.physicsBody!.collisionBitMask
            ball.physicsBody?.restitution = 0.4 // determines the ball object bounciness
            ball.position = location // ball will appear where the user tapped on the screen
            ball.name = "ball"
 // ball.position = CGPoint (x: 550,y: 700)
 addChild (ball) // adds the ball object to the screen
 usedBalls -= 1
            
        }
    }
    
}



func makeBouncer(at position: CGPoint) {
let bouncer = SKSpriteNode(imageNamed: "bouncer.png")
bouncer.position = position
bouncer.physicsBody = SKPhysicsBody(circleOfRadius: bouncer.size.width / 2.0)
bouncer.physicsBody?.isDynamic = false // bouncer object is fixed to the bottom of the screen
addChild(bouncer)

}

func makeSlot(at position: CGPoint,isGood: Bool) {
    var slotBase: SKSpriteNode
    var slotGlow: SKSpriteNode
    
    if isGood {
        slotBase = SKSpriteNode(imageNamed: "slotBaseGood.png")
        slotGlow = SKSpriteNode(imageNamed: "slotGlowGood.png")
        slotBase.name = "good"
        
    } else {
        slotBase = SKSpriteNode(imageNamed: "slotBaseBad.png")
        slotGlow = SKSpriteNode(imageNamed: "slotGlowBad.png")
        slotBase.name = "bad"
    }
    
    slotBase.position = position
    slotGlow.position = position
    
    slotBase.physicsBody = SKPhysicsBody(rectangleOf: slotBase.size)
    slotBase.physicsBody?.isDynamic = false
    
    addChild(slotBase)
    addChild(slotGlow)
    
    let spin = SKAction.rotate(byAngle: .pi,duration: 10)
    let spinForever = SKAction.repeatForever(spin)
    slotGlow.run(spinForever)
}

func collisionBetween(ball: SKNode,object: SKNode) {
    if object.name == "good" { // green slot
        destroy(ball: ball)
        score += 1
        usedBalls += 1
    } else if object.name == "bad" { // red slot
        destroy(ball: ball) // the ball will be removed once drops into a green or red slot
        score -= 1
        
    }
    
}


func BoxandballCollision(Box: SKNode,ball: SKNode) {
    if ball.name == "ball" {
    destroyObject(Box: Box)
        
    }
}


func destroyObject(Box: SKNode) {
    if let fireParticles = SKEmitterNode(fileNamed: "FireParticles") {
        fireParticles.position = Box.position
        addChild(fireParticles)
    }

    Box.removeFromParent() // Box should be removed when a ball will hit it
}


func destroy(ball: SKNode) {
    if let fireParticles = SKEmitterNode(fileNamed: "FireParticles") {
        fireParticles.position = ball.position
        addChild(fireParticles)
    }
    
    ball.removeFromParent() // ball object is removed from scene when hits a slot

}

func didBegin(_ contact: SKPhysicsContact) {
    guard let nodeA = contact.bodyA.node else { return }
    guard let nodeB = contact.bodyB.node else { return }
    
    if nodeA.name == "ball" {
        collisionBetween(ball: nodeA,object: nodeB)
    } else if nodeB.name == "ball" {
        collisionBetween(ball: nodeB,object: nodeA)
        
        }
    }
    
func begin(_ contact: SKPhysicsContact) {
    guard let nodeA = contact.bodyA.node else { return }
    guard let nodeB = contact.bodyB.node else { return }
    
    if nodeA.name == "Box" {
        BoxandballCollision(Box: nodeA,ball: nodeB)
    } else if nodeB.name == "Box" {
        BoxandballCollision(Box: nodeB,ball: nodeA)
      }
   }

我想在球击中时移除任何盒子对象,这只是一个用于学习目的的简单游戏,但我正在努力解决它。我对 Box 对象使用了完全相同的方法,这可以获取有关每次碰撞的通知Box.physicsBody!.contactTestBitMask = Box.physicsBody!.collisionBitMask”。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。