我的 didBeginContact() 函数不起作用 SWIFT 精灵工具包

MY didBeginContact() FUNCTION DOES NOT WORK SWIFT SPRITE KIT

嗨,我已经尝试解决这个问题很长时间了,我的问题是我的 didBeginContact() 不起作用 "rocket" 与 "missiles" 发生碰撞,它们被推下但是它没有 运行 功能,我该怎么做才能修复它? 提前致谢。

    //
//  PlaysScene.swift
//  Pocket Rocket3
//
//  Created by Lucas Farleigh on 27/11/2014.
//  Copyright (c) 2014 Lucas Farleigh. All rights reserved.
//

import spriteKit

class PlayScene:SKScene, SKPhysicsContactDelegate{
    //declaring the node in this scene!
    let background = SKSpriteNode(imageNamed: "background")



    let bara = SKSpriteNode(imageNamed: "bar6")
    let barb = SKSpriteNode(imageNamed: "bar6")

    let barc = SKSpriteNode(imageNamed: "bar6")
    let bard = SKSpriteNode(imageNamed: "bar6")

    let bare = SKSpriteNode(imageNamed: "bar6")
    let barf = SKSpriteNode(imageNamed: "bar6")

    let barg = SKSpriteNode(imageNamed: "bar6")
    let barh = SKSpriteNode(imageNamed: "bar6")

    let bari = SKSpriteNode(imageNamed: "bar6")
    let barj = SKSpriteNode(imageNamed: "bar6")

    let bar1 = SKSpriteNode(imageNamed: "bar6")
    let bar2 = SKSpriteNode(imageNamed: "bar6")

    let bar3 = SKSpriteNode(imageNamed: "bar6")
    let bar4 = SKSpriteNode(imageNamed: "bar6")

    let bar5 = SKSpriteNode(imageNamed: "bar6")
    let bar6 = SKSpriteNode(imageNamed: "bar6")

    let bar7 = SKSpriteNode(imageNamed: "bar6")
    let bar8 = SKSpriteNode(imageNamed: "bar6")

    let bar9 = SKSpriteNode(imageNamed: "bar6")
    let bar10 = SKSpriteNode(imageNamed: "bar6")
    let missile1  = SKSpriteNode(imageNamed:"Missile")
    let missile2  = SKSpriteNode(imageNamed:"Missile")
    let missile3  = SKSpriteNode(imageNamed:"Missile")
    let missile4  = SKSpriteNode(imageNamed:"Missile")
    let missile5  = SKSpriteNode(imageNamed:"Missile")
    let missile6  = SKSpriteNode(imageNamed:"Missile")
    let missile7  = SKSpriteNode(imageNamed:"Missile")
    let missile8  = SKSpriteNode(imageNamed:"Missile")
    let missile9  = SKSpriteNode(imageNamed:"Missile")
    let missile10 = SKSpriteNode(imageNamed:"Missile")
    let missile11 = SKSpriteNode(imageNamed:"Missile")
    let missile12 = SKSpriteNode(imageNamed:"Missile")
    let missile13 = SKSpriteNode(imageNamed:"Missile")
    let missile14 = SKSpriteNode(imageNamed:"Missile")
    let missile15 = SKSpriteNode(imageNamed:"Missile")
       let rocket = SKSpriteNode(imageNamed:"rocket")

    var  leftTouch = false
    var  rightTouch = false
    enum colideType:UInt32{
        case rocket = 1
        case missile = 2
    }

        var actionmove = SKAction.moveToY(-150, duration: 15)

