碰撞不适用于swift + sprite工具包

时间:2015-06-28 00:06:06

标签: swift sprite-kit

import SpriteKit



// fix spawning so close to the middle
// get ball col working
// set width apart/ height they must be apart (if statement)


let BallCategoryName = "ball"
let BarCategoryName = "block"
let BarNodeCategoryName = "blockNode"

let GreenBallCategory  : UInt32 = 0x1 << 0
let RedBallCategory: UInt32 = 0x1 << 1
let GreenBarCategory : UInt32 = 0x1 << 2
let RedBarCategory : UInt32 = 0x1 << 3

class GameScene: SKScene,  SKPhysicsContactDelegate {



    //VARIABLES IMPORTANTE//

    var greenBall = SKSpriteNode(imageNamed: "greenball")
    var redBall = SKSpriteNode(imageNamed: "redball")
    var bar0 = SKSpriteNode(imageNamed: "bar0")
    var bar1 = SKSpriteNode(imageNamed: "bar1")
    var bar2 = SKSpriteNode(imageNamed: "bar2")
    var bar3 = SKSpriteNode(imageNamed: "bar3")
    var bar4 = SKSpriteNode(imageNamed: "bar4")
    var bar5 = SKSpriteNode(imageNamed: "bar5")


    override func didMoveToView(view: SKView) {

        physicsWorld.contactDelegate = self


     // BALL CHARACTERISTICS//


    //Green//

        greenBall.position  = CGPoint(x: frame.size.width*0.25,  y:  (frame.size.height * 0.95)/2)
        greenBall.anchorPoint = CGPoint(x: 0.5, y: 0.5)
        greenBall.physicsBody = SKPhysicsBody(circleOfRadius: greenBall.size.width/2)
        greenBall.physicsBody?.friction = 0
        greenBall.physicsBody?.restitution = 1
        greenBall.physicsBody?.linearDamping = 0
        greenBall.physicsBody?.angularDamping = 0
        greenBall.physicsBody?.affectedByGravity = false
        greenBall.name = BallCategoryName
        addChild(greenBall)

    //Red//

        redBall.anchorPoint = CGPoint(x: 0.5 , y: 0.5)
        redBall.position  = CGPoint(x: frame.size.width*0.75 ,  y:  (frame.size.height * 0.95)/2)
        redBall.physicsBody = SKPhysicsBody(circleOfRadius: greenBall.size.width/2)
        redBall.physicsBody?.friction = 0
        redBall.physicsBody?.restitution = 1
        redBall.physicsBody?.linearDamping = 0
        redBall.physicsBody?.angularDamping = 0
        redBall.physicsBody?.affectedByGravity = false
        redBall.physicsBody?.dynamic = false



        addChild(redBall)



        //////////////////////////////////////////////////////


        // SETTING UP SCENE//

        GameScene(size: self.view!.frame.size) //WHy not work

        let borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
        self.physicsBody = borderBody
        self.physicsBody?.friction = 0




        // BACKGROUND COLOR




        // PHYSICS //

        physicsWorld.gravity = CGVectorMake(0, 0)





        //////////////////////////////////////////////////////



        //  EXTRA BARS
        var midBar = SKShapeNode(rectOfSize: CGSize(width : frame.size.width, height: 3))
        var menuBar = SKShapeNode(rectOfSize: CGSize(width : frame.size.width, height: 3))
        var invisMenuBar = SKShapeNode(rectOfSize: CGSize(width: frame.size.width, height: 0.00001))
        var botBar = SKShapeNode(rectOfSize: CGSize(width : frame.size.width, height: 3))

        midBar.fillColor = SKColor.whiteColor()
        menuBar.fillColor = SKColor.whiteColor()
        invisMenuBar.fillColor = SKColor.purpleColor()
        botBar.fillColor = SKColor.whiteColor()

    ////MID BAR////
      var minus = frame.size.height * 0.049
        midBar.position = CGPoint(x: frame.size.width/2, y: (frame.size.height * 0.95)/2)
          addChild(midBar)



    ////BOT BAR////
           botBar.position = CGPoint(x: frame.size.width/2, y: 0 + (botBar.frame.size.height/2))
        addChild(botBar)


    ////MENU BAR////
            menuBar.position = CGPoint(x: frame.size.width/2, y: frame.size.height - (frame.size.height * 0.05))

         addChild(menuBar)

  //// INVIS MENU BAR ////
        invisMenuBar.position = CGPoint(x: frame.size.width/2, y: frame.size.height - (frame.size.height * 0.047))
        invisMenuBar.physicsBody = SKPhysicsBody(rectangleOfSize: invisMenuBar.frame.size)
        invisMenuBar.physicsBody?.friction = 0
        invisMenuBar.physicsBody?.restitution = 1
        invisMenuBar.physicsBody?.linearDamping = 0
        invisMenuBar.physicsBody?.angularDamping = 0
        invisMenuBar.physicsBody?.affectedByGravity = false
        invisMenuBar.physicsBody?.dynamic = false


        invisMenuBar.zPosition = 100
        menuBar.zPosition = 5

        addChild(invisMenuBar)




   // TEST STUFF WITH BARS //

        let chosenOne = SKSpriteNode(imageNamed: "bar4")
        chosenOne.position = CGPoint(x: frame.midX - chosenOne.frame.width/2 - 50 ,  y: self.frame.height * 0.75)





        addChild(chosenOne)


        //////////////////////////////////////////////////////


        //// SETTING UP MY CONTACT ///

        greenBall.physicsBody?.categoryBitMask = GreenBallCategory
        chosenOne.physicsBody?.categoryBitMask = RedBarCategory

        greenBall.physicsBody?.contactTestBitMask = RedBarCategory
        chosenOne.physicsBody?.contactTestBitMask = GreenBallCategory

        chosenOne.physicsBody = SKPhysicsBody(rectangleOfSize: chosenOne.frame.size)


        func didBeginContact(contact: SKPhysicsContact) {
            // 1. Create local variables for two physics bodies
            var firstBody: SKPhysicsBody
            var secondBody: SKPhysicsBody

            // 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody
            if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
                firstBody = contact.bodyA
                secondBody = contact.bodyB
            } else {
                firstBody = contact.bodyB
                secondBody = contact.bodyA
            }

            // 3. react to the contact between ball and bottom
            if firstBody.categoryBitMask == GreenBallCategory && secondBody.categoryBitMask == RedBarCategory {
                //TODO: Replace the log statement with display of Game Over Scene
                println("Hit bottom. First contact has been made.")
            }
        }

我跟随http://www.raywenderlich.com/84341/create-breakout-game-sprite-kit-swift来信。我已经设置了自我联系代表,两者确实相互碰撞,但它没有打印到日志上以证明碰撞事实真的有效。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您的didBeginContact方法位于didMoveToView方法内。由于SKPhysicsContactDelegate中的两种方法都是可选的:

protocol SKPhysicsContactDelegate : NSObjectProtocol {
    optional func didBeginContact(contact: SKPhysicsContact)
    optional func didEndContact(contact: SKPhysicsContact)
}

您没有被警告GameScene没有实施这些方法。

didBeginContact移出didMoveToView,您应该没问题。

(旁注:我建议你将didMoveToView分成几个较小的方法。这样可以更容易理解发生了什么,因为此刻,它看起来很臃肿。)