gpt4 book ai didi

javascript - 碰撞不能正常工作

转载 作者:行者123 更新时间:2023-11-28 15:19:41 24 4
gpt4 key购买 nike

我正在尝试通过使用 Phaser.js 的一些示例来学习使用 Phaser.js 开发 HTML5 游戏,但我在运行时遇到了一些碰撞对象的问题。当“花花公子” Sprite 与平台“地面”碰撞时,它效果很好,但是当我添加管道图像时,“花花公子” Sprite 永远不会与它发生碰撞。有什么建议么?

整个代码:

<!DOCTYPE html>
<html>
<head>
<title>scene2</title>
</head>
<body>


<script type="text/javascript" src="./js/phaser.js"></script>
<script type="text/javascript">
var game = new Phaser.Game(800, 600, Phaser.AUTO, '', { preload: preload, create: create, update: update });

function preload(){
game.load.image('sky', './imgs/sky.png');
game.load.image('ground', './imgs/platform2.png');
game.load.image('pipe', './imgs/pipe.png');
game.load.spritesheet('dude', './imgs/dude.png', 32, 48);
}

var
sky,
player,
platforms,
pipe;

function create(){

game.physics.startSystem(Phaser.Physics.ARCADE);
sky = game.add.image(0, 0, 'sky');
sky.scale.setTo(2, 2);

platforms = game.add.group();
platforms.enableBody = true;
var ground = platforms.create(0, game.world.height - 64, 'ground');
ground.scale.setTo(500, 2);
ground.body.immovable = true;

pipe = game.add.sprite(32, -150, 'pipe');
game.physics.arcade.enable(pipe);

player = game.add.sprite(32, game.world.height - 150, 'dude');
game.physics.arcade.enable(player);
player.body.bounce.y = 0.2;
player.body.gravity.y = 300;
player.body.collideWorldBounds = true;

player.animations.add('left', [0, 1, 2, 3], 10, true);
player.animations.add('right', [5, 6, 7, 8], 10, true);

}

function update(){
game.physics.arcade.collide(player, platforms);
game.physics.arcade.collide(player, pipe);

var cursors = game.input.keyboard.createCursorKeys();
player.body.velocity.x = 0;

if (cursors.left.isDown)
{
// Move to the left
player.body.velocity.x = -150;

player.animations.play('left');
}
else if (cursors.right.isDown)
{
// Move to the right
player.body.velocity.x = 150;

player.animations.play('right');
}
else
{
// Stand still
player.animations.stop();

player.frame = 4;
}

// Allow the player to jump if they are touching the ground.
if (cursors.up.isDown && player.body.touching.down)
{
player.body.velocity.y = -350;
}
}
</script>
</body>
</html>

最佳答案

之前添加 game.physicals.enable(platforms, Phaser.Physics.ARCADE);
platforms = game.add.group();

这个phaser example - sprite-vs-group可能会有帮助。

关于javascript - 碰撞不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32057394/

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