当玩家 body 遇到怪物 body 时,函数 func didBeginContact(contact: SKPhysicsContact)
永远不会被调用。
我的类实现SKPhysicsContactDelegate
class GameScene: SKScene, SKPhysicsContactDelegate
属性collisionBitMask
我评论它是因为我读到如果我们不放置它,节点可以与所有节点发生冲突
我像这样添加我的播放器:
bear.position = CGPoint(x:self.frame.width/20, y:self.frame.height/5)
bear.size = CGSize(width: self.frame.width/7, height: self.frame.height/5)
bear.physicsBody = SKPhysicsBody(rectangleOfSize: bear.frame.size)
bear.physicsBody?.dynamic = false;
bear.physicsBody?.categoryBitMask = PhysicsCategory.Player
bear.physicsBody?.contactTestBitMask = PhysicsCategory.Monster
//bear.physicsBody?.collisionBitMask = PhysicsCategory.Monster
我添加这样的怪物:
let thePosition = CGPoint(x: positionX, y: (self.frame.height/7.4)*positionY)
let monster = SKSpriteNode(imageNamed: pattern as String)
monster.size = CGSize(width: self.frame.width/16, height: self.frame.height/11)
monster.name = name
monster.position = thePosition
monster.physicsBody = SKPhysicsBody(rectangleOfSize: monster.frame.size)
monster.physicsBody?.dynamic = false
monster.physicsBody?.categoryBitMask = PhysicsCategory.Monster // 3
monster.physicsBody?.affectedByGravity = false
monster.physicsBody?.contactTestBitMask = PhysicsCategory.Player // 4
//platformNode.physicsBody?.collisionBitMask = PhysicsCategory.Player // 5
addChild(monster)
我的物理类别
struct PhysicsCategory {
static let None : UInt32 = 0
static let All : UInt32 = UInt32.max
static let Player: UInt32 = 0b1 // 1
static let Monster : UInt32 = 0b10 // 2
static let Projectile: UInt32 = 0b100 // 3
static let Plateforme: UInt32 = 0b1000 // 4
}
我有多个纹理,例如当玩家行走时我称之为:
func walkingBear() {
bear.runAction( SKAction.repeatActionForever(SKAction.animateWithTextures(bearCoursFrames, timePerFrame: 0.1, resize: false, restore: true)), withKey:"cours")
}
我像这样初始化 bearCoursFrames
var walkFrames = [SKTexture]()
var numImages : Int = bearAnimatedAtlas.textureNames.count
for var i=1; i<=numImages/2; i++ {
let bearTextureName = "cours\(i)"
walkFrames.append(bearAnimatedAtlas.textureNamed(bearTextureName))
}
bearCoursFrames = walkFrames
请问我的错误在哪里,我在其他帖子中找不到解决方案 任何帮助将不胜感激
--------------------------------编辑------------------------- ----------------- 解决方案:我只需添加 self.physicalWorld.contactDelegate = self 现在就可以了。
最佳答案
答案很简单。有一条规则规定,至少必须将一个实体定义为动态实体才能注册联系人。
将玩家或其他 body 更改为动态,并且将检测到接触。
作为补充,如果您还没有启用物理视觉表示...这可能会有所帮助,因为有时由于实际物体未彼此相交而无法检测到接触,例如。当节点的 anchor 更改时,纹理和主体会错位(可以使用 SKPhysicsBody 类的适当初始化程序修复,但这是一个非常常见的错误)。
关于swift - Sprite Kit 从未检测到我的碰撞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36340196/