我們將使用一個計數器。所以每次你打到一個平臺時,加上,每次你離開,減去。當你的計數器爲0時,你不在陸地上。
在某處爲身體類型創建一個枚舉......我把它放在我的GameScene類的正上方。
enum BodyType:UInt32 {
case player = 1
case platform = 2
}
設置你的騎士類這樣...
class Knight: SKSpriteNode {
// this variable will be toggled by didBegin Contact ... when 0 you are not land. Assumes that player starts on land. //
var onLandCounter = 1
func setUpKnight() {
/// call this in didMoveToView when setting up your nodes
/// Should have other stuff in here too .. but this is the important bit //
self.physicsBody?.categoryBitMask = BodyType.player.rawValue
self.physicsBody?.collisionBitMask = BodyType.platform.rawValue
self.physicsBody?.contactTestBitMask = BodyType.platform.rawValue
}
func jump() {
if (onLandCounter >= 1) {
self.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
self.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 275))
}
}
}
中的touchesBegan
,你反而會做
theKnight.jump()
注意,假定您創建了騎士類的一個實例,並呼籲它theKnight
訣竅然後是你需要一個平臺類,你會自定義類你的地面體例如
![enter image description here](https://i.stack.imgur.com/RJNHI.png)
class Platform: SKSpriteNode {
func setUpPlatform() {
self.physicsBody?.categoryBitMask = BodyType.platform.rawValue
self.physicsBody?.collisionBitMask = BodyType.player.rawValue | BodyType.platform.rawValue
self.physicsBody?.contactTestBitMask = BodyType.player.rawValue
}
}
然後使用didBeginContact使計數器來改變你的theKnight實例,並允許跳,因爲騎士已經觸及地面再次發生。我們添加並且現在計數器至少爲1,所以我們可以跳轉。
例如
@objc(didBeginContact:) func didBegin(_ contact: SKPhysicsContact) {
let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
//MARK: KNIGHT with PLATFORM
if (firstBody.categoryBitMask == BodyType.player.rawValue && secondBody.categoryBitMask == BodyType.platformSurface.rawValue) {
if let theKnight:Knight = firstBody.node as? Knight,
let thePlatform:Platform = secondBody.node as? Platform
{
theKnight.onLandCounter += 1
}
}
}
記住在你的GameScene正確
class GameScene: SKScene, SKPhysicsContactDelegate {
var theKnight: Knight = Knight()
override func didMove(to view: SKView) {
self.enumerateChildNodes(withName: "//*") {
node, stop in
if let thePlatform:Platform = node as? Platform {
thePlatform.setUpPlatform()
}
}
// MARK: SET UP PLAYER
// Assumes you call your player in the Scene "Player1"
//AND! you set it's custom class to Knight
if (self.childNode(withName: "Player1") != nil) {
theKnight = self.childNode(withName: "Player1") as! Knight
theKnight.setUpKnight()
}
}
}
最後一組的東西了!在didEndContact
@objc(didEndContact:) func didEnd(_ contact: SKPhysicsContact) {
let firstBody: SKPhysicsBody
let secondBody: SKPhysicsBody
if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
firstBody = contact.bodyA
secondBody = contact.bodyB
} else {
firstBody = contact.bodyB
secondBody = contact.bodyA
}
//MARK: KNIGHT with PLATFORM
if (firstBody.categoryBitMask == BodyType.player.rawValue && secondBody.categoryBitMask == BodyType.platformSurface.rawValue) {
if let theKnight:Knight = firstBody.node as? Knight,
let thePlatform:Platform = secondBody.node as? Platform
{
theKnight.onLand -= 1
}
}
}
不要忘了自定義類你的騎士!
![enter image description here](https://i.stack.imgur.com/5ld4r.png)
現在你有一個設置...在跳轉功能纔會啓用時,您的播放器,使一個「勤」節點接觸上。
調整此解決方案,然而它適合您自己的代碼。讓我知道,如果我忘了支撐或什麼。
重點是....你只是使用計數器來防止使用跳轉功能。
可能的重複[Swift:如何讓我的角色在我的遊戲中只有在他碰到地面時才能跳躍?](https://stackoverflow.com/questions/45290346/swift-how-to-make-我的角色在我的遊戲中只能跳躍一次他擊中) –
請添加此信息(字符可以跳躍,只要屏幕輕點,而不是一次)到您的較早問題,因爲它們密切相關。 –
@SteveIves我更新了問題 – Luyer