Text

To add text to your game, select the ‘Text’ tool form the tool-bar on the left (looks like a big “T”).

Then add some text on your map (formatting tools are above). You cannot change formatting mid-sentence, instead you must create a new text object. For example:

After that, in the source editor:

"use strict";
window.Text.state.menu = {
    create: function() {
        //initialize both text objects
        this.text = mt.create("Text");
        this.count = mt.create("count");
    },

    update: function() {
        //calls addCount function on mouse button down
        this.game.input.onDown.add(this.addCount, this);

    },

    //gets integer from text object, adds 1 and sets it as the new value
    addCount: function() {
        this.count.setText(parseInt(this.count._text) + 1);
    }

};

TheĀ parseInt function converts the textual object’s inner text, which is a string, in to a numeric integer value. This function is a must if you need to change (add, subtract etc.) the value.

Sample project available here.

Leave a Reply

Your email address will not be published. Required fields are marked *