精灵工具包碰撞不起作用

时间:2018-05-01 00:03:24

标签: swift sprite-kit collision

这是我一直在努力的游戏。总之,有一个名为敌人的移动块,我希望它与一个名为invisibleGround2的不可见静态块相撞。当他们认为碰撞但是他们没有碰撞时我打印了它。我已经通过申请和其他人阅读了每个快速碰撞文件,我不知道什么是错的。任何帮助将不胜感激!

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {
    var orb = SKSpriteNode(imageNamed: "orb")
    var scrollingG:scrollingGround?
    var invisibleGround = SKSpriteNode(imageNamed: "invisible")
    var invisibleGround2 = SKSpriteNode(imageNamed: "invisible")

    let enemies = [SKSpriteNode(imageNamed: "blueE.png"), SKSpriteNode(imageNamed: "redE.png")]
    let enemyCategory : UInt32 = 1
    let jumperCategory : UInt32 = 1
    let rotateDuration = 2
    let enemyMoveSpeed = 5
    let groundScrollingSpeed = 5

    func createOrb(){
        let orbConst = frame.size.width/2
         let xConstraint = SKConstraint.positionX(SKRange(constantValue: orbConst))
        orb.position = CGPoint(x: frame.size.width/2, y: 480)
        orb.physicsBody = SKPhysicsBody(texture: orb.texture!, size: orb.texture!.size())
        orb.constraints = [xConstraint]

        self.addChild(orb)
        }

    func createScrollingGround () {
        scrollingG = scrollingGround.scrollingNodeWithImage(imageName: "ground", containerWidth: self.size.width)
        scrollingG?.scrollingSpeed = CGFloat(groundScrollingSpeed)
        scrollingG?.anchorPoint = .zero
        self.addChild(scrollingG!)
    }

    func createGround(){
        invisibleGround2.size.width = 1
        invisibleGround2.size.height = 1
        invisibleGround2.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width:1, height: 100))
        invisibleGround2.physicsBody?.isDynamic = false
        invisibleGround2.position = CGPoint(x: 530, y: 191)
        invisibleGround2.physicsBody?.categoryBitMask = jumperCategory
        invisibleGround2.physicsBody?.collisionBitMask = enemyCategory
        invisibleGround2.physicsBody?.contactTestBitMask = enemyCategory

        invisibleGround2.name = "jumper"
        invisibleGround.position = CGPoint(x: 0, y: 190)
        invisibleGround.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: self.size.width * 3, height: 10))
        invisibleGround.physicsBody?.isDynamic = false
        self.addChild(invisibleGround2)
        self.addChild(invisibleGround)
    }

    func getRandomEnemy(fromArray array:[SKSpriteNode])->SKSpriteNode{
        return array[Int(arc4random_uniform(UInt32(array.count)))]
    }

    func spawnEnemy() {
        if let enemy = getRandomEnemy(fromArray: enemies).copy() as? SKSpriteNode {
            enemy.position = CGPoint(x: frame.size.width + frame.size.width/3, y: 440)
            enemy.physicsBody = SKPhysicsBody(texture: enemy.texture!, size: enemy.texture!.size())
            if enemy.size.width < 95 {
                 enemy.physicsBody?.categoryBitMask = enemyCategory
               enemy.physicsBody?.collisionBitMask = jumperCategory
                enemy.physicsBody?.contactTestBitMask = jumperCategory
            }
            enemy.name = "enemy"
            self.addChild(enemy)
            let moveLeft = SKAction.moveBy(x: -1500, y: 0, duration: TimeInterval(enemyMoveSpeed))
            enemy.run(moveLeft)
        }
    }

    func addEnemies () {
        self.run(SKAction.repeatForever(SKAction.sequence([SKAction.run {
            self.spawnEnemy()
        }, SKAction.wait(forDuration: 4)])))
    }

    func jump() {
        orb.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 335))
    }

    func rotate() {
        let rotate = SKAction.rotate(byAngle: CGFloat(M_PI * -2.55), duration: TimeInterval(rotateDuration))
        let repeatAction = SKAction.repeatForever(rotate)
        orb.run(repeatAction)
    }

    override func didMove(to view: SKView) {
        createScrollingGround()
        createOrb()
        createGround()
        rotate()
        addEnemies()
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        jump()
    }

    override func update(_ currentTime: TimeInterval) {
        if self.scrollingG != nil {
            scrollingG?.update(currentTime: currentTime)

            func didBegin(_ contact: SKPhysicsContact) {
                let bodyA = contact.bodyA.categoryBitMask
                let bodyB = contact.bodyB.categoryBitMask

                if bodyA == jumperCategory && bodyB == enemyCategory {

                    print("hit")
                } else if  bodyA == enemyCategory && bodyB == jumperCategory {
                    print("hit 2")
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

不是一个swer,而是要检查一些事情:

  • 您是否已将场景的physicsworld delegate属性设置为 self
  • didBegin移到update
  • 之外
  • 是&#39;敌人&#39;精灵的宽度&lt; 95?
  • 添加print("Contact detected")作为重新定位的didBegin的第一行,这样您至少知道已检测到某些联系人。

请参阅我的答案https://stackoverflow.com/a/43605825/1430420,了解一个简单的SK碰撞演示,这可能会有所帮助 - 我认为它需要更新Swift 4,我会尝试这样做。