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

你说的曾经没有我的故事 提交于 2020-01-03 17:07:53

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!