Class: Object2D
object2d.Object2D
Represents an object in 2D space with a position and rotation.
The position should be in world space and represent the centre of the object, if the object is given a width and/or height.
Also contains an events system which can be used to execute arbitrary functions on certain events.
Hierarchy
Object2Dโณ
Cameraโณ
PhysicsObjectโณ
Shape
Constructors
constructor
โข new Object2D()
Defined in
src/object2d.ts:34
Properties
listeners
โข Protected listeners: Object = {}
Index signature
โช [index: string]: Listener[]
Defined in
src/object2d.ts:32
position
โข Private position: vec2
Defined in
src/object2d.ts:29
rotation
โข Private rotation: number = 0
Defined in
src/object2d.ts:30
setVec
โข Private setVec: vec2
Defined in
src/object2d.ts:49
Methods
addEventListener
โธ addEventListener(event, listener): boolean
Executes a function when an event is fired.
Parameters
| Name | Type | Description |
|---|---|---|
event | string | The event to listen for |
listener | Listener | The function to execute when the event fired |
Returns
boolean
Wether or not the listener was added
Defined in
src/object2d.ts:209
fireEvent
โธ fireEvent(event, ...e): void
Executes all listeners attached to a given event.
Parameters
| Name | Type | Description |
|---|---|---|
event | string | The event to fire |
...e | any | The data to pass to each event listener |
Returns
void
Defined in
src/object2d.ts:185
getEventListeners
โธ getEventListeners(event): Listener[]
Gets the listeners attached to an event.
Parameters
| Name | Type |
|---|---|
event | string |
Returns
Listener[]
An array of listeners attached to the given event
Defined in
src/object2d.ts:198
getEvents
โธ getEvents(): string[]
Gets the events that can be listened to on the object.
Returns
string[]
The events the object supports
Defined in
src/object2d.ts:175
getPosition
โธ getPosition(): vec2
Gets the object's position.
Returns
vec2
The object's position as a vec2
Defined in
src/object2d.ts:82
getRotation
โธ getRotation(): number
Gets the object's rotation as a number in radians relative to y+.
Returns
number
The object's rotation
Defined in
src/object2d.ts:139
moveRight
โธ moveRight(dist): void
Moves the object's position right (x+) relative to it's rotation.
Parameters
| Name | Type | Description |
|---|---|---|
dist | number | The distance to move right, can be + or - |
Returns
void
Defined in
src/object2d.ts:100
moveUp
โธ moveUp(dist): void
Moves the object's position up (y+) relative to it's rotation.
Parameters
| Name | Type | Description |
|---|---|---|
dist | number | The distance to move up, can be + or - |
Returns
void
Defined in
src/object2d.ts:114
removeEventListener
โธ removeEventListener(event, listener): boolean
Removes a listener from an event.
Parameters
| Name | Type | Description |
|---|---|---|
event | string | The event the listener is attached to |
listener | Listener | The listener to remove |
Returns
boolean
Wether or not the listener was removed
Defined in
src/object2d.ts:223
rotate
โธ rotate(angle): void
Increments the object's rotation by an angle in radians.
Parameters
| Name | Type | Description |
|---|---|---|
angle | number | The angle to rotate by in radians |
Returns
void
Defined in
src/object2d.ts:148
setPosition
โธ setPosition(pos): void
Sets the object's position.
Parameters
| Name | Type | Description |
|---|---|---|
pos | vec2 | The object's new position |
Returns
void
Defined in
src/object2d.ts:43
setPositionX
โธ setPositionX(x): void
Sets the x component of the object's position.
Parameters
| Name | Type | Description |
|---|---|---|
x | number | The object's new x coordinate |
Returns
void
Defined in
src/object2d.ts:56
setPositionY
โธ setPositionY(y): void
Sets the y component of the object's position.
Parameters
| Name | Type | Description |
|---|---|---|
y | number | The object's new y coordinate |
Returns
void
Defined in
src/object2d.ts:69
setRotation
โธ setRotation(angle): void
Sets the object's rotation.
Parameters
| Name | Type | Description |
|---|---|---|
angle | number | The object's new rotation |
Returns
void
Defined in
src/object2d.ts:128
setupEvents
โธ Protected setupEvents(): void
Sets all the events that can be used in listeners.
Listeners can be added for an event using:
this.listeners.eventName = [];
example Setup listeners for "position" and "rotate" events.
this.listeners.position = [];
this.listeners.rotate = [];
Returns
void
Defined in
src/object2d.ts:165
translate
โธ translate(v): void
Translates the object by the provided vector.
Parameters
| Name | Type | Description |
|---|---|---|
v | vec2 | The vector to translate by |
Returns
void
Defined in
src/object2d.ts:91