    override func didMoveToView(view: SKView) {
        self.physicsWorld.contactDelegate = self
        let print = SKAction.runBlock({
        println("moved")
        })
        let actionmove1 = SKAction.sequence([actionmove,print])

        var delay = SKAction.waitForDuration(NSTimeInterval(1.5))
        var delchild = SKAction.removeFromParent()
        var rand = arc4random_uniform(10)
        self.rocket.physicsBody = SKPhysicsBody(rectangleOfSize: self.rocket.size)

        self.physicsBody?.categoryBitMask = colideType.rocket.toRaw()
        self.physicsBody?.contactTestBitMask = colideType.missile.toRaw()
        rocket.physicsBody?.affectedByGravity = false
        rocket.physicsBody?.contactTestBitMask = colideType.missile.toRaw()
        rocket.physicsBody?.collisionBitMask = colideType.missile.toRaw()

        missile1.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile1.size)
        missile2.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile2.size)
        missile3.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile3.size)
        missile4.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile4.size)
        missile5.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile5.size)
        missile6.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile6.size)
        missile7.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile7.size)
        missile8.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile8.size)
        missile9.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile9.size)
        missile10.physicsBody = SKPhysicsBody(rectangleOfSize: self.missile10.size)

        missile1.physicsBody?.dynamic = false
        missile2.physicsBody?.dynamic = false
        missile3.physicsBody?.dynamic = false
        missile4.physicsBody?.dynamic = false
        missile5.physicsBody?.dynamic = false
        missile6.physicsBody?.dynamic = false
        missile7.physicsBody?.dynamic = false
        missile8.physicsBody?.dynamic = false
        missile9.physicsBody?.dynamic = false
        missile10.physicsBody?.dynamic = false

        missile1.physicsBody?.categoryBitMask = colideType.missile.toRaw()
        missile2.physicsBody?.categoryBitMask = colideType.missile.toRaw()
        missile3.physicsBody?.categoryBitMask = colideType.missile.toRaw()
        missile4.physicsBody?.categoryBitMask = colideType.missile.toRaw()
        missile5.physicsBody?.categoryBitMask = colideType.missile.toRaw()
        missile6.physicsBody?.categoryBitMask = colideType.missile.toRaw()
        missile7.physicsBody?.categoryBitMask = colideType.missile.toRaw()
        missile8.physicsBody?.categoryBitMask = colideType.missile.toRaw()
        missile9.physicsBody?.categoryBitMask = colideType.missile.toRaw()
        missile10.physicsBody?.categoryBitMask = colideType.missile.toRaw()

        missile1.physicsBody?.affectedByGravity = false
        missile2.physicsBody?.affectedByGravity = false
        missile3.physicsBody?.affectedByGravity = false
        missile4.physicsBody?.affectedByGravity = false
        missile5.physicsBody?.affectedByGravity = false
        missile6.physicsBody?.affectedByGravity = false
        missile7.physicsBody?.affectedByGravity = false
        missile8.physicsBody?.affectedByGravity = false
        missile9.physicsBody?.affectedByGravity = false
        missile10.physicsBody?.affectedByGravity = false


        missile1.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()
        missile2.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()
        missile3.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()
        missile4.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()
        missile5.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()
        missile6.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()
        missile7.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()
        missile8.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()
        missile9.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()
        missile10.physicsBody?.contactTestBitMask = colideType.rocket.toRaw()

      missile1.physicsBody?.collisionBitMask = colideType.rocket.toRaw()
        missile2.physicsBody?.collisionBitMask = colideType.rocket.toRaw()
        missile3.physicsBody?.collisionBitMask = colideType.rocket.toRaw()
        missile4.physicsBody?.collisionBitMask = colideType.rocket.toRaw()
        missile5.physicsBody?.collisionBitMask = colideType.rocket.toRaw()
        missile6.physicsBody?.collisionBitMask = colideType.rocket.toRaw()
        missile7.physicsBody?.collisionBitMask = colideType.rocket.toRaw()
        missile8.physicsBody?.collisionBitMask = colideType.rocket.toRaw()
        missile9.physicsBody?.collisionBitMask = colideType.rocket.toRaw()
        missile10.physicsBody?.collisionBitMask = colideType.rocket.toRaw()


        missile1.yScale = 0.35
        missile2.yScale = 0.35
        missile3.yScale = 0.35
        missile4.yScale = 0.35
        missile5.yScale = 0.35
        missile6.yScale = 0.35
        missile7.yScale = 0.35
        missile8.yScale = 0.35
        missile9.yScale = 0.35
        missile10.yScale = 0.35

        missile1.xScale = 0.6
        missile2.xScale = 0.6
        missile3.xScale = 0.6
        missile4.xScale = 0.6
        missile5.xScale = 0.6
        missile6.xScale = 0.6
        missile7.xScale = 0.6
        missile8.xScale = 0.6
        missile9.xScale = 0.6
        missile10.xScale = 0.6

        let delayA = SKAction.waitForDuration(NSTimeInterval(2.0))
        let delayB = SKAction.waitForDuration(NSTimeInterval(4.0))
        let delayC = SKAction.waitForDuration(NSTimeInterval(6.0))
        let delayD = SKAction.waitForDuration(NSTimeInterval(8.0))
        let delayE = SKAction.waitForDuration(NSTimeInterval(10.0))
        let delayF = SKAction.waitForDuration(NSTimeInterval(12.0))
        let delayG = SKAction.waitForDuration(NSTimeInterval(14.0))
        let delayH = SKAction.waitForDuration(NSTimeInterval(16.0))
        let delayI = SKAction.waitForDuration(NSTimeInterval(18.0))
        let delayJ = SKAction.waitForDuration(NSTimeInterval(20.0))

        var missile1Hide = SKAction.runBlock({
        self.missile1.hidden = true
        })

        var missile2Hide = SKAction.runBlock({
            self.missile2.hidden = true
        })

        var missile3Hide = SKAction.runBlock({
            self.missile3.hidden = true
        })

        var missile4Hide = SKAction.runBlock({
            self.missile4.hidden = true
        })

        var missile5Hide = SKAction.runBlock({
            self.missile5.hidden = true
        })

        var missile6Hide = SKAction.runBlock({
            self.missile6.hidden = true
        })

        var missile7Hide = SKAction.runBlock({
            self.missile7.hidden = true
        })

        var missile8Hide = SKAction.runBlock({
            self.missile8.hidden = true
        })

        var missile9Hide = SKAction.runBlock({
            self.missile9.hidden = true
        })

        var missile10Hide = SKAction.runBlock({
            self.missile10.hidden = true
        })

        var missile1show = SKAction.runBlock({
            self.missile1.hidden = false
        })

        var missile2show = SKAction.runBlock({
            self.missile2.hidden = false
        })

        var missile3show = SKAction.runBlock({
            self.missile3.hidden = false
        })

        var missile4show = SKAction.runBlock({
            self.missile4.hidden = false
        })

        var missile5show = SKAction.runBlock({
            self.missile5.hidden = false
        })

        var missile6show = SKAction.runBlock({
            self.missile6.hidden = false
        })

        var missile7show = SKAction.runBlock({
            self.missile7.hidden = false
        })

        var missile8show = SKAction.runBlock({
            self.missile8.hidden = false
        })

        var missile9show = SKAction.runBlock({
            self.missile9.hidden = false
        })

        var missile10show = SKAction.runBlock({
            self.missile10.hidden = false
        })

        var position1 = SKAction.runBlock({
        println("1")
        self.missile1.position =  CGPointMake(600,850)
        })

        var position2 = SKAction.runBlock({
            println("2")
            self.missile2.position = CGPointMake(300,850)

        })
        var position3 = SKAction.runBlock({

            self.missile3.position = CGPointMake(100,850)
          println("3")
        })
        var position4 = SKAction.runBlock({
            println("4")
            self.missile4.position = CGPointMake(900,850)

        })
        var position5 = SKAction.runBlock({
            println("5")
            self.missile5.position = CGPointMake(300,850)

        })
        var position6 = SKAction.runBlock({
            println("6")
            self.missile6.position = CGPointMake(600,850)

        })
        var position7 = SKAction.runBlock({
            println("7")
            self.missile7.position = CGPointMake(200,850)

        })
        var position8 = SKAction.runBlock({
            println("8")
            self.missile8.position = CGPointMake(600,850)

        })
        var position9 = SKAction.runBlock({
            println("9")
            self.missile9.position = CGPointMake(900,850)

        })
        var position10 = SKAction.runBlock({
            println("10")
            self.missile10.position = CGPointMake(200,850)

        })







        let sequence1 = SKAction.sequence([missile1Hide,delayA,position1,missile1show,actionmove])
        let sequence2 = SKAction.sequence([missile2Hide,delayB,position2,missile2show,actionmove])
        let sequence3 = SKAction.sequence([missile3Hide,delayC,position3,missile3show,actionmove])
        let sequence4 = SKAction.sequence([missile4Hide,delayD,position4,missile4show,actionmove])
        let sequence5 = SKAction.sequence([missile5Hide,delayE,position5,missile5show,actionmove])
        let sequence6 = SKAction.sequence([missile6Hide,delayF,position6,missile6show,actionmove])
        let sequence7 = SKAction.sequence([missile7Hide,delayG,position7,missile7show,actionmove])
        let sequence8 = SKAction.sequence([missile8Hide,delayH,position8,missile8show,actionmove])
        let sequence9 = SKAction.sequence([missile9Hide,delayI,position9,missile9show,actionmove])
        let sequence10 = SKAction.sequence([missile10Hide,delayJ,position10,missile10show,actionmove])

        let s1 = SKAction.sequence([delayA,position1,actionmove1])
        let s2 = SKAction.sequence([delayB,position2,actionmove1])
        let s3 = SKAction.sequence([delayC,position3,actionmove1])
        let s4 = SKAction.sequence([delayD,position4,actionmove1])
        let s5 = SKAction.sequence([delayE,position5,actionmove1])
        let s6 = SKAction.sequence([delayF,position6,actionmove1])
        let s7 = SKAction.sequence([delayG,position7,actionmove1])
        let s8 = SKAction.sequence([delayH,position8,actionmove1])
        let s9 = SKAction.sequence([delayI,position9,actionmove1])
        let s10 = SKAction.sequence([delayJ,position10,actionmove1])

        let r1 = SKAction.repeatActionForever(s1)
        let r2 = SKAction.repeatActionForever(s2)
        let r3 = SKAction.repeatActionForever(s3)
        let r4 = SKAction.repeatActionForever(s4)
        let r5 = SKAction.repeatActionForever(s5)
        let r6 = SKAction.repeatActionForever(s6)
        let r7 = SKAction.repeatActionForever(s7)
        let r8 = SKAction.repeatActionForever(s8)
        let r9 = SKAction.repeatActionForever(s9)
        let r10 = SKAction.repeatActionForever(s10)




        //actionmove: making it smooth

        //doing stuff with the background
        background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        background.yScale = 10.0
        background.xScale = 10.0
        addChild(background)
        addChild(rocket)
        rocket.yScale = 0.3
        rocket.xScale = 0.6




        addChild(missile1)
        addChild(missile2)
        addChild(missile3)
        addChild(missile4)
        addChild(missile5)
        addChild(missile6)
        addChild(missile7)
        addChild(missile8)
        addChild(missile9)
        addChild(missile10)


        self.missile1.runAction(r1, completion:{
            self.missile1.runAction(r1)
        })

        self.missile2.runAction(r2, completion:{
            self.missile1.runAction(r2)
        })

        self.missile3.runAction(r3, completion:{
            self.missile1.runAction(r3)
        })

        self.missile4.runAction(r4, completion:{
            self.missile1.runAction(r4)
        })

        self.missile5.runAction(r5, completion:{
            self.missile1.runAction(r5)
        })

        self.missile6.runAction(r6, completion:{
            self.missile1.runAction(r6)         })

        self.missile7.runAction(r7, completion:{
            self.missile1.runAction(r7)
        })


        self.missile8.runAction(r8, completion:{
            self.missile1.runAction(r8)
        })


        self.missile9.runAction(r9, completion:{
            self.missile1.runAction(r9)
        })


        self.missile10.runAction(r10, completion:{
            self.missile1.runAction(r10)
        })
        rocket.position = CGPointMake(CGRectGetMidX(self.frame), 200)

        func didBeginContact(contact: SKPhysicsContact){
            if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene{
                let skView = self.view as SKView!
                skView.presentScene(scene)
            }



        }
    }





    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        for touch in touches {
            let location = touch.locationInNode(self)
            var node = self.nodeAtPoint(location)
            var loc = location.x
            var tapLoc = SKAction.moveToX(loc, duration: 3.5)
           tapLoc.timingMode = SKActionTimingMode.EaseOut
            rocket.runAction(tapLoc)


        }
    }






    }

我相信你在 didMoveToView 中遗漏了:

//not sure if it is needed but almost everyone sets the gravity to 0,0 unless needed
self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)

另外 didBeginContact 应该在 didMoveToView 之外,而你已经把它放在里面了。

希望对您有所帮助。