0
votes

I am making a game with sprite kit in xcode and i want let the game finish when the player touch the monster, do you know how to use categorymask?

here is part of the code :

import SpriteKit import GameplayKit import CoreMotion

class GameScene: SKScene, SKPhysicsContactDelegate {

var player : SKSpriteNode!
var playerCategory : UInt32 = 0x1 << 0
var gameTimer2 : Timer!
var possiblealien = ["alien", "alien2", "alien3"]
let alienCategory : UInt32 = 0x1 << 1

override func didMove(to view: SKView) {


    player = SKSpriteNode (imageNamed: "player")
    player.position = CGPoint (x: self.frame.size.width / 2 - 500 , y: 
    player.size.height / 2 - 560)
    player.size = CGSize(width: player.size.width * 6 ,
                         height: player.size.height * 6)
    self.addChild(player)
    player.zPosition = 2

    self.physicsWorld.gravity = CGVector (dx: 0 , dy: 0 )
    self.physicsWorld.contactDelegate = self
    player.physicsBody?.contactTestBitMask = playerCategory
    player.physicsBody? = SKPhysicsBody(rectangleOf: player.size)
    player.physicsBody?.isDynamic = true
    player.physicsBody?.categoryBitMask = monsterCategory
    player.physicsBody?.collisionBitMask = 0


    gameTimer2 = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(addalien), userInfo: nil, repeats: true)
     motionManager.accelerometerUpdateInterval = 0.2
     motionManager.startAccelerometerUpdates(to: OperationQueue.current!) { ( data: CMAccelerometerData?, error:Error?)in

     if let accelerometerData = data {
     let acceleration = accelerometerData.acceleration
     self.xAcceleration = CGFloat(acceleration.x) * 0.65 + self.xAcceleration * 0.25
     }
    }

@objc func addalien () {

possiblealien = GKRandomSource.sharedRandom().arrayByShufflingObjects(in: possiblealien) as! [String]

let alien = SKSpriteNode(imageNamed: possiblealien[0])

let randomalienposition = GKRandomDistribution(lowestValue: -480, highestValue:  +800 )

let position = CGFloat(randomalienposition.nextInt())

alien.position = CGPoint (x: position - 200 , y: self.frame.size.height * 0.5 + alien.size.height)

alien.physicsBody = SKPhysicsBody(rectangleOf: alien.size)
alien.physicsBody?.isDynamic = true

alien.physicsBody?.categoryBitMask = alienCategory
alien.physicsBody?.contactTestBitMask = playerCategory
alien.physicsBody?.collisionBitMask = 0
alien.zPosition = 2
alien.size = CGSize(width: alien.size.width * 1 ,
                      height: alien.size.height * 1)
self.addChild(alien)

let animationduration:TimeInterval = 6
var actionArray = [SKAction]()

actionArray.append(SKAction.move(to: CGPoint(x: position, y: -1000 ),duration: animationduration))
actionArray.append(SKAction.removeFromParent())

alien.run(SKAction.sequence(actionArray))

}

1
are you sure? player.physicsBody?.categoryBitMask = monsterCategory, it should be player.physicsBody?.categoryBitMask= playerCategory, and contactBitMask = monsterCategory - Simone Pistecchia

1 Answers

0
votes

I see your problem, you need to declare it's physics body in a Physics Category, Otherwise, there would be no body for it at all

struct PhysicsCategory {

static let None:UInt32 = 0

static let All:UInt32 = UInt32.max

static let Player:UInt32 = 0b1

Also, The contactbitmasks should be what they will collide with along with collisiontestbitmask. catergorybitmask should be the category of the sprite in the struct

ex:

let alien = SKSpritenode()
alien.physicsBody?.categoryBitMask = PhysicsCategory.Alien
alien.physicsBody?.contactBitMask = PhysicsCategory.Player
alien.physicsBody?.collisionTestBitMask = PhysicsCategory.Player

Also, you can use | to declare if they will collide with multiple things