SKPhysics联系人无法正常运作

时间:2016-05-15 21:46:01

标签: swift sprite-kit skphysicsbody

我正在尝试在SpriteKit中使用SKPhysicsContactDelegate函数,它似乎无法正常工作。我希望一个精灵在击中另一个时执行动作。我在didBeginContact函数中设置了断点,由于某种原因,我的应用程序从不调用此函数。所有帮助赞赏。代码发布在下面。

struct PhysicsCatagory {
    static let Enemy :UInt32 = 0x1 << 0
    static let Slider :UInt32 = 0x1 << 1
    static let Circle :UInt32 = 0x1 << 2
}

class GameScene: SKScene, SKPhysicsContactDelegate {

    var EnemyTimer = NSTimer()

    var Circle = SKSpriteNode()
    var Slider = SKSpriteNode()
    var FastButton = SKNode()

    var Title = SKSpriteNode()
    var Text = SKSpriteNode()

    var Path = UIBezierPath()

    var gameStarted = Bool()

    override func didMoveToView(view: SKView) {

        self.physicsWorld.contactDelegate = self

        self.backgroundColor = UIColor.whiteColor()


        Circle = SKSpriteNode(imageNamed:"blueCircle")
        Circle.size = CGSize(width: 140, height: 140)
        Circle.position = CGPoint(x: CGRectGetMidX(self.frame), y: CGRectGetMidY(self.frame))
        Circle.zPosition = 1.0
        self.addChild(Circle)

        Slider = SKSpriteNode(imageNamed: "blocker1")
        Slider.size = CGSize(width: 15, height: 50)
        Slider.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 80)
        addChild(Slider)
        Slider.zPosition = 1.0


        moveClockWise()



            }


    func didBeginContact(contact: SKPhysicsContact) {
        if contact.bodyA.node != nil && contact.bodyB.node != nil{
            let firstBody = contact.bodyA.node as! SKSpriteNode
            let secondBody = contact.bodyB.node as! SKSpriteNode

        if ((firstBody.name == "Enemy") && (secondBody.name == "Slider")){

            collisionBall(firstBody, Slider: secondBody)

        }
        else if  ((firstBody.name == "Slider") && (secondBody.name == "Enemy")) {

            collisionBall(secondBody, Slider: firstBody)

        }


    }

    }

    func collisionBall(Enemy : SKSpriteNode, Slider : SKSpriteNode){

        Enemy.physicsBody?.dynamic = true
        Enemy.physicsBody?.affectedByGravity = true
        Enemy.physicsBody?.mass = 4.0
        Slider.physicsBody?.mass = 4.0

        Enemy.removeAllActions()

        Enemy.physicsBody?.contactTestBitMask = 0
        Enemy.physicsBody?.collisionBitMask = 0
        Enemy.name = nil



    }


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

        //Slider.hidden = false
        FastButton.hidden = false
        Title.hidden = true
        Text.hidden = true

        EnemyTimer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(GameScene.Enemies), userInfo: nil, repeats: true)

        //Physics
        Slider.physicsBody?.categoryBitMask = PhysicsCatagory.Slider
        Slider.physicsBody?.collisionBitMask = PhysicsCatagory.Enemy
        Slider.physicsBody?.contactTestBitMask = PhysicsCatagory.Enemy
        Slider.name = "Slider"
        Slider.physicsBody?.dynamic = true
        Slider.physicsBody?.affectedByGravity = true




        if gameStarted == false{

            gameStarted = true



        }
        else if gameStarted == true{


        }


    }

    func moveClockWise(){

        let dx = Slider.position.x / 2
        let dy = Slider.position.y / 2

        let rad = atan2(dy, dx)


        let Path = UIBezierPath(arcCenter: CGPoint(x: self.frame.width / 2, y: self.frame.height / 2), radius: 90, startAngle: rad, endAngle: rad + CGFloat(M_PI * 4), clockwise: true)

        let follow = SKAction.followPath(Path.CGPath, asOffset: false, orientToPath: true, speed: 150)
        //let rotate = SKAction.rotateByAngle(75, duration: 100)

        Slider.runAction(SKAction.repeatActionForever(follow).reversedAction())
        //Slider.runAction(SKAction.repeatActionForever(rotate).reversedAction())


    }


    func Enemies(){

        let Enemy = SKSpriteNode(imageNamed: "darkRedDot")
        Enemy.size = CGSize(width: 20, height: 20)


        //Physics
        Enemy.physicsBody = SKPhysicsBody(circleOfRadius: Enemy.size.width / 2)
        Enemy.physicsBody?.categoryBitMask = PhysicsCatagory.Enemy
        Enemy.physicsBody?.contactTestBitMask = PhysicsCatagory.Slider //| PhysicsCatagory.Circle
        Enemy.physicsBody?.collisionBitMask = PhysicsCatagory.Slider //| PhysicsCatagory.Circle
        Enemy.physicsBody?.dynamic = true
        Enemy.physicsBody?.affectedByGravity = false
        Enemy.name = "Enemy"

1 个答案:

答案 0 :(得分:0)

联系人没有开始,因为你的滑块没有物理体,因此无法识别接触。除非,它不在这个代码中,你的滑块没有物理体,但你的敌人呢。尝试声明Slider.phisicsBody = SKPhysicsBody(texture: SKTexture(imageNamed: "blocker1"), size: Slider.size)

另外,您应该阅读Swift中的标准命名约定。建议您的变量始终以小写字母开头(例如enemyTimercirclesliderfastButton等等。)