ios – 检测Sprite Collision而不在SpriteKit中反弹

前端之家收集整理的这篇文章主要介绍了ios – 检测Sprite Collision而不在SpriteKit中反弹前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是使用SpriteKit / Swift进行编码的新手,并且存在以下问题:
一个角色应该通过跳入它们来收集硬币.在检测碰撞和摆脱收集的硬币时没有问题,但是我的角色在硬币消失之前会反弹.

角色应该通过硬币飞行并在途中收集它.

let playerCategory: UInt32 = 0x1 << 0
let coinCategory: UInt32 = 0x1 << 1
player = SKSpriteNode(texture: playerTexture)
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.height / 2)
player.physicsBody?.dynamic = true
player.physicsBody?.allowsRotation = false
player.physicsBody?.categoryBitMask = playerCategory
player.physicsBody?.contactTestBitMask = coinCategory

var coin:SKSpriteNode = SKSpriteNode(texture: coinTexture)
coin.physicsBody = SKPhysicsBody(circleOfRadius: coin.size.height / 2)
coin.physicsBody?.dynamic = false
coin.physicsBody?.allowsRotation = false
coin.physicsBody?.categoryBitMask = coinCategory
coin.physicsBody?.contactTestBitMask = playerCategory
func playerDidCollideWithCoin(player:SKSpriteNode,thisCoin:SKSpriteNode) {
    thisCoin.removeFromParent()
    coinsCollected++
}

碰撞检测工作正常,但正如我所说,我怎样才能避免撞击并用“飞越”代替?

我正在使用Xcode 6 Beta 7

提前致谢!

以下评论中的解决方案;)

解决方法

spritekit的默认行为是,除非碰撞位掩码更改为0,否则所有内容都会与所有内容发生冲突.

在您不希望退回但接收来自它们的通知的所有对象中,将代码中的此值更改为0.

player = SKSpriteNode(texture: playerTexture)
player.physicsBody = SKPhysicsBody(circleOfRadius: player.size.height / 2)
player.physicsBody?.dynamic = true
player.physicsBody?.allowsRotation = false
player.physicsBody?.categoryBitMask = playerCategory
player.physicsBody?.contactTestBitMask = coinCategory
player.physicsBody?.collisionBitMask = 0

var coin:SKSpriteNode = SKSpriteNode(texture: coinTexture)
coin.physicsBody = SKPhysicsBody(circleOfRadius: coin.size.height / 2)
coin.physicsBody?.dynamic = false
coin.physicsBody?.allowsRotation = false
coin.physicsBody?.categoryBitMask = coinCategory
coin.physicsBody?.contactTestBitMask = playerCategory
coin.physicsBody?.collisionBitMask = 0

这将阻止这些对象相互反弹.

原文链接:https://www.f2er.com/iOS/331420.html

猜你在找的iOS相关文章