npm install phaser-virtual-joystick
import { VirtualJoystick } from 'phaser-virtual-joystick';
import * as Phaser from 'phaser';
import { VirtualJoystick } from 'phaser-virtual-joystick';
import { Bullet } from './Bullet';
import { Player } from './Player';
export class GameScene extends Phaser.Scene {
private joystick!: VirtualJoystick;
private player!: Player;
private playerSpeed: number = 250;
private bulletsGroup!: Phaser.Physics.Arcade.Group;
private currentFacingAngle: number = 0;
private isJoystickActive: boolean = false;
private guideImage!: Phaser.GameObjects.Image;
private guideText!: Phaser.GameObjects.Text;
constructor() {
super({ key: 'GameScene' });
}
preload() {
this.load.spritesheet('player', 'assets/player.png', {
frameWidth: 64, frameHeight: 64
});
this.load.image('bullet', 'assets/bullet.png');
this.load.image('fireButton', 'assets/shoot.png');
this.load.image('joy_icon', 'assets/joy_icon.png');
this.load.font('myfont', 'assets/fonts/JPRegular.ttf', 'opentype');
}
create() {
this.cameras.main.setBackgroundColor('rgba(166,246,134,0.9)');
this.cameras.main.setBounds(0, 0, 1280, 704);
this.physics.world.setBounds(0, 0, 1280, 704);
this.player = new Player(this, 490, 300);
this.player.setCollideWorldBounds(true);
this.player.anims.play('walk-right', true);
this.bulletsGroup = this.physics.add.group({
classType: Bullet,
maxSize: 30,
runChildUpdate: true
});
this.input.setTopOnly(false);
this.guideImage = this.add.image(110, this.scale.height - 150, 'joy_icon');
this.guideText = this.add.text(330, this.scale.height - 150,
`画面をタッチして
playerを移動`, { fontSize: '30px', color: '#ff0000' }).setOrigin(0.5);
this.tweens.add({
targets: [this.guideImage, this.guideText],
alpha: 0.3,
duration: 800,
yoyo: true,
repeat: -1
});
const baseSize = Math.min(this.scale.width, this.scale.height);
const baseRadius = baseSize * 0.18; // 約 18% (例: 80px)
const stickRadius = baseSize * 0.08; // 約 8% (例: 35px)
const deadZoneRadius = baseSize * 0.03; // 約 3% (例: 11px)
this.joystick = new VirtualJoystick({
scene: this,
deadZone: {
radius: deadZoneRadius,
fillColor: 0x1a1a1a,
strokeColor: 0xcccccc,
alpha: 0.2
},
baseArea: {
radius: baseRadius,
fillColor: 0x1a1a1a,
strokeColor: 0xcccccc,
alpha: 0.1
},
stick: {
radius: stickRadius,
fillColor: 0x666666,
strokeColor: 0x666666,
alpha: 0.5,
},
enableWithoutTouch: true,
bounds: {
topLeft: { x: 0, y: 10 },
bottomRight: {
x: this.scale.width * 0.8,
y: this.scale.height,
},
},
stickIcon: this.add.text(-100,30, '?', {
fontFamily: 'myfont', // ロード時に指定したキーを使用
fontSize: 60,
color: '#ffffff',
metrics: {
ascent: 52,
descent: 15,
fontSize: 60 // フォントサイズの実寸ピクセル
},
padding: { top: 10, bottom: 10, left: 10, right: 10 },
}).setOrigin(0.5, 0.5),
});
this.joystick.setDepth(2);
this.add.existing(this.joystick);
this.joystick.on('move', (data) => {
this.isJoystickActive = true;
if (this.isJoystickActive) {
this.player.handleMovement(data.x, data.y);
}
});
this.joystick.on('release', () => {
this.stopPlayer();
});
this.joystick.on('press', () => {
this.tweens.add({
targets: [this.guideImage, this.guideText],
alpha: 0,
duration: 500,
onComplete: () => {
this.guideImage.destroy();
this.guideText.destroy();
}
});
});
const buttonX = this.scale.width - 110;
const buttonY = this.scale.height - 100;
const fireButton = this.add.image(buttonX, buttonY, 'fireButton')
.setInteractive()
.setScrollFactor(0)
.setDepth(100)
.setScale(0.48)
.setAlpha(0.5);
fireButton.on('pointerdown', () => {
this.fireBullet();
});
}
private stopPlayer() {
this.player.setVelocity(0, 0);
this.player.anims.stop();
}
private fireBullet() {
const bullet = this.bulletsGroup.get(this.player.x, this.player.y + 15).setScale(0.15) as Bullet;
if (bullet) {
bullet.setActive(true);
bullet.setVisible(true);
bullet.fire(this.player.x, this.player.y, this.player.currentFacingAngle);
}
}
update(time: number, delta: number) {
if (!this.isJoystickActive) {
this.player.setFrame(9);
}
this.joystick?.update();
}
}
///////////////////
import * as Phaser from 'phaser';
export class Bullet extends Phaser.Physics.Arcade.Sprite {
private speed: number = 700;
constructor(scene: Phaser.Scene, x: number, y: number) {
super(scene, x, y, 'bullet');
scene.add.existing(this);
scene.physics.add.existing(this).setDepth(2);
scene.time.delayedCall(2000, this.destroyBullet, [], this);
}
private destroyBullet(): void {
this.destroy();
this.setVisible(false);
}
public fire(x: number, y: number, angleRad: number) {
this.enableBody(true, x, y, true, true);
const speed = 700;
const velocityX = Math.cos(angleRad) * speed;
const velocityY = Math.sin(angleRad) * speed;
this.setVelocity(velocityX, velocityY);
this.setRotation(angleRad);
}
update() {
if (this.y < 0 || this.y > 1676 || this.x < 0 || this.x > 2890) {
this.disableBody(true, true);
}
}
}
//////////////////////
import * as Phaser from 'phaser';
import { Bullet } from './Bullet';
import {VirtualJoystick} from "phaser-virtual-joystick";
export class Player extends Phaser.Physics.Arcade.Sprite {
private playerSpeed: number = 250;
private joystick!: VirtualJoystick;
currentFacingAngle: number = 0;
constructor(scene: Phaser.Scene, x: number, y: number){
super(scene, x, y, 'player');
scene.add.existing(this);
scene.physics.add.existing(this);
this.setBounce(0.2).setScale(1.3).setDepth(3);
this.setCollideWorldBounds(true); // 画面外に出ないように制限
this.setOrigin(0.5, 0.5);
this.createPlayerAnimations();
}
private createPlayerAnimations() {
const directions = [
{ key: 'walk-down', start: 0, end: 2 },
{ key: 'walk-left', start: 6, end: 8 },
{ key: 'walk-up', start: 3, end: 5 },
{ key: 'walk-right', start: 9, end: 11 },
{ key: 'walk-downright', start: 21, end: 23 },
{ key: 'walk-downleft', start: 18, end: 20 },
{ key: 'walk-upright', start: 15, end: 17 },
{ key: 'walk-upleft', start: 12, end: 14 },
{ key: 'idle', start: 9, end: 9 },
];
directions.forEach(dir => {
this.anims.create({
key: dir.key,
frames: this.anims.generateFrameNumbers('player', { start: dir.start, end: dir.end }),
frameRate: 10,
repeat: -1
});
});
}
public handleMovement(x: number, y: number){
const distance = Math.sqrt(x * x + y * y);
if (distance < 0.15) {
this.setVelocity(0, 0);
this.anims.stop();
return;
}
this.playerSpeed = 0;
if (distance >= 0.4) {
this.playerSpeed = distance * 400;
} else if (distance >= 0.6) {
this.playerSpeed = distance * 1200;
} else {
this.playerSpeed = distance * 150;
}
this.setVelocity(x * this.playerSpeed, y * this.playerSpeed);
const angle = Math.atan2(y, x);
let degrees = Phaser.Math.RadToDeg(angle);
let rounded = Math.round(degrees / 45) * 45;
rounded = ((rounded % 360) + 360) % 360;
if (rounded % 90 !== 0) {
switch (rounded) {
case 45: rounded -= 12; break; // 50度にする
case 135: rounded += 12; break; // 130度にする
case 225: rounded -= 12; break; // 230度にする
case 315: rounded += 12; break; // 310度にする
}
}
this.currentFacingAngle = Phaser.Math.DegToRad(rounded);
// 扱いやすいように 0 〜 360 度の範囲に正規化
if (degrees < 0) {
degrees += 360;
}
if (degrees >= 337.5 || degrees < 22.5) {
this.anims.play('walk-right', true);
this.setDepth(4);
} else if (degrees >= 22.5 && degrees < 67.5) {
this.anims.play('walk-downright', true);
this.setDepth(4);
} else if (degrees >= 67.5 && degrees < 112.5) {
this.anims.play('walk-down', true);
this.setDepth(1);
} else if (degrees >= 112.5 && degrees < 157.5) {
this.anims.play('walk-downleft', true);
this.setDepth(4);
} else if (degrees >= 157.5 && degrees < 202.5) {
this.anims.play('walk-left', true);
this.setDepth(4);
} else if (degrees >= 202.5 && degrees < 247.5) {
this.anims.play('walk-upleft', true);
this.setDepth(4);
} else if (degrees >= 247.5 && degrees < 292.5) {
this.anims.play('walk-up', true);
this.setDepth(4);
} else if (degrees >= 292.5 && degrees < 337.5) {
this.anims.play('walk-upright', true);
this.setDepth(4);
}
}
}