Я делаю игру с набором спрайтов в xcode и хочу, чтобы игра закончилась, когда игрок дотронулся до монстра. Знаете ли вы, как использовать categorymask?
часть кода:
import SpriteKit import GameplayKit import CoreMotion
класс 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))
}