Swift / SpriteKit多Sprite碰撞系统

时间:2018-08-18 04:48:37

标签: swift collision-detection

我目前正在迅速开发一款游戏。该代码创建了两个精灵节点,炸弹和现金。创建每个函数中的一个时,bombHit和cashCatch函数可以删除节点。创建多个炸弹和现金时,只能将最新炸弹检测为命中。反正有解决办法吗?

var screenSize:CGSize!
var start = SKSpriteNode()
var bombs = SKSpriteNode()
var wheelbarrow = SKSpriteNode()
var Background = SKSpriteNode()
var restart = SKSpriteNode()
var location: Int?
var bombcount = 0
var cashTest = SKSpriteNode()
var score = 0
var bombCount = 0
var aaa = 0
var bombWidth = 0
var decider = 0
var isGameRunning = true
var switcher = 0
var acount = 0
override func didMove(to view: SKView) {
    start = self.childNode(withName: "start") as! SKSpriteNode
    screenSize = self.frame.size
    aaa = Int(screenSize.width)
}


override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches{
        let location = touch.location(in: self)
        if (location.x <= (start.position.x + 175)) && (location.x >= (start.position.x - 175)) && (location.y >= (start.position.y - 175)) && (location.y <= (start.position.y + 175) && (start.isHidden == false)){
            start.isHidden = true
            createWheelbarrow()
            //rainingSkies()
            //createBombs()
            createCash()
            //cash.physicsBody?.applyImpulse(CGVector(dx: 0.0, dy: -150.0))
        }


    }
}
//func rainingSkies(){
    //for i in 1 ... 5{
        //createCash()
   // }
    //for i in 1 ... 3{
        //createBombs()
    //}
    //createCash()
    //createBombs()
//}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in touches {
        let location = touch.location(in: self)

        wheelbarrow.run(SKAction.moveTo(x: location.x, duration: 0.5))
    }
}
func startGame(){

}

func bombHit(){
    if (round(wheelbarrow.position.y + 3) >= round(bombs.position.y - 34)) && (round(bombs.position.x - 3) <= round(wheelbarrow.position.x + 75) && (round(bombs.position.x) >= (round(wheelbarrow.position.x) - 75) && round(bombs.position.y) >= round(wheelbarrow.position.y - 5))){
        bombs.position = CGPoint(x: 100000, y: 100000)
        bombs.removeFromParent()
        bombCount += 1
        endGame()
    }
    if (round(bombs.position.y) <= -(screenSize.height)){
        bombs.removeFromParent()
    }
}
func cashCatch(){
    if (round(wheelbarrow.position.y + 10) >= round(cashTest.position.y - 10)) && (round(cashTest.position.x - 47) <= round(wheelbarrow.position.x + 25) && (round(cashTest.position.x) >= (round(wheelbarrow.position.x) - 43))){
        cashTest.position = CGPoint(x: -5000, y: -5000)
        switcher = 1
    }
}
func createWheelbarrow(){
    wheelbarrow = SKSpriteNode(imageNamed: "wheelbarrow")
    wheelbarrow.position = CGPoint(x: (screenSize.width/3) - 100, y: (0 - (screenSize.height / 2) + 150))
    wheelbarrow.texture = SKTexture(imageNamed: "wheelbarrowImage")
    wheelbarrow.size = CGSize(width: (screenSize.width / 3), height: 100)
    wheelbarrow.zPosition = 1
    wheelbarrow.physicsBody?.isDynamic = false
    self.addChild(wheelbarrow)
}

func createCash(){
    switcher = 0
    cashTest = SKSpriteNode(imageNamed: "cashTest")
    randomizePosition()
    cashTest.position = CGPoint(x: CGFloat(bombWidth),y: screenSize.height - 300)
    cashTest.texture = SKTexture(imageNamed: "money dollar bill.png")
    cashTest.size = CGSize(width: 60.00, height: 36.00)
    cashTest.zPosition = 1
    cashTest.physicsBody?.mass = 200
    cashTest.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: cashTest.size.width, height: cashTest.size.height))
    cashTest.physicsBody?.isDynamic = false
    //cashTest.physicsBody?.allowsRotation = false
    //cashTest.physicsBody?.pinned = false
    //cashTest.physicsBody?.affectedByGravity = false
    //cashTest.physicsBody?.fieldBitMask = 0
    //cashTest.physicsBody?.linearDamping = 0
    //cashTest.physicsBody?.friction = 0

    self.addChild(cashTest)
    cashTest.run(SKAction.moveTo(y: (wheelbarrow.position.y + 10), duration: 5))
}


override func update(_ currentTime: TimeInterval) {
    bombHit()
    cashCatch()
    }

func randomizePosition(){
    decider = Int(arc4random_uniform(2))
    if decider == 1{
        bombWidth = Int((arc4random_uniform(UInt32(screenSize.width - 300))))
        bombWidth = -bombWidth
    }
    if decider == 0{
        bombWidth = Int(arc4random_uniform(UInt32(screenSize.width - 300)))
    }
}
createBombs(){
bombs = SKSpriteNode(imageNamed: "bombs")
randomizePosition()
bombs.position = CGPoint(x: CGFloat(bombWidth), y: round(screenSize.height - 300))
bombs.texture = SKTexture(imageNamed: "Bomb.png")
bombs.size = CGSize(width: 45, height: 45)
bombs.zPosition = 1
bombs.physicsBody?.mass = 100
bombs.physicsBody = SKPhysicsBody(circleOfRadius: 22.5)
bombs.physicsBody?.isDynamic = true
bombs.physicsBody?.allowsRotation = false
bombs.physicsBody?.pinned = false
bombs.physicsBody?.affectedByGravity = false
bombs.physicsBody?.fieldBitMask = 0
bombs.physicsBody?.linearDamping = 0
bombs.physicsBody?.friction = 0
self.addChild(bombs)
bombs.physicsBody?.applyForce(CGVector(dx: 0.0, dy: -250.0))


func endGame(){
    if bombCount == 3 {
        removeAllChildren()
    }
}

0 个答案:

没有答案