![]() |
how can I stop an action?
I've solved the problem of selecting and moving an object but now I can't deselect the object.
when I choose my first object and move it, it works properly but when i select my second object the first one moves also. how can i stop the first one? this is the code i wrote: import viz viz.go() viz.add('tut_ground.wrl') viz.clearcolor(0.5,0.5,1) soccerball1 = viz.add('soccerball.ive') soccerball2 = viz.add('soccerball.ive') soccerball3 = viz.add('soccerball.ive') soccerball1.translate(-1,1.5,3) soccerball2.translate(0,1.5,3) soccerball3.translate(1,1.5,3) arrow = viz.add('arrow.wrl') arrow.scale(.1,.1,.1) arrow.visible(viz.OFF) def mouseclick(button): if button == viz.MOUSEBUTTON_LEFT: ball = viz.pick() if ball.valid(): vizact.whilekeydown( viz.KEY_UP, ball.translate, 0, vizact.elapsed(1), 0, viz.REL_PARENT ) vizact.whilekeydown( viz.KEY_DOWN, ball.translate, 0, vizact.elapsed(-1), 0, viz.REL_PARENT ) vizact.whilekeydown( viz.KEY_RIGHT, ball.translate, vizact.elapsed(1), 0, 0, viz.REL_PARENT ) vizact.whilekeydown( viz.KEY_LEFT, ball.translate, vizact.elapsed(-1), 0, 0, viz.REL_PARENT ) pos = ball.get(viz.POSITION) pos[1] +=.2 arrow.translate(pos) arrow.visible(viz.ON) viz.callback(viz.MOUSEDOWN_EVENT,mouseclick) viz.mouse(viz.OFF) |
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): Code:
vizact.whilekeydown( viz.KEY_UP, myTranslate, viz.KEY_UP ) Code:
def myTranslate(key): |
Thanks for your help. Still I'm having problem with giving value to translate. when I assign value to it, the ball suddenly disappears.
Please check the code. import viz viz.go() ground = viz.add('tut_ground.wrl') viz.clearcolor(0.5,0.5,1) soccerball1 = viz.add('soccerball.ive') soccerball2 = viz.add('soccerball.ive') soccerball3 = viz.add('soccerball.ive') soccerball1.translate(-1,1.5,3) soccerball2.translate(0,1.5,3) soccerball3.translate(1,1.5,3) arrow = viz.add('arrow.wrl') arrow.scale(.1,.1,.1) arrow.visible(viz.OFF) def mouseclick(button): global ball if button == viz.MOUSEBUTTON_LEFT: pickObj = viz.pick() if pickObj.valid(): ball = pickObj ground.disable(viz.PICKING) def myTranslate(key): global ball if not ball: return else: if key == viz.KEY_UP: ball.translate(0,1) elif key == viz.KEY_DOWN: ball.translate() elif key == viz.KEY_RIGHT: ball.translate() elif key == viz.KEY_KP_LEFT: ball.translate() 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_KP_RIGHT) vizact.whilekeydown(viz.KEY_LEFT,myTranslate, viz.KEY_KP_LEFT) viz.callback(viz.MOUSEDOWN_EVENT,mouseclick) viz.mouse(viz.OFF) |
I have solved the problem of assigning value to translate, the only problem left is that the right ball jumps when I try to move it but the other 2 works perfect.
import viz viz.go() ground = viz.add('tut_ground.wrl') viz.clearcolor(0.5,0.5,1) ground.disable(viz.PICKING) soccerball1 = viz.add('soccerball.ive') soccerball2 = viz.add('soccerball.ive') soccerball3 = viz.add('soccerball.ive') soccerball1.translate(-1,1.5,3) soccerball2.translate(0,1.5,3) soccerball3.translate(1,1.5,3) def mouseclick(button): global ball if button == viz.MOUSEBUTTON_LEFT: pickObj = viz.pick() if pickObj.valid(): ball = pickObj def myTranslate(key): global ball if not ball: return else: if key == viz.KEY_UP: ball.translate(0,0.01,0,ball) elif key == viz.KEY_DOWN: ball.translate(0,-0.01,0,ball) elif key == viz.KEY_RIGHT: ball.translate(0.01,0,0,ball) elif key == viz.KEY_LEFT: ball.translate(-0.01,0,0,ball) 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) viz.callback(viz.MOUSEDOWN_EVENT,mouseclick) viz.mouse(viz.OFF) |
That is odd. The 3 balls are dealt with in the exact same way so having only 1 of them not behaving as intended is strange. In what way the third ball jumping? The only thing I can see about your code is that you have accidently put a space in the last argument of viz.KEY_UP in the whilekeydown statement.
|
All times are GMT -7. The time now is 02:45 AM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC