Input · class

TouchControls

A reusable mobile scheme: dynamic stick on the first zone; look plus a primary tap/hold and secondary downward swipe on the second. The class reports generic control state. A game decides whether those signals mean jump, fire, crouch, slide, or anything else.

Explained in Moving things.

class TouchControls
import { TouchControls } from '@driftengine/core';

Constructor

new

constructor(input: InputSource, options?: TouchControlsOptions)

Take the input surface, and optionally the two nodes the stick is drawn with.

ParameterTypeDescription
inputInputSource
options?TouchControlsOptions
More

Two forms, and the older one is kept working rather than deprecated. It was (input, stickBase, stickNub, options), which put an output channel where a reader expects a dependency — a consumer forbidden the page read the signature, concluded the class could not be used without two HTMLElements, and built two it threw away. The options form says what is true: the elements are how the stick is drawn, and drawing it is optional.

new TouchControls(input) is a working control scheme with no visible stick.

new

constructor(input: InputSource, stickBase: HTMLElement, stickNub: HTMLElement, options?: TouchControlsOptions)
ParameterTypeDescription
inputInputSource
stickBaseHTMLElement
stickNubHTMLElement
options?TouchControlsOptions

Properties

NameTypeDescription
moveXnumber
moveYnumber
primaryHeldboolean
secondaryHeldboolean
onFirstTouch(() => void) | null

Methods

tick

tick(nowMs: number): void

Resolve a still second-zone touch into the generic primary hold.

ParameterTypeDescription
nowMsnumber

consumePrimaryPress

consumePrimaryPress(): boolean

Consume the primary edge exactly once.

consumeLook

consumeLook(out: { dx: number; dy: number; }): void

Drain accumulated second-zone look motion into a caller-owned object.

ParameterTypeDescription
out{ dx: number; dy: number; }

dispose

dispose(): void