创建像Doodle Jump这样的游戏,除了从平台上弹跳之外,我的英雄会从物体上弹回来收集点数并在游戏中取得进步。作为我的英雄"反弹"对象,我将它设置为removeFromParent,它就像它应该的那样工作。然而,如果我的英雄错过了物体并意外地击中了墙壁或地面,那么物体也会消失,这很奇怪,因为我的地面没有物理体。
这是我的GameScene的代码:
import SpriteKit
var player: Player!
var ground: Ground!
var snowflake: Snowflake!
var isFingerOnPlayer = false
var gameOver = false
var playerTouched = false
var touching: Bool = false
var lastTouch: CGPoint? = nil
let xPlayerForce: CGFloat = 40
let yPlayerForce: CGFloat = 50
var touchPoint: CGPoint = CGPoint()
struct Collision {
static let player: UInt32 = 1
static let snowflake: UInt32 = 2
}
class GameScene: SKScene, SKPhysicsContactDelegate {
override func didMoveToView(view: SKView) {
player = Player()
player.position = CGPointMake(20, 150)
addChild(player)
ground = Ground()
ground.position = CGPointMake(10, -37)
addChild(ground)
player.physicsBody?.linearDamping = 0.0
player.physicsBody?.angularDamping = 500.0
player.physicsBody = SKPhysicsBody(rectangleOfSize: player.size)
player.physicsBody?.allowsRotation = false
player.physicsBody?.mass = 0
player.physicsBody?.friction = 1
player.physicsBody?.dynamic = true
physicsWorld.contactDelegate = self
physicsWorld.gravity = CGVector(dx: 0.0, dy: -3.0)
self.backgroundColor = SKColor(red: 0.15, green: 0.15, blue: 0.3, alpha: 1.0)
snowflake = Snowflake()
addChild(snowflake)
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let touchLocation = touch.locationInNode(self)
lastTouch = touchLocation
player.physicsBody?.categoryBitMask = snowCategory
player.physicsBody?.contactTestBitMask = playerCategory
player.physicsBody?.affectedByGravity = true
player.physicsBody?.collisionBitMask = playerCategory
player.physicsBody?.usesPreciseCollisionDetection = true
}
}
func touchedPlayer() {
playerTouched = true
snowflake.removeFromParent()
player.bounceOff()
}
override func touchesMoved(touches: Set<NSObject>, withEvent event: UIEvent) {
lastTouch = nil
}
override func touchesEnded(touches: Set<NSObject>, withEvent event: UIEvent) {
lastTouch = nil
}
override func update(currentTime: NSTimeInterval) {
if let touch = lastTouch {
var xForce: CGFloat = 0.0
var yForce: CGFloat = 0.0
let xTouchOffset = (touch.x - player.position.x)
let yTouchOffset = (touch.y - player.position.y)
if xTouchOffset > 0.0 {
xForce = xPlayerForce
} else if xTouchOffset < 0.0 {
xForce = -xPlayerForce
} // else we do nothing
if yTouchOffset > 0.0 {
yForce = yPlayerForce
} else if xTouchOffset > 0.0 {
yForce = -yPlayerForce
}
// here you can choose whether you want it to push
// the player node down, using similar code from the
// above if statement
let impulseVector = CGVector(dx: xForce, dy: yForce)
player.physicsBody?.applyImpulse(impulseVector)
}
}
func didBeginContact(contact: SKPhysicsContact) {
touchedPlayer()
}
}
答案 0 :(得分:0)
尽管事实上还不清楚什么是雪花实体。
有一个代码:
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
你的框架周围有边缘。
来自说明:
从CGRect创建边循环。边缘没有卷,旨在用于创建静态环境。 边缘可能会与体积发生碰撞,但不能相互碰撞。
在方法中
func didBeginContact(联系方式:SKPhysicsContact)
使用contact.contactBodyA.categoryBitMask
忽略此联系人。(应为零)。