Multiway Component
Used to bind keys to directions and have the entity move accordingly.
Multiway acts by listening to directional events, and then setting the velocity each frame based on the current direction and the current speed.
If a speed is not defined for a particular axis (x or y), then the velocity along that axis will not be set.
This behavior works in most cases, but can cause undesired behavior if you manipulate velocities by yourself while this component is in effect. If you need to resolve collisions, it's advised to correct the position directly rather than to manipulate the velocity. If you still need to reset the velocity once a collision happens, make sure to re-add the previous velocity once the collision is resolved.
Additionally, this component provides the entity with Motion
methods & events.
See Also
Methods
.multiway()
public this .multiway([Number speed,] Object keyBindings[, Object options])
- speed
A speed in pixels per second
- keyBindings
What keys should make the entity go in which direction. Direction is specified in degrees
- options
An object with options for
normalize
andmultipleDirectionBehavior
.
Constructor to initialize the speed and keyBindings. Component will listen to key events and move the entity appropriately. Can be called while a key is pressed to change direction & speed on the fly.
The options parameter controls the behavior of the component, and has the following defaults:
"normalize": false
. When set to true, the directional input always has a magnitude of 1"multipleDirectionBehavior": "all"
How to resolve multiple active directions. Set to "first" or "last" to allow only one active direction at a time.
Example
this.multiway(150, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180});
this.multiway({x:150,y:75}, {UP_ARROW: -90, DOWN_ARROW: 90, RIGHT_ARROW: 0, LEFT_ARROW: 180});
this.multiway({W: -90, S: 90, D: 0, A: 180});
See Also
.speed()
public this .speed(Object speed)
- speed
New speed the entity has, for x and y axis.
Change the speed that the entity moves with, in units of pixels per second. Can be called while a key is pressed to change speed on the fly.
If the passed object has only an x or y property, only the velocity along that axis will be controlled.
Example
this.speed({ x: 150, y: 50 });