swift - Sprite 套件碰撞不起作用

标签 swift sprite-kit collision

这是我一直在做的游戏。总之,有一个名为 enemy 的移动 block ,我希望它与一个名为 invisibleGround2 的不可见静态 block 发生碰撞。当它们应该碰撞但它们没有碰撞时,我让它打印出来。我阅读了 apply 和其他人提供的所有快速碰撞文档,但我不知道哪里出了问题。任何帮助将不胜感激!

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")
                }
            }
        }
    }
}

最佳答案

不是 swer,而是一些需要检查的东西:

  • 您是否将场景的 physicsworld delegate 属性设置为 自己?
  • didBegin 移到 update 之外
  • “敌人” Sprite 的宽度是否 < 95?
  • 添加 print("Contact detected") 作为您重新定位的 didBegin 的第一行,这样您至少知道一些联系人有被检测到。

在这里查看我的答案 https://stackoverflow.com/a/43605825/1430420对于一个可能有帮助的简单 SK 碰撞演示 - 我认为它需要更新到 Swift 4,我会尝试这样做。

关于swift - Sprite 套件碰撞不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50109786/

相关文章:

sprite-kit - SKRenderer——神秘类

javascript - html5 Canvas 弹性碰撞方 block

javascript - 使一个圆重叠时不被另一个圆排斥?

swift - SpriteKit SKPhysicsJoint修复了奇怪的行为

swift - Vapor Hello 样本中的强引用

ios - Swift 闭包内存使用

ios - Swift 中的匿名内部类

swift 3 - 制作一个flappybird游戏,水平管道麻烦

ssl - OpenSSL 和 CAPI - 证书冲突

ios - Swift NSString 函数语法用法