(JavaScript Cheat Sheet)
A minimalistic framework for tile-based 2D games in JavaScript.
Sample game: http://euske.github.io/jscs/index.html
run()
Basic plumbing such as event dispatch and main loop. In most cases, you don't need to change this.
-
new App(framerate, frame, images, audios, labels)
Asset management and overall state handling (game over, etc.)-
keyup(e)
Handles JavaScript events. -
keydown(e)
Handles JavaScript events. -
focus(e)
Handles JavaScript events. -
blur(e)
Handles JavaScript events. -
mouseup(e)
Handles JavaScript events. -
mousedown(e)
Handles JavaScript events. -
mousemove(e)
Handles JavaScript events. -
renderString(font, text, scale, x, y)
Graphically render text on the canvas in the given font. -
addElement(bounds)
Creates a new HTML div element and put it on a screen. -
removeElement(elem)
Removes an HTML element from the screen. -
post(e)
Post an event. -
init()
App initialization. [App specific code goes here.] -
update()
Called for every frame. [App specific code goes here.] -
repaint()
Repaint the entire screen. [App specific code goes here.]
-
-
new Scene(app)
Responsible for event handling for a particular scene of game. (Title, Game Over, Main Game, etc.)-
init()
Scene initialization. [A game specific code goes here.] -
update()
Called for every frame. [A game specific code goes here.] -
render(ctx, x, y)
Called when the scene needs to be painted. [A game specific code goes here.] -
move(vx, vy)
Receives the controller input. -
action(action)
Receives the action button input.
-
-
new GameScene(app)
[extends Scene]
Holds all entities that are shown in the game world.addObject(obj)
removeObject(task)
Adds/Removes a Game object to the scene.
-
new Task(body)
A generic process object that is called for every frame.init()
start()
update()
-
new Queue(tasks) [extends Task]
A list of Task objects that are executed sequentially.add(task)
remove(task)
-
new Sprite(bounds) [extends Task]
A visible object that might not interact with other characters.render(ctx, x, y)
-
new Actor(bounds, hitbox, tileno) [extends Particle]
An moving object that interacts with other Actors.-
collide(actor)
Called when the Actor object collides with another Actor. -
move(dx, dy)
-
-
log(x)
Prints a string to the console. -
clamp(v0, v, v1)
Keeps a number v within the range [v0, v1]. -
blink(t, d)
Returns true if t is within the on interval. -
rnd(a[, b])
Generates a random number in the range [0, a) or [a, b). -
format(v, n, c)
Formats a number to a fixed number of digits. -
copyArray(a)
Deep-copies an array. -
removeArray(a, f)
Removes element(s) from the array a that matches f. -
removeChildren(n, name)
Removes all DOM children that have the given tag from n. -
createCanvas(width, height)
Creates a canvas element with the given size. -
getEdgeyContext(canvas)
Returns a pixellated canvas 2D context. -
image2array(img)
Converts an image to 2D array. -
playSound(sound)
Plays an audio element. -
new Slot(object)
An event publisher.subscribe(recv)
Registers a function as an event receiver.unsubscribe(recv)
Unregisters a function as an event receiver.signal(arg)
Calls a registerd function of all subscriers.
-
new Vec2(x, y)
A 2D vector object.equals(v)
copy()
add(v)
sub(v)
modify(v)
rotate90(v)
move(dx, dy)
-
new Vec3(x, y, z)
A 3D vector object.equals(v)
copy()
add(v)
sub(v)
modify(v)
move(dx, dy, dz)
-
new Rectangle(x, y, width, height)
A rectangle object.equals(rect)
right()
bottom()
centerx()
centery()
center()
copy()
move(dx, dy)
inflate(dw, dh)
contains(v)
overlap(rect)
union(rect)
intersection(rect)
clamp(rect)
rndpt()
collide(v, rect)
Clips the motion vector v so that this rect doesn't intersect with the given rect.
-
new Box(origin, size)
A box object.equals(box)
center()
copy()
move(dx, dy, dz)
movev(v)
moveTo(v)
inflate(dx, dy, dz)
contains(v)
overlap(box)
union(box)
intersection(box)
clamp(box)
rndpt()
collide(v, box)