问题
I'm developing a game where my character jumps from land to land. I have everything down and done except my remaining problem is if you keep tapping the screen he can keep jumping forever. I want it so he has to hit the ground before he can jump again.
import SpriteKit
import GameplayKit
class GameScene: SKScene, SKPhysicsContactDelegate {
struct PhysicsCatagory {
static let Knight : UInt32 = 0x1 << 1
static let land : UInt32 = 0x1 << 2
}
var Knight = SKSpriteNode()
var land = SKSpriteNode(imageNamed: "CJLand4")
This is my current code that makes him jump:
Knight.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
Knight.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 325))
I have this under touchesBegan
and it adds gravity and on impulse it allows him to jump. But I only want him to be able to jump once every time he hits land. Please help if you can.
回答1:
You can check if vertical velocity is 0, and Knight only jump if are landing. It really depends on the type of jump you are looking for.
if Knight.physicsBody?.velocity.y == 0 { Jump() }
回答2:
Simply add a BOOL property to your scene called something like isJumping
which is initialised to false
.
Then, in touchesBegan
check to see if isJumping
is true. If not, perform the jump and set isJumping
to true
.
When the jump is over (knight has contacted the ground), set isJumping
back to false
.
来源:https://stackoverflow.com/questions/45290346/swift-how-to-make-my-character-in-my-game-only-be-able-to-jump-once-he-hits-the