View Single Post
  #2  
Old 02-11-2009, 01:54 AM
moooh moooh is offline
Member
 
Join Date: Dec 2008
Posts: 19
vizact.whilekeydown will be added as events that will listen to the specified keystrokes. When you click the first ball, the events for the first ball will be created. When you click the second ball the events for the second ball will be added to the chain of events that listen to KEY_UP etc. This already includes the events of the first ball since you are creating new events instead of replacing the old.

The ball variable is also in local scope in the mouseclick function so any change you make to it (the assignment of the new ball through ball = viz.pick() for instance) will not affect the global ball variable.

What you should do is move the vizact statements out of the function and replace the second argument with a function of your own. This function could handle the translation for you. This way you'll only have 1 event for each key.


change the mouseclick function to only handle the picking of a ball
Code:
def mouseclick(button):
    global ball    #global reference to the ball
    if button == viz.MOUSEBUTTON_LEFT:
        pickObj = viz.pick()
        if pickObj.valid():
            ball = pickObj   #assign the current chosen ball to the global variable
and outside of this function add the event registers now with a call to your function instead with the pressed key as the third argument.
Code:
vizact.whilekeydown( viz.KEY_UP, myTranslate, viz.KEY_UP )
vizact.whilekeydown( viz.KEY_DOWN, myTranslate, viz.KEY_DOWN )
vizact.whilekeydown( viz.KEY_RIGHT, myTranslate, viz.KEY_RIGHT )
vizact.whilekeydown( viz.KEY_LEFT, myTranslate, viz.KEY_LEFT )
and create the translate function

Code:
def myTranslate(key):
    global ball
    if not ball:  #no ball selected, don't try to translate
        return
    else:
        if key == viz.KEY_UP:  #up is being pressed
            ball.translate  #... add the rest of the up translation of the ball here
        elif key == viz.KEY_DOWN:
            ball.translate # ... add the rest of the down translation of the ball here
        elif key == viz.KEY_LEFT:
            ball.translate # ... add the rest of the left translation of the ball here
        elif key == viz.KEY_RIGHT:
            ball.translate # ... add the rest of the right translation of the ball here
This way you will only translate the currently chosen ball as the key events will always call the same myTranslate function regardless of which ball is chosen.
Reply With Quote