Swift SpriteKIT XCODE上的碰撞问题

时间:2015-03-13 00:23:26

标签: swift sprite-kit

我目前正在制作一个游戏,其中有一个大人物跑步和一个小人物跑步,我的游戏要求它在碰撞时检测并结束游戏,但是由于某种原因它不起作用而且没有检测到。我用println告诉我它什么时候发生,它什么也没做,它们只是跑过去了。这是我用过的代码。感谢您的时间!如果我做错了什么,请你告诉我吗?

由于

以下是我的DidMoveToView代码:

override func didMoveToView(view: SKView) {

    BigPerson.physicsBody?.friction = 0.0
    BigPerson.physicsBody?.linearDamping = 0.0





    self.hitagain = true
    self.respawn = false

    self.ChooseRandomScene()


    scoreLabelNode.fontName = "Helvetica-Bold"
    scoreLabelNode.position = CGPointMake(CGRectGetMidX(self.frame), self.frame.size.height / 1.26)
    scoreLabelNode.fontSize = 125
    scoreLabelNode.alpha = 0
    scoreLabelNode.zPosition = 9
    scoreLabelNode.text = "\(ScoreNumber)"
    self.addChild(scoreLabelNode)

    self.physicsWorld.contactDelegate = self


    self.physicsWorld.gravity = CGVectorMake( 0.0, -10.0 )

    var groundimage = SKTexture(imageNamed: "ground")
    groundimage.filteringMode = SKTextureFilteringMode.Linear
    var groundbody = SKTexture(imageNamed: "floorbody")
    var titleimage = SKTexture(imageNamed: "title1")
    var playbuttonimage = SKTexture(imageNamed: "play")
    playbuttonimage.filteringMode = SKTextureFilteringMode.Linear
    var leaderboardimage = SKTexture(imageNamed: "leaderboardbox")
    leaderboardimage.filteringMode = SKTextureFilteringMode.Linear

    var leftblockertexture = SKTexture(imageNamed: "floorbody")


    var removePipes = SKAction.removeFromParent()






    var txtField: UITextField = UITextField()
    txtField.frame = CGRectMake(50, 70, 200, 30)
    txtField.backgroundColor =  UIColor.grayColor()



    skyColor = SKColor(red: 113.0/255.0, green: 197.0/255.0, blue: 207.0/255.0, alpha: 1)
    self.backgroundColor = skyColor

    buttoncolour = SKColor.blackColor()













    //floorbody.color = SKColor.lightGrayColor()
    floorbody = SKSpriteNode(texture: groundbody)
    floorbody.size.width = self.size.width
    floorbody.size.height = self.size.height / 2
    floorbody.zPosition = 8
    floorbody.position = CGPoint(x: CGRectGetMidX(self.frame), y:self.frame.size.height / 7.7)
    self.addChild(floorbody)


    ground = SKSpriteNode(texture: groundimage)
    ground.zPosition = 9
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        ground.size.width = self.size.width/1.47
        ground.size.height = self.size.height / 18
    }else{
        ground.size.width = self.size.width/1.4
        ground.size.height = self.size.height / 16
    }
    ground.position = CGPoint(x: CGRectGetMidX(self.frame), y:self.frame.size.height / 2.70)
    self.addChild(ground)


    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        dummy.position = CGPoint(x: CGRectGetMidX(self.frame), y:self.frame.size.height / 2.80)
    }else{
        dummy.position = CGPoint(x: CGRectGetMidX(self.frame), y:self.frame.size.height / 2.76)
    }





    dummy.physicsBody = SKPhysicsBody(rectangleOfSize: CGSizeMake(self.frame.size.width, groundimage.size().height))
    dummy.physicsBody?.dynamic = false
    self.addChild(dummy)

    title = SKSpriteNode(texture: titleimage)
    title.zPosition = 7
    title.size.width = self.size.width / 3
    title.size.height = self.size.height / 13.5
    title.position = CGPoint(x: CGRectGetMidX(self.frame), y:self.frame.size.height / 1.17)
    self.addChild(title)

    //contactNode = SKSpriteNode(texture: leftblockertexture)
    contactNode.size.height = self.size.height / 3
    contactNode.size.width = self.size.width / 200
    contactNode.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) / 1.1)
    contactNode.physicsBody?.dynamic = false
    contactNode.physicsBody = SKPhysicsBody(rectangleOfSize: contactNode.size)
    contactNode.physicsBody?.allowsRotation = false
    contactNode.physicsBody?.categoryBitMask = BodyType.ScoreCategory.rawValue
    contactNode.physicsBody?.collisionBitMask = BodyType.BigPersonCategory.rawValue
    contactNode.physicsBody?.contactTestBitMask = BodyType.BigPersonCategory.rawValue
    self.addChild(contactNode)




    //leftblocker = SKSpriteNode(texture: leftblockertexture)
    leftblocker.zPosition = 9
    leftblocker.size.width = self.size.width / 20
    leftblocker.size.height = self.size.height / 5
    leftblocker.position = CGPoint(x: CGRectGetMidX(self.frame) / 1.87, y: CGRectGetMidY(self.frame))
    leftblocker.physicsBody?.dynamic = false
    leftblocker.physicsBody = SKPhysicsBody(rectangleOfSize: leftblocker.size)

    leftblocker.physicsBody?.categoryBitMask = BodyType.LeftCategory.rawValue
    leftblocker.physicsBody?.collisionBitMask = BodyType.BigPersonCategory.rawValue
    leftblocker.physicsBody?.contactTestBitMask = BodyType.BigPersonCategory.rawValue

    self.addChild(leftblocker)

    //rightblocker = SKSpriteNode(texture: leftblockertexture)
    rightblocker.zPosition = 9
    rightblocker.size.width = self.size.width / 20
    rightblocker.size.height = self.size.height / 5
    rightblocker.physicsBody?.dynamic = false
    rightblocker.position = CGPoint(x: CGRectGetMidX(self.frame) * 1.46, y: CGRectGetMidY(self.frame))
    rightblocker.physicsBody = SKPhysicsBody(rectangleOfSize: rightblocker.size)
    rightblocker.physicsBody?.categoryBitMask = BodyType.RightCategory.rawValue
    rightblocker.physicsBody?.collisionBitMask = BodyType.BigPersonCategory.rawValue
    rightblocker.physicsBody?.contactTestBitMask = BodyType.BigPersonCategory.rawValue


    self.addChild(rightblocker)





    playbutton = SKSpriteNode(texture: playbuttonimage)
    playbutton.zPosition = 7
    playbutton.size.width = self.size.width / 6
    playbutton.size.height = self.size.height / 7
    playbutton.position = CGPoint(x: CGRectGetMidX(self.frame) / 1.25, y:self.frame.size.height / 0.5 )
    playbutton.physicsBody = SKPhysicsBody(circleOfRadius: playbutton.size.height / 2.0)
    playbutton.physicsBody?.dynamic = true
    playbutton.physicsBody?.allowsRotation = false
    self.addChild(playbutton)

    leaderboardbutton = SKSpriteNode(texture: leaderboardimage)
    leaderboardbutton.zPosition = 7
    leaderboardbutton.size.width = self.size.width / 6
    leaderboardbutton.size.height = self.size.height / 7
    leaderboardbutton.position = CGPoint(x: CGRectGetMidX(self.frame) / 0.84, y:self.frame.size.height / 0.5 )
    leaderboardbutton.physicsBody = SKPhysicsBody(circleOfRadius: leaderboardbutton.size.height / 2.0)
    leaderboardbutton.physicsBody?.dynamic = true
    leaderboardbutton.physicsBody?.allowsRotation = false
    self.addChild(leaderboardbutton)


    ScoreSoundAction = SKAction.playSoundFileNamed(ScoreSound, waitForCompletion: false)



}



