如何在spritekit swift中更改碰撞时的spritekit

时间:2015-07-08 12:44:03

标签: swift

如何在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()
        }

1 个答案:

答案 0 :(得分:1)

如果您想更改精灵的图像,那么您应该使用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
  

别忘了这一步!

然后添加func

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: "")



        }