PDA

View Full Version : A door question... left button mouse


Sakiot
08-24-2011, 03:45 PM
Hello people, I am new in this program and the teacher gave us some homework... itīs kinda embarrasing because it may be something noob but I tried what my logic gave me in other programs... Here we go

The problem is that I have a door and I need to open it with the left button of the mouse; and then when the door is open use again the left button of the mouse to close it...

I already get how to open the door but I have problems with the closing...

This is my code for the action of opening and closing the door and also the code for the left button of the mouse (Sorry, its in spanish XD)


def clickIzquierdo(): #This def is to assign the action of the Left Button
objeto = viz.pick()
if objeto == puerta:
abrePuerta()

vizact.onmousedown(viz.MOUSEBUTTON_LEFT,clickIzqui erdo)

def abrePuerta(): #This def is to open the door
puerta.runAction(vizact.spinTo(euler = [-90,0,0], speed = 10.0))
sonido_abre.play()

vizact.onkeydown("z",abrePuerta)

def cierraPuerta(): #This def is to close the door
puerta.runAction(vizact.spinTo(euler = [0,0,0], speed = 10.0))
sonido_cierra.play()

vizact.onkeydown("x",cierraPuerta)



By the words of my teacher is that I have to assign the def "cierraPuerta" in the def "clickIzquierdo"... but I dont know how to establish when the door is open and when is closed...The door always open but never closes it

sleiN13
08-25-2011, 12:23 AM
you could use a global variable to save the status of the door. Or you could assign this variable directly to the door during construction.

puerta.abre = False (assuming abre is open ;))

then in the mouse even function check if the door is open or not and execute the appropriate function to open or close it.

Sakiot
08-25-2011, 03:32 PM
Thank you very much, actually the use of a global variable cross my mind but... it didnīt work... this is my code with the global variable "estadoPuerta" (door status)

estadoPuerta = 0 #Global Variable

def clickIzquierdo(): #Left button action
objeto = viz.pick()
if objeto == puerta:
if estadoPuerta == 0:
abrePuerta()
else:
if estadoPuerta == 1:
cierraPuerta()

vizact.onmousedown(viz.MOUSEBUTTON_LEFT,clickIzqui erdo)

def abrePuerta(): #Open door
puerta.runAction(vizact.spinTo(euler = [-90,0,0], speed = 10.0)) # Euler(Y,X,Z)
estadoPuerta = 1
sonido_abre.play() #Asignar sonido al picarle al boton de la puerta

vizact.onkeydown("x",abrePuerta)

def cierraPuerta(): #Close door
puerta.runAction(vizact.spinTo(euler = [0,0,0], speed = 10.0)) # Euler(Y,X,Z)
estadoPuerta = 0
sonido_cierra.play()

vizact.onkeydown("z",cierraPuerta)

I am sure that my problem is the "clickIzquierdo" section and the use of IF and ELSE... Itīs my first time with the program and I still don't know so much (I have worked with C# and C++ and the logic is the same but... the way of writing it is different and thats my problem... I think).

Thank you for your attention =D

sleiN13
08-26-2011, 12:45 AM
your code seems correct. language vizard uses is python you can work with that like c#

define classes will make things clearer most of the time.

class something(object):

also you could use if a == 'a' and b == 'b':
and elif instead of

else:
if:

I replaced your door opening code with a print

print "print this string"

and I got the correct results so maybe check if your tabs are placed correctly for all the if statements.