Vizard 7 » Tutorials & Examples » Example scripts » Events » Event callbacks

Event callbacks


This script demonstrates the use of the vizact event functions.

This script demonstrates the use of the vizact event functions.
'c' changes the ball color.
Arrow keys to move ball.
Spacebar toggles the background color changing event on and off.
'r' removes the background color changing event entirely.
import viz
import vizact


import vizinfo

import vizshape
ball = vizshape.addSphere(radius=0.1,pos=(0,1.5,3))

#Pressing 'c' chooses a new ball color at random
vizact.onkeydown('c', ball.color, vizact.choice( [viz.RED,viz.BLUE,viz.GREEN], vizact.RANDOM ) )

#You can use vizact.whilekeydown() to quickly code up user controlled object movement
vizact.whilekeydown( viz.KEY_UP, ball.setPosition, 0, vizact.elapsed(1), 0, viz.REL_PARENT )
vizact.whilekeydown( viz.KEY_DOWN, ball.setPosition, 0, vizact.elapsed(-1), 0, viz.REL_PARENT )
vizact.whilekeydown( viz.KEY_RIGHT, ball.setPosition, vizact.elapsed(1), 0, 0, viz.REL_PARENT )
vizact.whilekeydown( viz.KEY_LEFT, ball.setPosition, vizact.elapsed(-1), 0, 0, viz.REL_PARENT )

#Create a timer that fires every 1 to 5 seconds chosen randomly
#When the timer fires, change the background color to the color of the looping choice parameter
#Returns an EventFunction object
bgColorEvent = vizact.ontimer( vizact.randfloat( .5, 3 ), viz.clearcolor, vizact.choice( [viz.RED,viz.WHITE,viz.BLUE] ) )

#Turns the firing of the above timer on and off by calling setEnabled() on the bgColorEvent EventFunction object
#When spacebar is pressed, call setEnabled() on bgColorEvent with toggling between True/False as arguments
vizact.onkeydown(' ', bgColorEvent.setEnabled, vizact.choice( [False,True] ) )

#Permanently stops the color changing background event
vizact.onkeydown('r', vizact.removeEvent, bgColorEvent )