Graphics Reference
In-Depth Information
you want to use for forward-walking speed and rotation speed when the character turns. The default values for
walk and turn are 0 , because the character should not be moving when the script is first executed.
speed = .07
bspeed = -.07
rspeed = 0.02
walk = 0
turn = 0
As you saw, the Motion and Action actuators in the logic bricks did not have any of their fields filled in.
These values can be provided in the script itself, and this is the point where that is done for the Action actuator.
Review the discussion of the Action actuator in Chapter 14 if you need a reminder of what the individual fields
mean, and print the output of print (dir(action)) to find out what other methods are available for ac-
cessing an Action actuator.
The Action actuator should use the Walk action created in Chapter 14. The start frame should be 1 , and the
end frame of the action loop should be 20 . Finally, the Loop Stop play mode should be selected, which is done
using the mode property. The play modes are represented by integers: 0 represents the Play play mode, 1 rep-
resents the Property play mode, 2 represents the Flipper play mode, 3 represents the Loop Stop play mode, and
4 represents the Loop End play mode.
The code for setting these various Action actuator parameters is as follows:
action.action = 'Walk'
action.frameStart = 1
action.frameEnd = 20
action.mode = 3
Theactuallogicpartofthecodeissimple.Onceagain,usethe dir() commandtoprintoutalistofmethods
appropriate for sensors. The positive property is used to return whether the sensor is currently being ac-
tivated. For example, in this code, forward.positive will return a True value if the up arrow is being
pressed. The variables defined previously are used here to assign the appropriate values to walk and turn ,
according to the input from the sensors.
if forward.positive:
walk = speed
if back.positive:
walk = bspeed
if left.positive:
turn = rspeed
if right.positive:
turn = -rspeed
Another if conditional is needed to set the Action activator as active when the character is moving and in-
active ifthe character isnotmoving. Anytime anactuator'sparameters are changed, the activate() method
must be called from the controller in order for the new parameters to be realized in the game. When the deac-
tivate() method is called, the actuator is rendered inactive. So in order to have the character walk when the
movement sensors are positive, the following conditional is used:
if forward.positive or back.positive or left.positive or right.positive:
cont.activate(action)
else:
cont.deactivate(action)
Search WWH ::




Custom Search