Game Development Reference
In-Depth Information
useful to know if the button was just pressed. Pressing the button once for instance
can be used for selecting a menu option or firing a gun. It's a simple piece of code
to add. This code should be added to the ControllerButton class.
bool _wasHeld = false;
public bool Pressed { get; private set; }
public void Update()
{
// reset the pressed value
Pressed = false;
byte buttonState = Sdl.SDL_JoystickGetButton(_joystick, _buttonId);
Held = (buttonState == 1);
if (Held)
{
if(_wasHeld == false)
{
Pressed = true;
}
_wasHeld = true;
}
else
{
_wasHeld = false;
}
}
The Pressed value is only true for one frame when the button is pressed, the
Held value is true for as long as the button is pressed. The controller class and
the various controls it uses have resulted in quite a lot of code. This code is all
related and could be better organized by separating it into its own namespace,
Engine.Input . Reorganizing your code is an important part of building up a
reusable library. All the controller classes are involved with input so a separate
input sub-library can be created.
Right-click the engine project and choose New Folder on the context menu, as
shown in Figure 9.13.
Call the new folder Input and drag and drop all the control classes into the folder.
The input class should also be added to this folder so you end with something
Search WWH ::




Custom Search