I tested with a simple scene.
我测试了一个简单的场景。
Func didBeginContact
can't be called.
无法调用Func didBeginContact。
I have try to use collisionBitMask
and s1.physicsBody = SKPhysicsBody(circleOfRadius: 100)
. The problem was persistent.
我尝试使用collisionBitMask和s1.physicsBody = SKPhysicsBody(circleOfRadius:100)。问题持续存在。
What can I do to fix it?
我该怎么做才能解决它?
import SpriteKit
class Test2Scene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
physicsWorld.contactDelegate = self
physicsWorld.speed = 0
let s1 = SKSpriteNode(imageNamed: kImagePlayer)
s1.position = CGPointMake(100, 100)
s1.physicsBody = SKPhysicsBody(rectangleOfSize: s1.size)
s1.physicsBody?.categoryBitMask = 1
s1.physicsBody?.contactTestBitMask = 2
//s1.physicsBody?.collisionBitMask = 2
self.addChild(s1)
let s2 = SKSpriteNode(imageNamed: kImagePlayer)
s2.position = CGPointMake(100, 500);
s2.runAction(SKAction.moveToY(0, duration: 1))
s2.physicsBody = SKPhysicsBody(rectangleOfSize: s2.size)
s2.physicsBody?.categoryBitMask = 2
//s2.physicsBody?.collisionBitMask = 1
self.addChild(s2);
print("view did load")
}
func didBeginContact(contact: SKPhysicsContact) {
print("aaa")
}
func didEndContact(contact: SKPhysicsContact) {
print("bbb")
}
}
2 个解决方案
#1
1
Your code is pretty much all wrong, but your offender is :
你的代码几乎都错了,但你的罪犯是:
physicsWorld.speed = 0
From the docs:
来自文档:
The default value is 1.0, which means the simulation runs at normal speed. A value other than the default changes the rate at which time passes in the physics simulation. For example, a speed value of 2.0 indicates that time in the physics simulation passes twice as fast as the scene’s simulation time. A value of 0.0 pauses the physics simulation.
默认值为1.0,表示模拟以正常速度运行。默认值以外的值会更改物理模拟中经过的速率。例如,速度值为2.0表示物理模拟中的时间传递速度是场景模拟时间的两倍。值0.0会暂停物理模拟。
So setting it to 0.0 pauses the simulation. Second problem is that your nodes are affected by gravity but you move them using SKAction
. That way you are pulling the node out of physics simulation and you may experience weird behaviours.
因此将其设置为0.0会暂停模拟。第二个问题是您的节点受重力影响,但您使用SKAction移动它们。这样你就可以将节点拉出物理模拟,你可能会遇到奇怪的行为。
Also I would recommend to read this great * post about bitmasks and how they work / should be used.
另外,我建议阅读这篇关于bitmasks以及它们如何工作/应该如何使用的*文章。
#2
0
Find out the reason. Need use
找出原因。需要使用
s1.physicsBody!.dynamic = true
s2.physicsBody!.dynamic = true
#1
1
Your code is pretty much all wrong, but your offender is :
你的代码几乎都错了,但你的罪犯是:
physicsWorld.speed = 0
From the docs:
来自文档:
The default value is 1.0, which means the simulation runs at normal speed. A value other than the default changes the rate at which time passes in the physics simulation. For example, a speed value of 2.0 indicates that time in the physics simulation passes twice as fast as the scene’s simulation time. A value of 0.0 pauses the physics simulation.
默认值为1.0,表示模拟以正常速度运行。默认值以外的值会更改物理模拟中经过的速率。例如,速度值为2.0表示物理模拟中的时间传递速度是场景模拟时间的两倍。值0.0会暂停物理模拟。
So setting it to 0.0 pauses the simulation. Second problem is that your nodes are affected by gravity but you move them using SKAction
. That way you are pulling the node out of physics simulation and you may experience weird behaviours.
因此将其设置为0.0会暂停模拟。第二个问题是您的节点受重力影响,但您使用SKAction移动它们。这样你就可以将节点拉出物理模拟,你可能会遇到奇怪的行为。
Also I would recommend to read this great * post about bitmasks and how they work / should be used.
另外,我建议阅读这篇关于bitmasks以及它们如何工作/应该如何使用的*文章。
#2
0
Find out the reason. Need use
找出原因。需要使用
s1.physicsBody!.dynamic = true
s2.physicsBody!.dynamic = true