【发布时间】:2016-07-11 14:16:25
【问题描述】:
所以我已经在这个游戏上工作了一段时间,一切都按照我想要的方式工作,但是我已经卡在 didBeginContact 上几天了,我无法理解它,我想它看起来不错,我检查了几个论坛并得到了几乎相同的结果,因此它适用于我的游戏。但仍然没有:(任何帮助将不胜感激。 所以这是我的 didBeginContact:
func didBeginContact(contact: SKPhysicsContact) {
print("i worked")
// Every contact has two bodies, we do not know which body is which.
// We will find which body is the penguin, then use the other body
// to determine what type of contact is happening.
let otherBody:SKPhysicsBody
// Combine the two penguin physics categories into one mask
// using the bitwise OR operator |
let bunnyMask = PhysicsCategory.bunny.rawValue | PhysicsCategory.bunnyDamaged.rawValue
// Use the bitwise AND operator & to find the penguin.
// This returns a positive number if bodyA’s category is
// the same as either the penguin or damagedPenguin:
if (contact.bodyA.categoryBitMask & bunnyMask) > 0 {
// bodyA is the penguin, we want to find out what bodyB is:
otherBody = contact.bodyB
}
else {
// bodyB is the penguin, we will test against bodyA:
otherBody = contact.bodyA
}
// Find the contact type:
switch otherBody.categoryBitMask {
case PhysicsCategory.grass.rawValue:
print("nom nom grass")
case PhysicsCategory.mud.rawValue:
print("mud hit")
case PhysicsCategory.stone.rawValue:
print("ow my head")
case PhysicsCategory.lava.rawValue:
print("it burns")
case PhysicsCategory.hedgehog.rawValue:
print("stop touching me!")
default:
print("Contact with no game logic")
}
}
这是兔子的密码:
class Bunny : SKSpriteNode, GameSprite{
var currentSpeed: Double = 5
var textureAtlas: SKTextureAtlas = SKTextureAtlas(named: "bunny.atlas")
var moveAnimation = SKAction()
func spawn(parentNode: SKNode, position: CGPoint, size: CGSize = CGSize(width: 25, height: 25)) {
parentNode.addChild(self)
createAnimations()
self.size = size
self.position = position
self.runAction(moveAnimation, withKey: "moveAnimation")
self.physicsBody = SKPhysicsBody(rectangleOfSize: self.size)
self.physicsBody?.categoryBitMask = PhysicsCategory.bunny.rawValue
self.physicsBody?.contactTestBitMask = PhysicsCategory.hedgehog.rawValue
self.physicsBody?.collisionBitMask = PhysicsCategory.hedgehog.rawValue
self.physicsBody?.affectedByGravity = false
self.physicsBody?.dynamic = true
}
还有兔子的 GameScene 代码:
initialBunnyPosition = CGPoint(x: 25, y: self.size.height)
bunny.zPosition = 1000
bunny.spawn(world, position: initialBunnyPosition)
任何有任何事情的人请帮忙,如果您需要更多代码片段,请告诉我,我会提供更多信息。 提前致谢。
【问题讨论】:
-
您是否设置了联系人的代理? (self.physicsWorld.contactDelegate = self)
-
是的,我有,但仍然一无所有:(
-
你是否为你的所有角色创建了正确的物理边界:)?
-
我为我的每个节点都使用了
self.physicsBody = SKPhysicsBody(rectangleOfSize: self.size),这就是你的意思吗?对不起,如果我听起来很愚蠢,我对 SpriteKit 和游戏开发很陌生。 -
“我工作过”会被打印出来吗?你能告诉我们你的 PhysicsCategory 原始值吗?您的 Gamescene 类是否实现了 SKPhysicsContactDelete 协议?
标签: ios iphone swift sprite-kit swift2