enum BodyType:UInt32
{
case BigPersonCategory = 1
case SmallPersonCategory = 2
case ScoreCategory = 4
case LeftCategory = 8
case RightCategory = 16
}

以下是产卵代码:

        self.BigPerson.size.height = self.size.height / 9
        self.BigPerson.size.width = self.size.width / 13.45
        self.BigPerson.position = CGPointMake(CGRectGetMidX(self.frame) / 2 , self.frame.size.height / 2.2)


        self.BigPerson.zPosition = 10
        BigPerson.physicsBody = SKPhysicsBody(rectangleOfSize: BigPerson.size)
        //BigPerson.physicsBody?.usesPreciseCollisionDetection = true
        BigPerson.physicsBody?.dynamic = false
        BigPerson.physicsBody?.categoryBitMask = BodyType.BigPersonCategory.rawValue
        BigPerson.physicsBody?.allowsRotation = false
        BigPerson.physicsBody?.collisionBitMask = BodyType.SmallPersonCategory.rawValue
        BigPerson.physicsBody?.contactTestBitMask = BodyType.SmallPersonCategory.rawValue



        let MoveRight = SKAction.moveByX(self.size.width * 1.20, y: 0, duration: MoveTime)
        BigPerson.runAction(MoveRight)

        self.addChild(self.BigPerson)

        ChooseSide = 1

        //

        //self.SmallPerson = SKSpriteNode(imageNamed: "")
        //self.SmallPerson = SKSpriteNode(texture: SmallPersonTexture)
        self.SmallPerson.size.height = self.size.height / 14
        self.SmallPerson.size.width = self.size.width / 21

        self.SmallPerson.position = CGPointMake(CGRectGetMidX(self.frame) * 1.5, self.frame.size.height / 2.3)


        self.SmallPerson.zPosition = 9
        SmallPerson.physicsBody = SKPhysicsBody(rectangleOfSize: SmallPerson.size)
        SmallPerson.physicsBody?.usesPreciseCollisionDetection = true
        SmallPerson.physicsBody?.dynamic = false
        SmallPerson.physicsBody?.categoryBitMask = BodyType.SmallPersonCategory.rawValue
        SmallPerson.physicsBody?.allowsRotation = false
        SmallPerson.physicsBody?.collisionBitMask = 0
        SmallPerson.physicsBody?.contactTestBitMask = BodyType.BigPersonCategory.rawValue


        SmallPerson.size.width = -SmallPerson.size.width
        self.addChild(self.SmallPerson)

以下是检测碰撞的代码:

        func didBeginContact(contact: SKPhysicsContact) {





        let contactMask = contact.bodyA.categoryBitMask | contact.bodyB.categoryBitMask

        let fadeAway = SKAction.fadeOutWithDuration(0.31)

        switch(contactMask)
        {
case BodyType.BigPersonCategory.rawValue | BodyType.SmallPersonCategory.rawValue:


            BigPerson.removeAllActions()
              StopGame()
     println("Test")


 default:
                 return
}
}

1 个答案:

答案 0 :(得分:0)

您必须设置contactDelegate以在代码中获取联系人检测回调。同时将SKPhysicsContactDelegate添加到SKScene

添加SKPhysicsContactDelegate

class GameScene : Scene, SKPhysicsContactDelegate

添加contactDelegate

self.physicsWorld.contactDelegate = self

将两个实体的dynamic属性设置为true。

BigPerson.physicsBody?.dynamic = true
SmallPerson.physicsBody?.dynamic = true