Q29vbA
Q29vbA

Reputation: 13

Global variable would not change

I called a global boolean in the function, but it still enters the 'if' even though the condition is not true.

I've been searching around but couldn't find a similar problem.

from Tkinter import *
import time

jumping = False
def jump(parts, high = 25): #to fix: can jump again in middle jump
    global jumping
    jumping = True
    for t in range(high*-3, 0, 1):
        w.move(parts[0], 0, 1)
        master.update()
        time.sleep(.0083)
    jumping = False
#thats the condition, which can be entered even while "jump()" is running.
if not jumping:
    master.bind('w', lambda x: jump(penguin_parts))

I expected it not to allow me to press 'w' and activate "jump()" when the function is in process but it actually lets me, just like jumping is still False.

Upvotes: 1

Views: 65

Answers (1)

MegaEmailman
MegaEmailman

Reputation: 545

There's nothing there to unbind the w key to the jump() function. Once it's bound, it remains bound until you unbind it.

from Tkinter import *
import time

jumping = False
def jump(parts, high = 25):
    if jumping == False:
        global jumping
        jumping = True
        for t in range(high*-3, 0, 1):
            w.move(parts[0], 0, 1)
            master.update()
            time.sleep(.0083)
        jumping = False
    master.bind('w', lambda x: jump(penguin_parts))

Upvotes: 2

Related Questions