1

This is a game I have been working on. In summary there is a moving block named enemy and I want it to collide with an invisible static block called invisibleGround2. I have it printing hit when they supposedly collide but they are not colliding. Ive read every swift collision documentation by apply and others out there and I dont know whats wrong. Any help would be much appreciated!

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")
                }
            }
        }
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579

1 Answers1

0

Not an swer, but some things to check:

  • Have you set the scene’s physicsworld delegate property set to self?
  • Move didBegin outside of update
  • Is the 'enemy' sprite's width < 95?
  • Add a print("Contact detected") as the first line of your relocated didBegin so you at least know that some contact has been detected.

See my answer here https://stackoverflow.com/a/43605825/1430420 for a simple SK collision demo which might help - I think it needs updates to Swift 4 which I'll try and do.

Steve Ives
  • 7,894
  • 3
  • 24
  • 55
  • to be more specific, he is could possibly be setting the physics world in the ViewController when the scene is presented, so it should be `scene.delegate = scene` (the scene variable on a scene is equivalent to self) – Knight0fDragon May 03 '18 at 15:13