我该如何设置才能让我的玩家和 object 拥有物理 body?
How do I set it up so that only my player and an object have a physics body?
创建像 Doodle Jump 这样的游戏,除了从平台弹回外,我的英雄从 objects 弹回以收集积分和游戏进度。作为我的英雄 "bounces off" object,我将其设置为 removeFromParent 并且它正常工作。但是,如果我的英雄错过了 object 并且不小心撞到墙壁或地面,object 也会消失,这很奇怪,因为我的地面没有物理 body.
这是我的 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()
}
}
尽管不清楚什么是雪花实体。
有一个密码:
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
你的框架有优势。
来自描述:
从 CGRect 创建边循环。边缘没有体积,旨在用于创建静态环境。 边缘可以与体积的主体碰撞,但不能相互碰撞。
方法中
func didBeginContact(联系人:SKPhysicsContact)
使用 contact.contactBodyA.categoryBitMask
忽略此联系人。(应为零)。
创建像 Doodle Jump 这样的游戏,除了从平台弹回外,我的英雄从 objects 弹回以收集积分和游戏进度。作为我的英雄 "bounces off" object,我将其设置为 removeFromParent 并且它正常工作。但是,如果我的英雄错过了 object 并且不小心撞到墙壁或地面,object 也会消失,这很奇怪,因为我的地面没有物理 body.
这是我的 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()
}
}
尽管不清楚什么是雪花实体。
有一个密码:
self.physicsBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
你的框架有优势。
来自描述:
从 CGRect 创建边循环。边缘没有体积,旨在用于创建静态环境。 边缘可以与体积的主体碰撞,但不能相互碰撞。
方法中
func didBeginContact(联系人:SKPhysicsContact)
使用 contact.contactBodyA.categoryBitMask
忽略此联系人。(应为零)。