Skip to content

Novout/pixi-controller

Repository files navigation

PIXI.JS Controller

License Badge Version Badge Bundle Badge

Set of controls for Pixi.js to facilitate general handling of external events.

Install

npm install pixi.js pixi-controller

or

yarn add pixi.js pixi-controller

Simple Example

import * as PIXI from 'pixi.js';
import { PIXIController, BUTTON, PLAYER } from 'pixi-controller';

const app = new PIXI.Application();
document.body.appendChild(app.view);

app.loader.add('example', 'example.jpg').load((loader, resources) => {
    const Controller = new PIXIController();
    const example = new PIXI.Sprite(resources.example.texture);

    app.stage.addChild(example);

    Controller.Mouse.prevent(BUTTON.RIGHT);

    app.ticker.add(() => {
      if(Controller.Mouse.isButtonDown(BUTTON.LEFT)) {
        console.log('left')
      }

      if(Controller.Mouse.isButtonDown(BUTTON.RIGHT)) {
        console.log('right')
      }
      
      if (Controller.Keyboard.isKeyDown(...PLAYER.LEFT))
        example.x -= 1;
      if (Controller.Keyboard.isKeyDown(...PLAYER.RIGHT))
        example.x += 1;
      if (Controller.Keyboard.isKeyDown(...PLAYER.UP))
        example.y -= 1;
      if (Controller.Keyboard.isKeyDown(...PLAYER.DOWN))
        example.y += 1;

      Controller.update();
    });
});

Credits

-nom- for creation pixi.js-mouse and pixi.js-keyboard