Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 211
Luyer Avatar asked Nov 08 '22 19:11

Luyer


1 Answers

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() }
like image 75
Maetschl Avatar answered Nov 15 '22 12:11

Maetschl