0
$\begingroup$

I'm trying to make my character jump using python script. And the character jumps but I want to check if the character is already jumping, and if he is, then not be able to jump again. But for some reason this code doesn't work.

# this triggers when I press space.
if isJump.positive:       #<-- this one doesnt work. but it still prints 'jumped'
   if jumping == False
       cont.activate(j)
       jumping = True
       print("Jumped")
       cont.deactivate(j)


# this triggers when I collide with an object with a property 'ground'    
if collide.positive: #<-- This whole block works fine
   jumping = False
   num = 0
   print(num)
   cont.deactivate(j)    
$\endgroup$
1
  • $\begingroup$ can we see the rest of the code please? this will help us help you. $\endgroup$ – Scalia Feb 8 '16 at 19:42
2
$\begingroup$

You store the current state in an intermediate variable. This can't work. You need to store it in a storage that will survive at least till the next call access. I wondered that you did not get any errors.

There are many ways to solve that issue.

FSM (build in state machine)

One way is to use the build-in state machine.

You can define a "jumping" state that, (when current) tells the character is currently jumping. On the other hand, if it is not current the character is not jumping. You do not even need Python to do that.

any state can transit to jumping state:

enter image description here

The jumping state cares the jump e.g. playing animations, detecting collisions and switch to other states

enter image description here

Property

You can create your own state machine. E.g. by using a property of the game object:

jumping = cont.owner.get("jumping")

# this triggers when I press space.
if not jumping and isJump.positive:       
       cont.activate(j) # what is a "j"?
       cont.owner["jumping"] = True
       print("Jumped")
       cont.deactivate(j) # this will not take effect

# Better separate start jumping from jumping. 
# This code does not need to be executed when there is no jumping, regardless of the collision sensor result.

if jumping and collide.positive: #<-- This whole block works fine
   cont.owner["jumping"] = False
   num = 0
   print(num)
   cont.deactivate(j)

There are much more ways. I hope it helps a bit.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.