Spritekit Collisions help

Hello,


I start in the world of SpriteKit and I have a little trouble with "CategoryBitMask"

I would like to have a "player" that can fire "torpedoes" on "Aliens" and if an "Alien" is in contact with the "Player" it explodes. But when I shoot a torpedo, the "Player" explodes as it was part of the category of aliens ...

I do not understand or is my mistake, can you help me and explain me?


thank you,


Max62


let EnemisCategory:UInt32 = 0x1 << 1
    let photontorpilleCategory:UInt32 = 0x1 << 0
    let AlienEtPlayerCategoryContact:UInt32 = 0x1 << 2

override func didMove(to view: SKView) {
        player.physicsBody?.categoryBitMask = AlienEtPlayerCategoryContact
        player.physicsBody?.contactTestBitMask = EnemisCategory
        player.physicsBody?.collisionBitMask = 0
        player.physicsBody?.usesPreciseCollisionDetection = true
        player.physicsBody = SKPhysicsBody(rectangleOf: player.size)

      gameTimer = Timer.scheduledTimer(timeInterval: 0.75, target: self, selector: #selector(addEnemis), userInfo: nil, repeats: true)
        motionManger.accelerometerUpdateInterval = 0.2
        motionManger.startAccelerometerUpdates(to: OperationQueue.current!) { (data:CMAccelerometerData?, error:Error?) in
            if let accelerometerData = data {
                let acceleration = accelerometerData.acceleration
                self.xAcceleration = CGFloat(acceleration.x) * 0.75 + self.xAcceleration * 0.25
            }
        }
}

    @objc func addEnemis () {
        possibleEnemiss = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: possibleEnemiss) as! [String]
    
        let Enemis = SKSpriteNode(imageNamed: possibleEnemiss[0])
    
        let randomEnemisPosition = GKRandomDistribution(lowestValue: 0, highestValue: 414)
        let position = CGFloat(randomEnemisPosition.nextInt())
        Enemis.size.width = 30 //Largeur
        Enemis.size.height = 30 //Hauteur
        Enemis.position = CGPoint(x: position, y: self.frame.size.height + Enemis.size.height)
    
        Enemis.physicsBody = SKPhysicsBody(rectangleOf: Enemis.size)
        Enemis.physicsBody?.isDynamic = true
    
        Enemis.physicsBody?.categoryBitMask = EnemisCategory
        Enemis.physicsBody?.contactTestBitMask = photontorpilleCategory | AlienEtPlayerCategoryContact
    
        Enemis.physicsBody?.collisionBitMask = 0
        self.addChild(Enemis)
    
        let animationDuration:TimeInterval = 6
    
        var actionArray = [SKAction]()
    
    
        actionArray.append(SKAction.move(to: CGPoint(x: position, y: -Enemis.size.height), duration: animationDuration))
        actionArray.append(SKAction.removeFromParent())
    
        Enemis.run(SKAction.sequence(actionArray))
    
    
    }

func didBegin(_ contact: SKPhysicsContact) {
        var firstBody:SKPhysicsBody
        var secondBody:SKPhysicsBody
  
        if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
            firstBody = contact.bodyA
            secondBody = contact.bodyB
        }else{
            firstBody = contact.bodyB
            secondBody = contact.bodyA
        }
  
        if (firstBody.categoryBitMask & photontorpilleCategory) != 0 && (secondBody.categoryBitMask & EnemisCategory) != 0 {
            torpilleDidCollideWithEnemis(torpilleNode: firstBody.node as! SKSpriteNode, EnemisNode: secondBody.node as! SKSpriteNode)
        }
  
        if (firstBody.categoryBitMask & EnemisCategory) != 0 && (secondBody.categoryBitMask & AlienEtPlayerCategoryContact) != 0 {
            ContactEntreAlienEtPlayer(Player1: secondBody.node as! SKSpriteNode, Enemis1: firstBody.node as! SKSpriteNode)
        }
    }

    func Attaque1() {
        self.run(SKAction.playSoundFileNamed("Attaque1.mp3", waitForCompletion: false))
   
        let torpilleNode = SKSpriteNode(imageNamed: "Attaque")
        torpilleNode.size.height = player.frame.height/10
        torpilleNode.size.width = player.frame.width/10
        torpilleNode.position = player.position
        torpilleNode.position.y = player.frame.minY
   
        torpilleNode.physicsBody = SKPhysicsBody(circleOfRadius: torpilleNode.size.width / 2)
        torpilleNode.physicsBody?.isDynamic = true
   
        torpilleNode.physicsBody?.categoryBitMask = photontorpilleCategory
        torpilleNode.physicsBody?.contactTestBitMask = EnemisCategory
        torpilleNode.physicsBody?.collisionBitMask = 0
        torpilleNode.physicsBody?.usesPreciseCollisionDetection = true
   
        self.addChild(torpilleNode)
   
        let animationDuration:TimeInterval = 7
   
   
        var actionArray = [SKAction]()
   
        actionArray.append(SKAction.move(to: CGPoint(x: player.position.x, y: self.frame.size.height - 10), duration: animationDuration))
        actionArray.append(SKAction.removeFromParent())
   
        torpilleNode.run(SKAction.sequence(actionArray))
   
    }

Replies

But when I shoot a torpedo, the "Player" explodes as it was part of the category of aliens ...


What do you mean? I cannot find any clue in your code that `player` was part of the category of aliens.

the Player should not be in the Alien category but the player still explodes like an alien

You wrote the "Player" explodes as it was part of the category of aliens, but the observable fact is

the player still explodes like an alien, OK?

Exactly, I am French, I may have misunderstood. the player must not be part of the category "alien" and it must not explode when it launches a torpedo.

Please describe the observable fact. Your code has not shown any behavior that it was part of the category of aliens, is that OK?


I mean, you have never put the `player` to be category of alien by code nor by sks file settings.

what do you mean by sks file parameters ?

I thought you knew you could setup physics on the sks editor.