使用Sprite套件检测播放器与物体之间的碰撞

时间:2018-11-17 17:49:04

标签: xcode sprite-kit collision

我正在用xcode中的sprite工具包制作游戏,我希望当玩家触摸怪物时让游戏结束,您知道如何使用categorymask吗?

这是代码的一部分:

导入SpriteKit    导入GameplayKit    导入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))

}

1 个答案:

答案 0 :(得分:0)

我明白了您的问题,您需要在“物理”类别中声明它是物理主体,否则,根本没有它的主体

struct PhysicsCategory {

static let None:UInt32 = 0

static let All:UInt32 = UInt32.max

static let Player:UInt32 = 0b1

此外,contactbitmasks应该与碰撞测试位mask一起发生碰撞。 catergorybitmask应该是struct中子画面的类别

例如:

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

此外,您可以使用|声明他们是否会与多种事物相撞