Sprite kit联系问题

时间:2016-03-24 01:43:42

标签: swift sprite-kit

我正在制作一款需要你躲避掉落物体的游戏。我试图做到这一点,以便当坠落的物体击中玩家某些代码运行时。但它没有用,请告诉我我的代码有什么问题。

import SpriteKit

struct physicsCatagory {
    static let person : UInt32 = 0x1 << 1
    static let Ice : UInt32 = 0x1 << 2
}


class GameScene: SKScene, SKPhysicsContactDelegate {

   var person = SKSpriteNode(imageNamed: "Person")



    override func didMoveToView(view: SKView) {


        physicsWorld.contactDelegate = self


        person.position = CGPointMake(self.size.width/2, self.size.height/12)
        person.setScale(0.4)
        person.physicsBody = SKPhysicsBody(rectangleOfSize: person.size)
        person.physicsBody?.affectedByGravity = false
        person.physicsBody?.categoryBitMask = physicsCatagory.person
        person.physicsBody?.contactTestBitMask = physicsCatagory.Ice
        person.physicsBody?.dynamic = false



        var IceTimer = NSTimer.scheduledTimerWithTimeInterval(0.5, target: self, selector: Selector("spawnFirstIce"), userInfo: nil, repeats: true)

        self.addChild(person)


        func didBeginContact(contact: SKPhysicsContact) {
            let firstBody = contact.bodyA
            let secondBody = contact.bodyB

            if firstBody.categoryBitMask == physicsCatagory.person && secondBody.categoryBitMask == physicsCatagory.Ice || firstBody.categoryBitMask == physicsCatagory.Ice && secondBody.categoryBitMask == physicsCatagory.person{
       NSLog ("Test Test")           }


        }


    }




    func spawnFirstIce(){

        let Ice = SKSpriteNode(imageNamed: "FirstIce")
        Ice.setScale(0.5)
        Ice.physicsBody = SKPhysicsBody(rectangleOfSize: Ice.size)
        Ice.physicsBody?.categoryBitMask = physicsCatagory.Ice
        Ice.physicsBody?.contactTestBitMask = physicsCatagory.person
        Ice.physicsBody?.affectedByGravity = false
        Ice.physicsBody?.dynamic = true



        let MinValue = self.size.width / 8
        let MaxValue = self.size.width - 20
        let SpawnPoint =  UInt32(MaxValue - MinValue)
        Ice.position = CGPoint(x: CGFloat(arc4random_uniform(SpawnPoint)), y: self.size.height)
        self.addChild(Ice)



        let action = SKAction.moveToY(-85, duration: 3.0)
        let actionDone = SKAction.removeFromParent()
        Ice.runAction(SKAction.sequence([action,actionDone]))


    }
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {


        for touch in touches {
            let location = touch.locationInNode(self)

         person.position.x = location.x



                   }
    }
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        for touch in touches {
            let location = touch.locationInNode(self)

            person.position.x = location.x



        }
    }




    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

1 个答案:

答案 0 :(得分:1)

您已在didBeginContact方法内定义didMoveToView:)

didBeginContact置于该方法之外,并使其成为GameScene类的成员。