code getting executed one more time than it should

Hi

so i haven’t really used python with blender wince 2.49 and im getting back into the fray. Im getting some basic player movement thing going on.

I want it so that when it presses the w key it sets the a game property to .05 and than when the button isnt being pressed it .005 is subtracted from the property. the properties representing 3 axis of rotation re copied to dRot at the top of the code. Something weird is going on though

it works the way it should and the rotation gets to 0 but than despite there being an if statement that should prevent this, it subtracts .005 one more time.
Here is the code

 
 
from bge import logic

from bge import events
 
cont = logic.getCurrentController()
obj = cont.owner


motion = cont.actuators['Motion']
key = logic.keyboard.events


tx = obj["tx"]
ty = obj.get("tx")
tz = obj.get("tx")
motion.dRot = [tx,ty,tz]
cont.activate(motion)
#print("hi")
moving = 0
wkey = obj['wkey']


keyboard = bge.logic.keyboard
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED
JUST_RELEASED = bge.logic.KX_INPUT_JUST_RELEASED       


if keyboard.events[bge.events.WKEY] == JUST_ACTIVATED:
    obj['tx'] = .05    
    moving += 1
    obj['wkey'] = True
    print(tx)
if keyboard.events[bge.events.AKEY] == JUST_ACTIVATED:
    moving += 1
if keyboard.events[bge.events.SKEY] == JUST_ACTIVATED:
    moving += 1
if keyboard.events[bge.events.DKEY] == JUST_ACTIVATED:
    moving += 1  
            
            
if keyboard.events[bge.events.WKEY] == JUST_RELEASED:
     obj['wkey'] = False
     moving -= 1 
     print(moving)
if keyboard.events[bge.events.WKEY] == JUST_RELEASED:
     moving -= 1 
if keyboard.events[bge.events.SKEY] == JUST_RELEASED:
     moving -= 1 
if keyboard.events[bge.events.DKEY] == JUST_RELEASED:
     moving -= 1     
    

if wkey == False:
    if obj['tx'] > 0:
        print("hey")
        moving -= 1
        obj['tx'] -= .005

    if ty != 0:
        moving -= 1
        ty -= .1
    if tz != 0:
        moving -= 1
        tz -= .1

I don’t know whether you ever figured this out or not…

My guess would be a floating-point issue. If obj[‘tx’] is, for instance, 0.000000000000000000000000001 then it would pass your conditional logic.

edit wrong thread.

#I once had a problem with keyboard events. I think the problem is that Blender executed your code a lot of times #while you are holding the key.

while (keyboard.events[bge.events.WKEY] == bge.logic.KX_INPUT_ACTIVE) :moving += 11

#This will halt Blender to remain in the same script. But be aware this might cause performance issues