本文介绍了初学者Swift Sprite套件-节点碰撞检测帮助(SKPhysicsContact)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个精灵在触摸另一个精灵时将自身删除。

I want a sprite to delete itself when touching another sprite. Right now when they touch, they just push each other.

我有这个:

let alphaCategory: UInt32 = 0x1 << 0
let betaCategory: UInt32 = 0x1 << 1

我使子画面变成动态的,不受重力的影响

I made the sprites dynamic and not affected by gravity

self.physicsworld.contactDelegate = self

alpha.physicsBody?.categoryBitMask = alphaCategory
alpha.physicsBody?.contactTestBitmask = betaCategory

beta.physicsBody?.categoryBitMask = betaCategory
beta.physicsBody?.contactTestBitmask = alphaCategory

我无法迅速找到对我有意义的任何东西,但我认为问题在这里

I couldn't find anything in swift that made sense to me, but I think the problem is here

func didBeginContact(contact: SKPhysicsContact) {

    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.BodyB
        beta.removeFromParent()
    }
}


推荐答案

首先,您应该设置firstBody& secondBody按其冲撞位面具的顺序:

First you should set your firstBody & secondBody to the order of their collisionBitMask:

if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask
    {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    }
else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
     }

if firstBody.categoryBitMask=0 && secondBody.categoryBitMask=1 {
      secondBody.removeFromParent()
}

此将防止您的精灵与任何东西(包括彼此)碰撞。在设置其他BitMask属性的位置进行设置:

This will prevent your sprites from colliding with anything (including each other). Set this where you set the other BitMask properties:

alpha.physicsBody.collisionBitMask = 0
beta.physicsBody.collisionBitMask = 0

这篇关于初学者Swift Sprite套件-节点碰撞检测帮助(SKPhysicsContact)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 02:41