Sprite Kit 从未检测到我的碰撞
Sprite Kit never detect my collision
当玩家 body 遇到怪物 body 时,函数 func didBeginContact(contact: SKPhysicsContact)
永远不会被调用。
我的class工具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
请问我的错误在哪里,我在其他地方找不到解决方案post
任何帮助将不胜感激
--------------------------------编辑------------ ------------------
解决方案:我只需添加 self.physicsWorld.contactDelegate = self,现在就可以了。
答案很简单。有一条规则规定至少有一个 body 必须定义为动态才能注册联系人。
将播放器或其他 body 更改为动态,并且将检测到接触。
另外,如果您还没有启用物理视觉表示...这可能会有所帮助,因为有时无法检测到接触,因为实际的物体没有相互交叉,例如。当节点的锚点改变时,纹理和 body 错位(这是可以修复的,使用适当的 SKPhysicsBody class 初始化程序,但这是一个非常常见的错误)。
我刚加
self.physicsWorld.contactDelegate = self
现在可以使用了。
当玩家 body 遇到怪物 body 时,函数 func didBeginContact(contact: SKPhysicsContact)
永远不会被调用。
我的class工具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
请问我的错误在哪里,我在其他地方找不到解决方案post 任何帮助将不胜感激
--------------------------------编辑------------ ------------------ 解决方案:我只需添加 self.physicsWorld.contactDelegate = self,现在就可以了。
答案很简单。有一条规则规定至少有一个 body 必须定义为动态才能注册联系人。
将播放器或其他 body 更改为动态,并且将检测到接触。
另外,如果您还没有启用物理视觉表示...这可能会有所帮助,因为有时无法检测到接触,因为实际的物体没有相互交叉,例如。当节点的锚点改变时,纹理和 body 错位(这是可以修复的,使用适当的 SKPhysicsBody class 初始化程序,但这是一个非常常见的错误)。
我刚加
self.physicsWorld.contactDelegate = self
现在可以使用了。