玩家 SKPhysicsBody 通过其他 SKPhysicsBody swift 4 Xcode 9 [英] player SKPhysicsBody passes through other SKPhysicsBody swift 4 Xcode 9

查看:20
本文介绍了玩家 SKPhysicsBody 通过其他 SKPhysicsBody swift 4 Xcode 9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照 swift 3 game development 一书中的说明创建一个简单的点击来翻转游戏.我有两个 SKSpriteNodes,一个是玩家,另一个是静态对象,如果它接触到玩家会伤害到它.

I'm creating a simple tap to flap game following the instructions from the book swift 3 game development. I have two SKSpriteNodes one is the player and the other is a static object that can hurt the player if it makes contact.

对玩家的伤害将通过 categoryBitMask 通过自定义联系逻辑实现.问题是当我设置静态对象的 categoryBitMask 时,当玩家与静态对象接触时,会触发自定义逻辑,但玩家现在会穿过静态对象.

Damage to the player will be implemented through custom contact logic through the categoryBitMask. The problem is when I set the categoryBitMask of the static object then when the player makes contact with the static object the custom logic is triggered but the player now moves through the static object.

这里是播放器和静态对象的 init() 函数

Here are the init() functions for both player and static object

静态对象

    init() {
    super.init(texture: nil, color: .clear, size: initialSize)
    createAnimations()
    self.run(spinAnimation)
    let startTexture = textureAtlas.textureNamed("blade")
    self.physicsBody = SKPhysicsBody(texture: startTexture, size:   initialSize)
    self.physicsBody?.affectedByGravity = false
    self.physicsBody?.isDynamic = false
    self.physicsBody?.categoryBitMask = PhysicsCategory.enemy.rawValue
    self.physicsBody?.collisionBitMask = ~PhysicsCategory.heroDamage.rawValue
    self.zPosition = -9
  }

玩家

    init() {
    super.init(texture: nil, color: .clear, size: initialSize)
    let textureBody = textureAtlas.textureNamed("pierre-flying-3")
    self.physicsBody = SKPhysicsBody(texture: textureBody, size: self.size)
    self.physicsBody?.linearDamping = 0.9
    self.physicsBody?.mass = 30
    self.physicsBody?.allowsRotation = false
    self.zPosition = 1
    createAnimations()
    self.run(soarAnimation, withKey: "soarAnimation")
    self.physicsBody?.categoryBitMask = PhysicsCategory.hero.rawValue
    self.physicsBody?.contactTestBitMask = PhysicsCategory.ground.rawValue |
                                           PhysicsCategory.enemy.rawValue |
                                           PhysicsCategory.coin.rawValue |
                                           PhysicsCategory.powerUp.rawValue
    self.physicsBody?.collisionBitMask = PhysicsCategory.ground.rawValue
}

如果我删除 isDynamic = false 条件,那么是的,它可以工作,但对象不再是静态的.我已经尝试增加对象的质量并删除 isDynamic = false .但是出于某种奇怪的原因,这与仅包含 IsDynamic = false 语句的效果相同

If I remove the isDynamic = false condition then yes it works but the object is no longer static. I've tried increasing the mass of the object and removing the isDynamic = false also. But for some strange reason this has the same effect of just including the IsDynamic = false statement

有什么帮助吗?谢谢

编辑categoryBitMask 值的枚举

edit The enumeration of the categoryBitMask values

enum PhysicsCategory:UInt32 {
case hero = 1
case heroDamage = 2
case ground = 4
case enemy = 8
case coin = 16
case powerUp = 32
}

推荐答案

应该发生碰撞的两个节点之一需要动态以便发生碰撞地方.你不能让它们都是静态的.

One of the two nodes that should collide needs to be dynamic in order for a collision to take place. You can't have them all static.

这篇关于玩家 SKPhysicsBody 通过其他 SKPhysicsBody swift 4 Xcode 9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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