Last active
January 10, 2025 22:03
-
-
Save naosim/7c6ef1af55a38d703c837b533f012f4a to your computer and use it in GitHub Desktop.
アクションゲームを作る初期状態
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* プレイヤー。矢印で操作できる | |
*/ | |
class Player { | |
gameObject; | |
cursors; | |
create(scene) { | |
const player = (this.gameObject = scene.physics.add.existing( | |
scene.add.rectangle(100, 300, 16, 28, 0xffff00) | |
)); | |
player.body.setCollideWorldBounds(true); | |
// Input Events | |
if (!scene.input.keyboard) { | |
throw new Error("scene.input.keyboard is undefined"); | |
} | |
this.cursors = scene.input.keyboard.createCursorKeys(); | |
} | |
update(/** @type {Phaser.Scene}*/ scene) { | |
if (this.cursors.left.isDown) { | |
this.gameObject.body.setVelocityX(-160); | |
} else if (this.cursors.right.isDown) { | |
this.gameObject.body.setVelocityX(160); | |
} else { | |
this.gameObject.body.setVelocityX(0); | |
} | |
if ( | |
this.cursors.up.isDown && | |
(this.gameObject.body.touching?.down || this.gameObject.body.blocked.down) | |
) { | |
this.gameObject.body.setVelocityY(-330); | |
} | |
// scene.cameras.main.centerOn(this.gameObject .x, this.gameObject .y); | |
} | |
} | |
var config = { | |
type: Phaser.AUTO, | |
width: 400, | |
height: 400, | |
physics: { | |
default: "arcade", | |
arcade: { | |
gravity: { x: 0, y: 300 }, | |
debug: false | |
} | |
}, | |
scene: { | |
preload: preload, | |
create: create, | |
update: update | |
} | |
}; | |
const player = new Player(); | |
var game = new Phaser.Game(config); | |
function preload() {} | |
function create() { | |
player.create(this); | |
} | |
function update() { | |
player.update(this); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment