VirtualAnalogStick

The VirtualAnalogStick is for systems which do not have a normal analog stick. The virtual analog stick is displayed on the screen and can be controlled by touch or mouse controls.

How to use:

  • Create a new VirtualAnalogStick.
  • Call mouse handlers (mouseDown(), mouseUp(), mouseMove()), when they are called in your Game-class.
  • Use the angle and strength variables of the VirtualAnalogStick to determine in which direction the user holds it.
  • Override the render()-function to make a beautiful virtual analog stick.
  • If your VirtualAnalogStick has another shape than a simple circle, override the checkMouseCollision()-function. You might also want to override isMouseWithinMoveArea() and isMouseWithinDeadArea().
  • If the interaction should be canceled, for example if your game or a level in your game restarts, or if the virtual dpad loses focus, because a window has been opened, or in any similar case, call reset().

Caution: - Take care that nothing weird happens when the user presses on both a real analog stick, or dpad, and the virtual analog stick at the same time. Because this can happen on systems that have both. You can use user_is_interacting to handle this.

Look into the VirtualAnalogStickDemo to see how the VirtualAnalogStick can be used.

Constructor

new (x:Int, y:Int, size:Int, dead_distance:Int, full_distance:Int, move_distance:Int)

Constructor

Parameters:

x

: x position on screen in pixels

y

: y position on screen in pixels

size

: Size in pixels (used for both width and height)

dead_area:

Within this area in the center no direction is pressed

move_area:

Outside of this area no direction is pressed

Variables

angle:Float

Direction that the user is pressing:

size:Int

strength:Float

user_is_interacting:Bool

To check if the user is currently using the virtual analog stick at all: Indicates that the user is interacting with the virtual analog stick, because he clicked/touched it. The interaction is continued until the user releases the mouse button or stops touching. It is continued even while the mouse/touch position is outside of the virtual analog stick.

x:Int

y:Int

Methods

checkMouseCollision (mouse_x:Int, mouse_y:Int):Bool

Checks if the mouse or touch is on the virtual analog stick. Here, this is just a simple circle collision test. If your stick has another shape, override this function.

distanceToCenter (mouse_x:Int, mouse_y:Int):Float

isMouseWithinDeadDistance (mouse_x:Int, mouse_y:Int):Bool

isMouseWithinFullDistance (mouse_x:Int, mouse_y:Int):Bool

isMouseWithinMoveDistance (mouse_x:Int, mouse_y:Int):Bool

mouseDown (mouse_x:Int, mouse_y:Int):Void

Mouse handlers Call these when the mouse handlers in your Game-class are called

mouseMove (mouse_x:Int, mouse_y:Int):Void

Mouse handlers Call these when the mouse handlers in your Game-class are called

mouseUp (mouse_x:Int, mouse_y:Int):Void

render (painter:Graphics):Void

Please override this with your own function.

Parameters:

painter

: g2 Graphics object

reset ():Void

If the interaction should be canceled, for example if your game or a level in your game restarts, or if the stick loses focus, because a window has been opened, or in any similar case, call reset().