我的代码中存在问题。
我正在创建一个游戏,我需要有一个可以同时出现多次的精灵,这样做我创建了一个类,所以我可以多次执行addChild(障碍),它会产生一个SKSpriteNode与另一个完全相似。
我的问题是我想检查我的播放器和障碍物之间的碰撞但是因为它来自同一个SKSpriteNode,计算机无法知道我在说什么障碍。
以下是我创建播放器和障碍的方法:
import SpriteKit
class Obstacle: SKSpriteNode {
init() {
let obstacleTexture = SKTexture(imageNamed: "obstacle")
super.init(texture: obstacleTexture, color: UIColor.clearColor(), size: CGSize(width: 80, height: 80))
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
class GameScene: SKScene {
var player:SKSpriteNode!
override func didMoveToView(view: SKView) {
//player setup
let playerTexture = SKTexture(imageNamed: "player")
player = SKSpriteNode(texture: playerTexture)
player.position = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.2)
}
//how I spawn an obstacle
func spawnObstacle() {
let obstacle = Obstacle()
//obstacle position setup
obstacle.position.x = CGFloat(arc4random()) % self.frame.size.width
obstacle.position.y = self.frame.size.height + 200
//random spin action setup
var rotateObstacle = SKAction.rotateByAngle(CGFloat(M_PI), duration: Double((drand48() + 1) * 0.75))
if random() % 2 == 0 {
rotateObstacle = SKAction.rotateByAngle(CGFloat(M_PI), duration: Double((drand48() + 1) * 0.75))
}else{
rotateObstacle = SKAction.rotateByAngle(-CGFloat(M_PI), duration: Double((drand48() + 1) * 0.75))
}
let rotateObstacleForever = SKAction.repeatActionForever(rotateObstacle)
//random move action setup
let moveObstacle = SKAction.moveTo(CGPointMake(CGFloat(arc4random()) % self.frame.size.width, -200), duration: Double((drand48() + 1) * 1.5))
//running the actions
obstacle.runAction(rotateObstacleForever)
obstacle.runAction(moveObstacle)
addChild(obstacle)
}
}
}
如何检测玩家何时与任何障碍物碰撞?
答案 0 :(得分:0)
要检测碰撞,您可以使用SpriteKit物理。
这里有3个元素涉及假设的碰撞:
<强>建议强>
将此参数设置为调试阶段以查看物理对象边界:
skView.showsPhysics = true
代码示例(警告: - 此代码只是开始实现您的物理的一个点,我不知道您的项目的其余部分,所以您的工作将是正确,因为你认为它对你的对象更好):
enum CollisionTypes: UInt32 {
case Field = 1
case Player = 2
case Obstacle = 4
}
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
self.physicsWorld.gravity = CGVectorMake(0, 0) // set your gravity value
self.physicsWorld.contactDelegate = self
let fieldBody = SKPhysicsBody.init(edgeLoopFromRect: self.frame)
self.physicsBody = fieldBody
self.physicsBody!.affectedByGravity = false
self.physicsBody!.usesPreciseCollisionDetection = true
self.physicsBody!.dynamic = true
self.physicsBody!.mass = 0.8
self.physicsBody!.friction = 0
self.physicsBody!.linearDamping = 0
self.physicsBody!.angularDamping = 0
self.physicsBody!.restitution = 0
self.physicsBody!.categoryBitMask = CollisionTypes.Field.rawValue
self.physicsBody!.contactTestBitMask = CollisionTypes.Player.rawValue
// Prepare the player
player.physicsBody = SKPhysicsBody(circleOfRadius: player.frame.width/2)
player.physicsBody!.affectedByGravity = false
player.physicsBody!.restitution = 0.0
player.physicsBody!.linearDamping = 0
player.physicsBody!.friction = 0.3
player.physicsBody!.dynamic = true
player.physicsBody!.mass = 0.2
player.physicsBody!.allowsRotation = false
player.physicsBody!.categoryBitMask = CollisionTypes.Player.rawValue
player.physicsBody!.contactTestBitMask = CollisionTypes.Field.rawValue | CollisionTypes.Obstacles.rawValue
player.physicsBody!.collisionBitMask = CollisionTypes.Field.rawValue | CollisionTypes.Obstacles.rawValue
//Prepare the obstacles (you must do it in your obstacle class)
obstacle.physicsBody = SKPhysicsBody(circleOfRadius: obstacle.frame.width/2)
obstacle.physicsBody!.affectedByGravity = false
obstacle.physicsBody!.restitution = 0.0
obstacle.physicsBody!.linearDamping = 0
obstacle.physicsBody!.friction = 0.3
obstacle.physicsBody!.dynamic = true
obstacle.physicsBody!.mass = 0.8
obstacle.physicsBody!.allowsRotation = true
obstacle.physicsBody!.categoryBitMask = CollisionTypes.Obstacle.rawValue
obstacle.physicsBody!.contactTestBitMask = CollisionTypes.Player.rawValue
obstacle.physicsBody!.collisionBitMask = CollisionTypes.Player.rawValue
}
func didBeginContact(contact: SKPhysicsContact) {
if (contact.bodyA.categoryBitMask == CollisionTypes.Player.rawValue &&
contact.bodyB.categoryBitMask == CollisionTypes.Obstacle.rawValue) {
print("contact between Player and Obstacle")
}
if (contact.bodyA.categoryBitMask == CollisionTypes.Player.rawValue &&
contact.bodyB.categoryBitMask == CollisionTypes.Field.rawValue) {
print("contact between Player and Field")
}
}
}