How game objects and behaviors work in KAPLAY
masterKAPLAY uses a component-based architecture. You build game objects by passing an array of components to add(). Behaviors are handled through an imperative syntax using event listeners and update loops:
- Component-based properties: Components like
area()provide methods like.onCollide(). Components likehealth()provide properties like.hp. - Lifecycle and Input: Use
onUpdate()for frame-by-frame logic,onUpdate(tag, callback)to run logic for all objects with a specific tag, andonKeyDown(key, callback)for input handling. - Object Management: Use
destroy(obj)to remove an object from the scene.
// .onCollide() comes from "area" component
player.onCollide("enemy", () => {
// .hp comes from "health" component
player.hp--;
});
// check fall death
player.onUpdate(() => {
if (player.pos.y >= height()) {
destroy(player);
}
});
// All objects with tag "enemy" will move to the left
onUpdate("enemy", (enemy) => {
enemy.move(-400, 0);
});
// move up 100 pixels per second every frame when "w" key is held down
onKeyDown("w", () => {
player.move(0, 100);
});