【发布时间】:2021-12-07 21:17:49
【问题描述】:
在我的代码中,如果分数大于或等于10,我想调用函数floor2,然后当玩家触摸门时应该调用该函数。
目前,当玩家与门重叠时,它什么也不做。我尝试过移动 if 语句,但似乎并没有做太多。我认为是上面提到的 if 语句或函数 floor2 导致了错误,但它是一个逻辑错误,所以不能确定它来自哪里
这是我的代码:
<html>
<head>
<meta charset="UTF-8" />
<title>Making your first Phaser 3 Game - Part 7</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',
arcade: {
gravity: { y: 300 },
debug: false
}
},
scene: {
preload: preload,
create: create,
update: update
}
};
var player;
var platforms;
var cursors;
var score = 0;
var scoreText;
var end;
var game = new Phaser.Game(config);
function preload ()
{
this.load.image('sky', 'assets/l1.png');
this.load.image('ground', 'assets/stone.png');
this.load.image('star', 'assets/wine.png');
this.load.image('bomb', 'assets/skull.png');
this.load.image('door', 'assets/door1.png');
this.load.spritesheet('dude', 'assets/test.png', { frameWidth: 39, frameHeight: 56 });
this.load.spritesheet('dead', 'assets/test2.png', { frameWidth: 39, frameHeight: 38.5 });
this.load.audio("bgm",'assets/constantine.mp3');
this.load.audio("bottle",'assets/bottle.mp3');
}
function create (score)
{
bgm = this.sound.add("bgm", { loop: true});
bgm.volume = 1.5
bgm.play();
bottle = this.sound.add("bottle",{loop:false});
bottle.volume = 0.05;
this.add.image(400, 300, 'sky');
door = this.add.image(50,550,'door');
platforms = this.physics.add.staticGroup();
platforms.create(400, 568, 'ground').setScale(2).refreshBody();
platforms.create(600, 400, 'ground');
platforms.create(50, 250, 'ground');
platforms.create(750, 220, 'ground');
player = this.physics.add.sprite(100, 450, 'dude');
player.setBounce(0.2);
player.setCollideWorldBounds(true);
this.anims.create({
key: 'left',
frames: this.anims.generateFrameNumbers('dude', { start: 1, end: 3 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'turn',
frames: this.anims.generateFrameNumbers('dude', { start: 0, end: 0 }),
frameRate: 20
});
this.anims.create({
key: 'right',
frames: this.anims.generateFrameNumbers('dude', { start: 4, end: 6 }),
frameRate: 10,
repeat: -1
});
this.anims.create({
key: 'dead',
frames: this.anims.generateFrameNumbers('dead', { start: 0, end: 1 }),
frameRate: 10,
repeat: -1
});
cursors = this.input.keyboard.createCursorKeys();
this.physics.add.collider(player, platforms);
stars = this.physics.add.group({
key: 'star',
repeat: 11,
setXY: { x: 12, y: 0, stepX: 70 }
});
stars.children.iterate(function (child) {
child.setBounceY(Phaser.Math.FloatBetween(0.4, 0.8));
});
this.physics.add.collider(stars, platforms);
this.physics.add.overlap(player, stars, collectStar, null, this)
scoreText = this.add.text(16, 16, 'score: 0', { fontSize: '32px', fill: '#000' });
bombs = this.physics.add.group();
this.physics.add.collider(bombs, platforms);
this.physics.add.collider(player, bombs, hitBomb, null, this);
if (score >= 10)
{
this.physics.add.overlap(player,door,floor2(score));
}
}
function update ()
{
if (cursors.left.isDown)
{
player.setVelocityX(-160);
player.anims.play('left', true);
}
else if (cursors.right.isDown)
{
player.setVelocityX(160);
player.anims.play('right', true);
}
else
{
player.setVelocityX(0);
player.anims.play('turn');
}
if (cursors.up.isDown && player.body.touching.down)
{
player.setVelocityY(-330);
}
}
function collectStar (player, star)
{
star.disableBody(true, true);
score += 10;
bottle.play();
scoreText.setText('Score: ' + score);
if (stars.countActive(true) === 0)
{
stars.children.iterate(function (child) {
child.enableBody(true, child.x, 0, true, true);
});
var x = (player.x < 400) ? Phaser.Math.Between(400, 800) : Phaser.Math.Between(0, 400);
var bomb = bombs.create(x, 16, 'bomb');
bomb.setBounce(1);
bomb.setCollideWorldBounds(true);
bomb.setVelocity(Phaser.Math.Between(-200, 200), 20);
}
}
function hitBomb (player, bomb)
{
this.physics.pause();
player.setTint(0xff0000);
player.anims.play('dead');
end = this.add.text(300, 300, 'game over', { fontSize: '64px', fill: '#808080' });
gameOver = true;
}
function floor2(score)
{
this.physics.pause()
win = this.add.text(300, 300, 'you win', { fontSize: '64px', fill: '#808080' });
gameOver = true;
}
</script>
</body>
</html>
这是游戏的样子
【问题讨论】:
-
不可能只看你的代码就知道。我建议使用
console.log(score)。还要在floor2中添加一个console.log以查看它是否到达那里。 -
无法测试,所以您可能只是忘记在 2 楼为
this.physics.pause();添加分号;
标签: javascript phaser-framework