javascript - 在 Phaser.js 中添加移动功能
问题描述
我使用 Phaser.js 制作了一个简单的 Bug Dodger 游戏,源代码可以在下面找到:
https://github.com/ankurg132/phaser-codecademy-projects/tree/master/Bug%20Dodger%20Game
你可以在这里看到一个现场演示:https ://codepen.io/ankurg132/pen/jOWgXer
function preload() {
this.load.image('bug1', 'https://s3.amazonaws.com/codecademy-content/courses/learn-phaser/physics/bug_1.png');
this.load.image('bug2', 'https://s3.amazonaws.com/codecademy-content/courses/learn-phaser/physics/bug_2.png');
this.load.image('bug3', 'https://s3.amazonaws.com/codecademy-content/courses/learn-phaser/physics/bug_3.png');
this.load.image('platform', 'https://s3.amazonaws.com/codecademy-content/courses/learn-phaser/physics/platform.png');
this.load.image('codey', 'https://s3.amazonaws.com/codecademy-content/courses/learn-phaser/physics/codey.png');
}
const gameState = {
score: 0
};
function create() {
gameState.player = this.physics.add.sprite(225, 450, 'codey').setScale(.5);
const platforms = this.physics.add.staticGroup();
platforms.create(225, 490, 'platform').setScale(1, .3).refreshBody();
gameState.scoreText = this.add.text(195, 485, 'Score: 0', { fontSize: '15px', fill: '#000000' });
gameState.player.setCollideWorldBounds(true);
this.physics.add.collider(gameState.player, platforms);
gameState.cursors = this.input.keyboard.createCursorKeys();
const bugs = this.physics.add.group();
function bugGen () {
const xCoord1 = Math.random() * 450;
const xCoord2 = Math.random() * 450;
const xCoord3 = Math.random() * 450;
bugs.create(xCoord1, 10, 'bug1');
bugs.create(xCoord2, 10, 'bug2');
bugs.create(xCoord3, 10, 'bug3');
}
const bugGenLoop = this.time.addEvent({
delay: 500,
callback: bugGen,
callbackScope: this,
loop: true,
});
this.physics.add.collider(bugs, platforms, function (bug) {
bug.destroy();
gameState.score += 10;
gameState.scoreText.setText(`Score: ${gameState.score}`);
})
this.physics.add.collider(gameState.player, bugs, () => {
bugGenLoop.destroy();
this.physics.pause();
this.add.text(180, 250, 'Game Over', { fontSize: '15px', fill: '#000000' });
this.add.text(152, 270, 'Click to Restart', { fontSize: '15px', fill: '#000000' });
// Add your code below:
this.input.on('pointerup', () =>{
gameState.score = 0;
this.scene.restart();
});
});
}
function update() {
if (gameState.cursors.left.isDown) {
gameState.player.setVelocityX(-160);
} else if (gameState.cursors.right.isDown) {
gameState.player.setVelocityX(160);
}else if (this.input.activePointer.isDown) {
if(this.input.activePointer.worldX < gameState.player.getCenter().x)
gameState.player.setVelocityX(-160);
else gameState.player.setVelocityX(160);
}else {
gameState.player.setVelocityX(0);
}
}
const config = {
type: Phaser.AUTO,
width: 450,
height: 500,
backgroundColor: "b9eaff",
physics: {
default: 'arcade',
arcade: {
gravity: { y: 50 },
enableBody: true,
}
},
scene: {
preload,
create,
update
}
};
const game = new Phaser.Game(config);
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Bug Dodger</title>
<style>
canvas {
width: 100%;
}
</style>
</head>
<body>
<script src="https://cdn.jsdelivr.net/npm/phaser@3.16.2/dist/phaser.min.js"></script>
<script defer src="game.js"></script>
</body>
</html>
现在我想在游戏中添加一个简单的移动功能,以便当我触摸玩家精灵左侧或右侧的任意位置时,玩家可以朝那个方向移动。目前我正在使用 cursors 属性,所以它只有在我按下箭头键时才有效,我该如何更改它以便它也可以在触摸时工作。请提供整个过程。提前致谢。
解决方案
您应该能够使用input.activePointer
. 然后它将与鼠标和/或触摸一起使用。
function update() {
if (gameState.cursors.left.isDown) {
gameState.player.setVelocityX(-160);
} else if (gameState.cursors.right.isDown) {
gameState.player.setVelocityX(160);
} else if (this.input.activePointer.isDown) {
if(this.input.activePointer.worldX < gameState.player.getCenter().x)
gameState.player.setVelocityX(-160);
else gameState.player.setVelocityX(160);
} else {
gameState.player.setVelocityX(0);
}
}
推荐阅读
- angular - 如何在角度构建后在 index.html 的脚本标签中注入延迟
- git - 在 Azure Devops 中为不同类型的 PR 自动设置分支策略
- html - 在将图像从 jsp 文件发送到 jquery ajax 时,它向我显示 [object FormData]。它仅用于图像价值。如何解决?
- android - 关于使用导航组件时在导航图中放置片段的问题?
- jmeter - 如何为 ssh 协议计算 jmeter 中文件的处理时间(源到目标文件处理)
- android - 单击“启动应用程序”时,我创建的新目录和新资源文件未显示在应用程序上
- python - 如何在使用 Python 时摆脱 Vscode 终端中显示的路径?
- office365 - Power Automate - 提取数字组合
- c# - 将变量传递给类c#
- visual-studio-code - 如何解决非复合名称'Validator'的use语句没有效果