Component Class
A Component is any object in your game world. It can be a rectangle, text, or image,
depending on the type you specify. Components have properties such as x
, y
,
width
, height
, color
, and motion variables like speedX
and speedY
. They can also interact with other components through collision detection.
Creating a component is straightforward. For example, a red box can be created using the
"rect"
type. If you want text, use "text"
. For images, you can pass in
a source path. Once created, add the component to the display so that it will be drawn every frame.
You can then change its properties inside the update()
function.
Syntax
const obj = new Component(width, height, color, x, y, type);
Example
const player = new Component(50, 50, "red", 100, 100, "rect");
display.add(player);
function update() {
player.x += 1;
}
Notes
- Valid types include
"rect"
,"text"
, and"image"
. - Components can have physics properties like gravity.
- Use
crashWith()
to detect collisions.