![]() |
#1
|
|||
|
|||
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) |
#2
|
|||
|
|||
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 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 ) 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 |
#3
|
|||
|
|||
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) |
#4
|
|||
|
|||
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) |
#5
|
|||
|
|||
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.
|
![]() |
Thread Tools | |
Display Modes | Rate This Thread |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Stop for... loop | Johannes | Vizard | 20 | 04-01-2014 01:53 AM |
How to stop vizact.move | Jerry | Vizard | 3 | 06-04-2009 04:25 PM |
delays with <avatar>.act and speech | vsully | Vizard | 15 | 08-23-2007 04:24 PM |
When a video stop ? | djdesmangles | Vizard | 2 | 01-11-2007 04:15 PM |
download problems & avatar action | vgracie | Vizard | 1 | 09-14-2006 11:29 AM |