Graphics Reference
In-Depth Information
Figure 16-19 Sensors and actuators connected to the volume.py controller
Unlike the example in Chapter 14, the 3D button on the Sound actuator panel should not be selected.
The code of volume.py is as follows. To keep the volume consistent over repeated calls of the script, you
need to store the value in a GameLogic attribute. The way to do this, as you saw previously in this chapter, is
to assign a value GL.vol in the context of an initialization loop using hasattr() . Possible volume values
range from 0 to 1, so set GL.vol to mid-volume, at 0.5 .
from bge import logic as GL
if not hasattr(GL, "init"):
GL.vol = 0.5
GL.init = None
As always, the controller, owner, actuators, and sensors must be retrieved, as you see here:
cont = GL.getCurrentController()
own = cont.owner
play = cont.actuators['playsound']
volumeup = cont.sensors['volumeup']
volumedown = cont.sensors['volumedown']
Just as in the previous cases of working with sensors in Python, the positive property is called to assess
whether the sensor is being triggered. The code here simply sets up two if conditionals: one for the case of
increasing the volume and one for the case of decreasing the volume:
if volumeup.positive:
if GL.vol < 1.0:
GL.vol = GL.vol + 0.01
if volumedown.positive:
if GL.vol > 0.01:
GL.vol = GL.vol - 0.01
 
Search WWH ::




Custom Search