1

In the code below I have a couple nodes but right now I am just trying to detect collisions between the player and the enemy bullet( I refer to it as "BBullet") but under the function that detects collisions, nothing pops up.

import SpriteKit
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

    var player = SKSpriteNode()

    var firebutton = SKShapeNode()
    var bullet = SKSpriteNode()

    enum CategoryMask : UInt32 {
        case player = 1
        case GBullet = 2
        case BBullet = 3
        case enemyships = 4
    }

    override func didMove(to view: SKView) {
        player = SKSpriteNode(color: .red, size: CGSize(width: 150, height: 150))
        player.physicsBody?.affectedByGravity = false
        player.position = CGPoint(x: 100, y: 375)
        player.physicsBody?.isDynamic = true
        player.physicsBody?.restitution = 1
        player.physicsBody?.categoryBitMask = CategoryMask.player.rawValue
        player.physicsBody?.collisionBitMask = CategoryMask.BBullet.rawValue
        player.physicsBody?.contactTestBitMask = CategoryMask.BBullet.rawValue
        player.name = "Player"

        addChild(player)
        firebutton = SKShapeNode(circleOfRadius: 75)
        firebutton.position = CGPoint(x: 1000, y: 150)
        firebutton.alpha = 0.7
        firebutton.physicsBody?.isDynamic = false
        firebutton.physicsBody?.affectedByGravity = false
        firebutton.physicsBody?.categoryBitMask = 0
        firebutton.physicsBody?.collisionBitMask = 100
        firebutton.physicsBody?.contactTestBitMask = 100
        firebutton.physicsBody?.contactTestBitMask = 0
        firebutton.physicsBody?.collisionBitMask = 0
        addChild(firebutton)
    }

    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        for touch in touches{
            spawnenemybullets()
            let position = touch.location(in: self)
            if firebutton.contains(position){
                bullet = SKSpriteNode(color: .orange, size: CGSize(width: 20, height: 10))
                bullet.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: 10))
                bullet.physicsBody?.affectedByGravity = false
                bullet.physicsBody?.isDynamic = true
                bullet.physicsBody?.allowsRotation = false
                bullet.position.x = player.position.x + 90
                bullet.position.y = player.position.y
                bullet.physicsBody?.mass = 0.01
                bullet.physicsBody?.categoryBitMask = CategoryMask.GBullet.rawValue
                bullet.physicsBody?.collisionBitMask = CategoryMask.enemyships.rawValue
                bullet.physicsBody?.contactTestBitMask = CategoryMask.enemyships.rawValue
                bullet.name = "FBullet"
                addChild(bullet)
                bullet.physicsBody?.applyForce(CGVector(dx: 400, dy: 0))

            }
        }
    }

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

             if player.contains(position) {

                if position.y > 85 && position.y < 665 && position.x > 85 && position.x < 640{
                    player.position = position
                }
            }
        }
    }

    func spawnenemybullets () {
        bullet = SKSpriteNode(color: .red, size: CGSize(width: 20, height: 10))
        bullet.physicsBody = SKPhysicsBody(rectangleOf: CGSize(width: 20, height: 10))
        bullet.physicsBody?.affectedByGravity = false
        bullet.physicsBody?.isDynamic = true
        bullet.physicsBody?.allowsRotation = false
        bullet.position.x = 1300
        bullet.position.y = 200
        bullet.physicsBody?.mass = 0.01
        bullet.physicsBody?.categoryBitMask = CategoryMask.BBullet.rawValue
        bullet.physicsBody?.collisionBitMask = CategoryMask.player.rawValue
        bullet.physicsBody?.contactTestBitMask = CategoryMask.player.rawValue
        bullet.name = "EnemyBullet"
        addChild(bullet)
        bullet.physicsBody?.applyForce(CGVector(dx: -400, dy: 0))
    }

    func didBegin(_ contact: SKPhysicsContact) {
        print("Contact Happened")
    }

}

The two Sprites both have their contact masks set up but for some reason they are not colliding. When I test the project on my phone, they just go right through each other. How do I get them to contact each other?

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116

1 Answers1

1

You haven't created a physicsBody for player (or fireButton).

You also need to make yourself the physics contact delegate:

class GameScene: SKScene, SKPhysicsContactDelegate { physicsWorld.contactDelegate = self

Edit: my step-by-step guide for collisions and contacts: https://stackoverflow.com/a/51041474/1430420

A guide to collision and contactTest bit masks: https://stackoverflow.com/a/40596890/1430420

Manipulating bit masks to turn individual collisions and contacts off and on. https://stackoverflow.com/a/46495864/1430420

Steve Ives
  • 7,894
  • 3
  • 24
  • 55