如何在 spritekit swift 中更改碰撞时的 spritekit。
else if collidedBird.birdType == 6 {
if UIDevice.currentDevice().userInterfaceIdiom == .Pad
{
_bird.physicsBody?.applyImpulse(CGVectorMake(0, birdImpluseReturn(70.0)))
} else {
_bird.physicsBody?.applyImpulse(CGVectorMake(0, birdImpluseReturn(70.0)))
}
let fire = NSTimer.scheduledTimerWithTimeInterval(0.05, target: self, selector: Selector("createDragonFire"), userInfo: nil, repeats: false)
playEffectSound("BS_Std_Jump_SFX.mp3")
_platform.removeFromParent()
}
最佳答案
如果你想改变Sprite的图像,那么你应该使用SKPhysicsContactDelegate
方法。
一般来说,这意味着您必须实现SKPhysicsContactDelegate
,创建一个新的结构:
例如:
struct PhysicCategories {
static let NoneC : UInt32 = 0x1 << 0
static let Bird : UInt32 = 0x1 << 1
static let PlatformC : UInt32 = 0x1 << 2
static let FinishC : UInt32 = 0x1 << 3
static let ObstacleC : UInt32 = 0x1 << 4
}
然后进入您的didMoveToView()
函数并写入:
PhysicsWorld.contactdelegate = self
Dont forget to make this Step!
然后添加函数
func didBeginContact(contact: SKPhysicsContact) {
var contactbody1 = contact.bodyA
var contactbody2 = contact.bodyB
if contactbody1.categoryBitMask < contactbody2.categoryBitMask{
contactbody1 = contact.bodyA
contactbody2 = contact.bodyB
} else {
contactbody1 = contact.bodyB
contactbody2 = contact.bodyA
}
if ((contactbody1.categoryBitMask == PhysicCategories.BirdC) && (contactbody2.categoryBitMask == PhysicCategories.ObstacleC)){
// Make a GameOver Function for that!
// GameOver()
}
if ((contactbody1.categoryBitMask == PhysicCategories.BirdC) && (contactbody2.categoryBitMask == PhysicCategories.PlatformC)){
contactbody1.node.texture = SKTexture(imageNamed: "")
}
关于swift - 我如何在 spritekit Swift 中的碰撞中更改我的 spritekit,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31293091/