在下面的代码中,我有几个节点,但是现在我只是想检测玩家和敌人子弹之间的碰撞(我将其称为“ BBullet”),但是在检测碰撞的功能下,什么都没有弹出
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
var player = SKSpriteNode()
var firebutton = SKShapeNode()
var bullet = SKSpriteNode()
enum CategoryMask : UInt32 {
case player = 1
case GBullet = 2
case BBullet = 3
case enemyships = 4
}
override func didMove(to view: SKView) {
player = SKSpriteNode(color: .red, size: CGSize(width: 150, height: 150))
player.physicsBody?.affectedByGravity = false
player.position = CGPoint(x: 100, y: 375)
player.physicsBody?.isDynamic = true
player.physicsBody?.restitution = 1
player.physicsBody?.categoryBitMask = CategoryMask.player.rawValue
player.physicsBody?.collisionBitMask = CategoryMask.BBullet.rawValue
player.physicsBody?.contactTestBitMask = CategoryMask.BBullet.rawValue
player.name = "Player"
addChild(player)
firebutton = SKShapeNode(circleOfRadius: 75)
firebutton.position = CGPoint(x: 1000, y: 150)
firebutton.alpha = 0.7
firebutton.physicsBody?.isDynamic = false
firebutton.physicsBody?.affectedByGravity = false
firebutton.physicsBody?.categoryBitMask = 0
firebutton.physicsBody?.collisionBitMask = 100
firebutton.physicsBody?.contactTestBitMask = 100
firebutton.physicsBody?.contactTestBitMask = 0
firebutton.physicsBody?.collisionBitMask = 0
addChild(firebutton)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches{
spawnenemybullets()
let position = touch.location(in: self)
if firebutton.contains(position){
bullet = SKSpriteNode(color: .orange, size: CGSize(width: 20, height: 10))
bullet.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: 10))
bullet.physicsBody?.affectedByGravity = false
bullet.physicsBody?.isDynamic = true
bullet.physicsBody?.allowsRotation = false
bullet.position.x = player.position.x + 90
bullet.position.y = player.position.y
bullet.physicsBody?.mass = 0.01
bullet.physicsBody?.categoryBitMask = CategoryMask.GBullet.rawValue
bullet.physicsBody?.collisionBitMask = CategoryMask.enemyships.rawValue
bullet.physicsBody?.contactTestBitMask = CategoryMask.enemyships.rawValue
bullet.name = "FBullet"
addChild(bullet)
bullet.physicsBody?.applyForce(CGVector(dx: 400, dy: 0))
}
}
}
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
for touch in touches {
let position = touch.location(in: self)
if player.contains(position) {
if position.y > 85 && position.y < 665 && position.x > 85 && position.x < 640{
player.position = position
}
}
}
}
func spawnenemybullets () {
bullet = SKSpriteNode(color: .red, size: CGSize(width: 20, height: 10))
bullet.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: 10))
bullet.physicsBody?.affectedByGravity = false
bullet.physicsBody?.isDynamic = true
bullet.physicsBody?.allowsRotation = false
bullet.position.x = 1300
bullet.position.y = 200
bullet.physicsBody?.mass = 0.01
bullet.physicsBody?.categoryBitMask = CategoryMask.BBullet.rawValue
bullet.physicsBody?.collisionBitMask = CategoryMask.player.rawValue
bullet.physicsBody?.contactTestBitMask = CategoryMask.player.rawValue
bullet.name = "EnemyBullet"
addChild(bullet)
bullet.physicsBody?.applyForce(CGVector(dx: -400, dy: 0))
}
func didBegin(_ contact: SKPhysicsContact) {
print("Contact Happened")
}
}
两个Sprite都设置了其接触面罩,但是由于某些原因它们没有碰撞。当我在手机上测试该项目时,他们彼此之间会一直走下去。如何让他们彼此联系?
答案 0 :(得分:1)
您尚未为physicsBody
(或player
)创建fireButton
。
您还需要使自己成为物理联系人代表:
class GameScene: SKScene, SKPhysicsContactDelegate {
physicsWorld.contactDelegate = self
编辑:我关于碰撞和接触的循序渐进指南: https://stackoverflow.com/a/51041474/1430420
碰撞和接触指南:测试位掩码: https://stackoverflow.com/a/40596890/1430420
操纵位掩码可打开和关闭各个碰撞和接触。 https://stackoverflow.com/a/46495864/1430420