Input

To enable keyboard input, you must first initialize and assign it to variables. Mouse input works without assignment, but by changing parameters of individual sprites.

"use strict";
window.Input.state.menu = {
    create: function() {
		//creates sprite and enables mouse input and mouse drag
        this.box = mt.create("box_simple");
		this.box.inputEnabled = true;
		this.box.input.enableDrag();

        //creates variables, containing the keys
        this.cursors = this.game.input.keyboard.createCursorKeys();
        this.shift = this.game.input.keyboard.addKey(Phaser.Keyboard.SHIFT);
        this.ctrl = this.game.input.keyboard.addKey(Phaser.Keyboard.CONTROL);
    },

    update: function() {
        //carries out an action, depending on the pressed button, while the button is held down
        if (this.cursors.up.isDown) this.box.y -= 4;
        if (this.cursors.down.isDown) this.box.y += 4;
        if (this.cursors.right.isDown) this.box.x += 4;
        if (this.cursors.left.isDown) this.box.x -= 4;

        if (this.shift.isDown) this.box.angle += 2;
        if (this.ctrl.isDown) this.box.angle -= 2;
    }
};

Move the box around, by using the cursor keys or dragging with mouse. Rotate with CTRL and SHIFT.

Full sample available here.

Leave a Reply

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