GameJs API

  • gamejs

    This module holds the essential Rect and Surface classes as well as static methods for preloading assets. gamejs.ready() is maybe the most important as it kickstarts your app.

  • gamejs/display

    Methods to create, access and manipulate the display Surface.

  • gamejs/draw

    Utilities for drawing geometrical objects to Surfaces. If you want to put images on the screen see gamejs.image.

    Colors

    There are several ways to specify colors. Whenever the docs says "valid #RGB string" you can pass in any of those formats:

    "#ff00ff"
    "rgb(255, 0, 255)"
    "rgba(255,0, 255, 1)"
  • gamejs/event

    Methods for polling mouse, keyboard and ntwork;

    Call get() in your main loop to get a list of events that happend since you last called.

    A pattern for using this might look like so: your main game function (tick in this example) is being called by gamejs.time.fpsCallback() 25 times per second. Inside tick we call gamejs.event.get() for a list of events that happened since the last tick and we loop over each event and act on the event properties.

    var events = gamejs.event.get()
    events.forEach(function(event) {
       if (event.type === gamejs.event.MOUSE_UP) {
         gamejs.log(event.pos, event.button);
       } else if (event.type === gamejs.event.KEY_UP) {
         gamejs.log(event.key);
       }
    });
  • gamejs/font

    Methods for creating Font objects which can render text to a Surface.

    Example:
    // create a font
    var font = new Font('20px monospace');
    // render text - this returns a surface with the text written on it.
    var helloSurface = font.render('Hello World')
  • gamejs/http

    Make synchronous http requests to your game's serverside component.

    If you have a server module in your app that exports a stick web application then that app is started by GameJs. You can send & load objects from the server-side with the gamejs.http.load(url) and gamejs.http.save(url, object) functions.

    You will need stick! install it with ringo-admin:

    ringo-admin install ringo/stick

    Example

    Server-side in server.js:

    var {Application} = require('stick');
    var {jsonResponse} = require('stick/helpers');
    
    // create & export a stick application
    var app = exports.app = Application();
    app.configure('params', 'notfound', 'error', 'route');
    
    // route url /foobar to this function
    // see stick docu for more info on how to route.
    app.get('/foobar', function() {
        return jsonResponse({"hello": "world"});
    });

    Client-side:

     var response = gamejs.http.load("/foobar");
     gamejs.log(response);
     // outputs: {"hello": "world"}
    more on how to write web applications with stick:
    • http://ringojs.org/api/stick/stick/
    • http://github.com/hns/stick
  • gamejs/image

    Load images as Surfaces. All images must be preloaded:

    gamejs.preload(["images/ship.png", "images/sunflower.png"]);

    and can then be loaded as Surfaces with gamejs.image.load.

  • gamejs/mask

    Image masks. Usefull for pixel perfect collision detection.

  • gamejs/mixer

    Playing sounds with the html5 audio tag. Audio files must be preloaded with the usual gamejs.preload() function. Only ogg files supported.

  • gamejs/pathfinding/astar

    AStar Path finding algorithm

    Use the findRoute(map, from, to, [timeout]) function to get the linked list leading from a point to another on the given map.

    The map interface is seen in Map.

    The resulting point list includes the two points from and to and in between all points leading from to to from (sic! optimization).

    Example result:
    ({
        point: [
            3,
            3
        ],
        from: {
            point: [
                2,
                2
            ],
            from: {
                point: [
                    1,
                    1
                ],
                from: {
                    point: [
                        0,
                        0
                    ],
                    from: null,
                    length: 0
                },
                length: 291,
                score: 573
            },
            length: 524,
            score: 665
        },
        length: 729,
        score: 729
    })

    Optionally, the search is canceld after timeout in millseconds.

    If there is no route null is returned.

    Points are given as an Array [x, y].

  • gamejs/scene

    Provides higher level classes for creating simple prototype games in director mode. Your games will typically be similarly to gamejs/scene.Scene: have a bunch of SpriteGroups, update them, draw them, do collision detection amongst them and react to events from the event loop.

    Scene is a default, quick-start implementation for getting something on the screen fast - you can and probably will replace the generic Scene with something custom later on.

  • gamejs/sprite

    Provides Sprite the basic building block for any game and SpriteGroups, which are an efficient way for doing collision detection between groups as well as drawing layered groups of objects on the screen.

  • gamejs/time

    Provides tools for game time managment.

    This is very different from how PyGame works. We can not pause the execution of the script in Browser JavaScript, so what we do you do is write a main function which contains the code you would put into your main loop and pass that to fpsCallback():

  • gamejs/transform

    Rotate and scale Surfaces.

  • gamejs/utils/arrays

    Utility functions for working with Objects

  • gamejs/utils/binaryheap

  • gamejs/utils/matrix

    Matrix manipulation, used by GameJs itself. You probably do not need this unless you manipulate a Context's transformation matrix yourself.

  • gamejs/utils/objects

    Utility functions for working with Objects

  • gamejs/utils/vectors

.. show more