Swift: How to make my character only jump in my game when he hits the ground?

I am developing a game where my character jumps from earth to earth. I do and do everything, except for my remaining problem, if you continue to click on the screen, it can continue to jump forever. I want him 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 it jump:

Knight.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
Knight.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 325))

I have it under touchesBegan, and it adds gravity and momentum, which allows him to jump. But I want him to be able to jump once every time he hits the ground. Please help if you can.

+4
source share
2 answers

, 0, . , .

if Knight.physicsBody?.velocity.y == 0 { Jump() }
+1

BOOL isJumping, false.

touchesBegan , isJumping true. , isJumping true.

( ), isJumping false.

0

Source: https://habr.com/ru/post/1682166/


All Articles