Phaserで利用する小ネタ
Phaser画面のサイズ取得方法
1 2 3 4 |
gameW = this.sys.game.config.width; console.log(gameW); gameH = this.sys.game.config.height; console.log(gameH); |
Phaserでのキー操作
1 2 3 4 5 6 7 8 9 10 11 12 13 |
var cursors = this.input.keyboard.createCursorKeys(); if(cursors.right.isDown) { this.robotImage.x += speed; } if(cursors.left.isDown) { this.robotImage.x -= speed; } if(cursors.up.isDown) { this.robotImage.y -= speed; } if(cursors.down.isDown) { this.robotImage.y += speed; } |
キーボード操作
1 2 3 4 5 6 7 8 9 10 11 12 |
function update(){ cursors = this.input.keyboard.createCursorKeys(); if(cursors.right.isDown) { // 右カーソルキーが押下 } else if(cursors.left.isDown) { // 左カーソルキーが押下 } else if(cursors.up.isDown) { // 上カーソルキーが押下 } else if(cursors.down.isDown) { // 下カーソルキーが押下 } } |
input plugin
keyboard plugin
createCursorKeys
key
マウスカーソルのポイント
マウスカーソルのクリックされた位置に画像を移動して表示される
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
scene.create = function() { // 画像をとりあえず表示しておく this.player = this.add.sprite(400, 300, 'player'); }; scene.update = function() { // マウスカーソルの位置でクリックされた if(this.input.activePointer.isDown) { // カーソルのX座標にPlayerのX座標を移動 this.player.x = this.input.activePointer.x; // カーソルのY座標にPlayerのY座標を移動 this.player.y = this.input.activePointer.y; } }; |
画像がマウスカーソルの追従をする
マウスカーソルの位置に画像を表示する。カーソルの位置に画像が追従する
1 2 3 4 5 6 7 |
scene.update = function() { // マウスカーソルの位置を追従する // カーソルのX座標にPlayerのX座標を移動 this.player.x = this.input.activePointer.x; // カーソルのY座標にPlayerのY座標を移動 this.player.y = this.input.activePointer.y; }; |