实现碰撞检测

时间:2015-04-01 13:46:31

标签: ios swift sprite-kit game-physics

基本上游戏包括玩家在屏幕上移动的篮子,游戏的目的是让玩家抓住从屏幕顶部落下的球。我目前正在尝试在球和篮子之间添加碰撞检测,但是面临困难,即实现这种碰撞检测。我是swift,sprite kit和app开发的新手,所以请帮忙。任何帮助,将不胜感激。我面临的另一个问题是所有的球落在屏幕的中心。一行代码应该执行,当球击中篮子并且随后球应该消失时,请帮助我,因为我是Spritekit的新手。

import SpriteKit

class GameScene: SKScene {

var basket = SKSpriteNode()

let actionMoveRight = SKAction.moveByX(50, y: 0, duration: 0.2)
let actionMoveLeft = SKAction.moveByX(-50, y: 0, duration: 0.2)
//let physicsBody = SKPhysicsBody(texture: , size: 3500)

override func didMoveToView(view: SKView) {
    /* Setup your scene here */

    self.physicsWorld.gravity = CGVectorMake(0.0, -0.5)
    self.backgroundColor = SKColor.whiteColor()
    basket = SKSpriteNode(imageNamed: "basket")
    basket.setScale(0.5)
    basket.position = CGPointMake(self.size.width/2, self.size.height/8)
    basket.size.height = 50
    basket.size.width = 75
    self.addChild(basket)

    let updateAction = SKAction.runBlock {



        var choice = arc4random_uniform(3)

        switch choice {
        case 1 :
            var ball1 = SKSpriteNode(imageNamed: "redBall")
            ball1.position = CGPointMake(self.size.width/3, self.size.height)
            ball1.setScale(0.5)
            ball1.size.height = 20
            ball1.size.width = 30
            ball1.physicsBody = SKPhysicsBody(circleOfRadius: ball1.size.height / 2.75)
            ball1.physicsBody!.dynamic = true
            self.addChild(ball1)
            println("0")


        case 0 :
            var ball2 = SKSpriteNode(imageNamed: "redBall")
            ball2.position = CGPointMake(self.size.width/5, self.size.height)
            ball2.setScale(0.5)
            ball2.size.height = 20
            ball2.size.width = 30
            ball2.physicsBody = SKPhysicsBody(circleOfRadius: ball2.size.height / 2.75)
            ball2.physicsBody!.dynamic = true
            self.addChild(ball2)
            println("1")


        case 2 :
            var ball3 = SKSpriteNode(imageNamed: "redBall")
            ball3.position = CGPointMake(self.size.width*4/5, self.size.height)
            ball3.setScale(0.5)
            ball3.size.height = 20
            ball3.size.width = 30
            ball3.physicsBody = SKPhysicsBody(circleOfRadius: ball3.size.height / 2.75)
            ball3.physicsBody!.dynamic = true
            self.addChild(ball3)
            println("2")


        default :
            println("Problem")

        }



    }

    let waitDuration : NSTimeInterval = 1.0
    let updateAndWaitAction = SKAction.sequence([updateAction,SKAction.waitForDuration(waitDuration)])
    let repeatForeverAction = SKAction.repeatActionForever(updateAndWaitAction)
    self.runAction(repeatForeverAction)
}

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    /* Called when a touch begins */

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        if location.x > basket.position.x {
            if basket.position.x < self.frame.maxX {
                basket.runAction(actionMoveRight)
            }
        }
        else {
            if basket.position.x > self.frame.minX {
                basket.runAction(actionMoveLeft)
            }
        }
    }
}

override func update(currentTime: CFTimeInterval) {


}

}

1 个答案:

答案 0 :(得分:1)

现在,您有一个代码,通常用于用户正在录制某些内容的情况。你需要使用BodyA&amp; BodyB并为您的节点分配位掩码。

self.basket.physicsBody?.categoryBitMask = ColliderType.basket.rawValue
self.basket.physicsBody?.contactTestBitMask = ColliderType.ball1.rawValue
self.basket.physicsBody?.collisionBitMask = ColliderType.ball1.rawValue
self.basket.physicsBody?.contactTestBitMask = ColliderType.ball2.rawValue
self.basket.physicsBody?.collisionBitMask = ColliderType.ball2.rawValue
self.basket.physicsBody?.contactTestBitMask = ColliderType.ball3.rawValue
self.basket.physicsBody?.collisionBitMask = ColliderType.ball3.rawValue

也为每一个球做到这一点。然后在func didBeginContact中你应该对Xcode说什么,如果你有动画或其他东西:

if (contact.bodyA.categoryBitMask == ColliderType.ball1.rawValue || contact.bodyB.categoryBitMask == ColliderType.ball1.rawValue) {
        yourGameOverFunc()
    }
    if (contact.bodyA.categoryBitMask == ColliderType.ball2.rawValue || contact.bodyB.categoryBitMask == ColliderType.ball2.rawValue) {
        yourGameOverFunc()
    }
    if (contact.bodyA.categoryBitMask == ColliderType.ball3.rawValue || contact.bodyB.categoryBitMask == ColliderType.ball3.rawValue) {
        yourGameOverFunc()
    }