gpt4 book ai didi

javascript - 如何让我的 Phaser 游戏射出不止一支箭?

转载 作者:行者123 更新时间:2023-11-30 10:59:52 27 4
gpt4 key购买 nike

我正在使用 Phaser 框架在 JavaScript 中制作射箭游戏。到目前为止,我已经了解了箭头的去向和击中目标的位置,但是如果您希望箭头再次射出,则必须重新加载页面。如何让它可以多次射击?

<!DOCTYPE html> 
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>TSA Video Game</title>
<script src="//cdn.jsdelivr.net/npm/phaser@3.11.0/dist/phaser.js"></script>
<style type="text/css">
body {
margin: 0;
}
</style>
</head>
<body>

<script type="text/javascript">

var config = {
type: Phaser.AUTO,
width: 800,
height: 600,
physics: {
default: 'arcade'
},
scene: {
preload: preload,
create: create,
update: update,
render: render
}
};

//Start the game
var game = new Phaser.Game(config);

function preload ()
{
this.load.image('sky', 'assets/sky.png');
this.load.image('archer', 'assets/archer.png');
this.load.image('target', 'assets/target.png');
this.load.image('ground', 'assets/ground.png');
this.load.image('rings', 'assets/rings.png');
this.load.image('arrow', 'assets/arrow.png');

}

function create ()
{
//Load all the images
this.add.image(400, 300, 'sky');
this.add.image(200, 200, 'ground');
this.add.image(530, 365, 'target');
this.add.image(300, 100, 'rings');

//Create the archer/player
this.player = this.physics.add.sprite(100, 410, 'archer');
//Create the arrow to shoot
this.arrow = this.physics.add.sprite(100, 430, 'arrow');

//Get keypresses
this.cursors = this.input.keyboard.createCursorKeys();
//Assign input for spacebar
this.spacebar = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);

}

function update ()
{
//Declare constants for movement
const moveAmt = 1500;
const moveYamt = 0;
this.player.setDrag(2000);

//Move the player left or right
if (this.cursors.right.isDown)
this.player.setVelocityX(moveAmt);
if (this.cursors.left.isDown)
this.player.setVelocityX(-moveAmt);

//Rotation of the player
if (this.cursors.up.isDown && this.player.angle > -45) {
this.player.angle -= 1;}
if (this.cursors.down.isDown && this.player.angle < 0) {
this.player.angle += 1;}

//Shooting with the spacebar
if (Phaser.Input.Keyboard.JustDown(this.spacebar)) {
this.arrow.setVelocityX(moveAmt);
this.arrow.setVelocityY(moveYamt);
}

//Stop the arrow once it hits the bullseye
if (this.arrow.x > 480) {
this.arrow.x = 480;
this.arrow.setVelocityX(0);
this.arrow.setVelocityY(0);
}

}

function render() {
}

</script>
</body>
</html>

我试过创建三个箭头,并在“if (Phaser.Keyboard.Justdown”语句中嵌套另一个 if 语句,但这没有用。

最佳答案

您需要能够创建箭头数组。

这是一个快速、上手的示例:

在你的create()函数中

this.arrows = [];//创建你的数组

之后:

if (Phaser.Input.Keyboard.JustDown(this.spacebar)) {
let arrow = this.physics.add.sprite(100, 430, 'arrow'); // Create a new arrow
arrow.setVelocityX(moveAmt); // get it moving
arrow.setVelocityY(moveYamt);
this.arrows.push(arrow); // save it in the array
}

是的,您需要更多代码,但这应该有所帮助。

然后你需要循环遍历运动中的箭头并更新它们中的每一个

关于javascript - 如何让我的 Phaser 游戏射出不止一支箭?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58276265/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com