初学者 swift sprite 工具包 - 节点碰撞检测帮助 (SKPhysicsContact) [英] beginner swift sprite kit - node collision detection help (SKPhysicsContact)

查看:29
本文介绍了初学者 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

我在 swift 中找不到任何对我有意义的东西,但我认为问题就在这里

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)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