Today we’re going to look at a method of creating a control scheme using only one events, rather than six.

The key event is the Step event.  The Step event fires every tick, once.  When you go to create it by clicking Add Event -> Step you’ll want to ensure you select Step and not Begin Step or End Step.

A:  Create a new project, or open your work project.
ER:  The project opens into the Workspace.

A:  Create a new Sprite by clicking Sprites in the Resources toolbox and choosing Create Sprite.
ER:  The Sprite window appears.

A:  Click Edit Image on the Sprite window.
ER:  The Edit Image screen appears.

A:  Click the Paint Bucket in the Toolbox, then click anywhere on the image to fill it.
ER:  The translucent square becomes a solid white.

A:  Close the Edit Image screen by clicking the X in the tab.
ER:  The Edit Image screen closes.

A:  In the Resources toolbox right click Objects and choose Create Object.
ER:  The object editor appears.

A:  Click the ellipsis (…) next to No Sprite to bring up the Sprite Selection window.
ER:  The Sprite Selection window should appear.

A:  Select the solid white box in  the Sprite Selection Window by double clicking it.
ER:  The sprite selection box will close, leaving the Object Editor window as the primary focus of the Workspace.

A:  Click Add Event, cursor down to Step, then select Step from the resulting menu.

ER:  The Code Window will appear.

A:  Enter the following code in the window.  (Explanations as to what’s occurring are below.)

ER:  The code window contains the above and there are no red warning symbols in the left margin.

So what’s going on in the above code?  First we’re setting friction for the object; here we maintain the 0.2 value we used for the previous movement method.  The friction setting determines how fast the object slows down once input driving it has disappeared.

Second, we’re creating a new variable objectSpeed.  This determines how many pixels the object moves in the appropriate direction.  Honestly, this belongs in the Create Event but I wanted to demonstrate a single script solution for movement.

Third, we’re executing checks and storing the result in a variable.  We’re checking whether the A, D, S, and/or W keys are being pressed.  These boolean indicators are then used to determine whether or not we should modify the x and y position of the object with the objectSpeed.

The general format of the if command is if (condition) { true code; } else { false code; }.  Condition can be any statement which results in a True or False variable.  We could combine the *_PRESSED indicator assignments directly into the if statement if we had to simplify this code further, but the current structure makes it explicit what’s occurring.