Help us understand the problem. What is going on with this article?

pixi.jsのイベントを無理やり代入して動かす

More than 1 year has passed since last update.

この記事は @HibikineCode 1人 Advent Calendar 2018 の3日目の記事です。

ゲームエンジン pixi.js にはドキュメント化されていない機能があります。私が知っているうちの1つ、「on コールバック関数を DisplayObject に代入すると動く」について調べてみました。pixiをしばきたい人はどうぞ。

pixi.js のイベント

pixi.jsのイベントは、ドキュメント通りにはonメソッドで登録します。

const sprite = new Sprite();
sprite.interactive = true;
sprite.on('pointerdown', () => console.log('Clicked!');

ところが、これでも登録できます。

const sprite = new Sprite();
sprite.interactive = true;
sprite.pointerdown = () => console.log('Clicked!');

この場合は当然1つしか登録できませんので、基本的には前者の方法を使うことになります。

この方法は、React Pixi Fiberといったライブラリを使う時に効果を発揮します。

const HogeSprite = () => (
  <Sprite
    texture={texture}
    interactive
    pointerdown={() => console.log('Clicked!')}
  />
);

感想

実は v4 以前の機能としてあって、互換で残ってるのかも。

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away