开发者问题收集

碰撞无法正常工作

2015-08-17
965

我正在尝试使用 Phaser.js 的一些示例来学习开发 HTML5 游戏,但是在处理碰撞对象时遇到了一些麻烦。 当“dude”精灵与平台“地面”碰撞时,它运行良好,但是当我添加管道图像时,“dude”精灵永远不会与它发生碰撞。有什么建议吗?

完整代码:

<!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>
1个回答

添加 game.physics.enable(平台,phaser.physics.arcade);

794963392

this phaser示例-Sprite-VS-group 可能会有所帮助。

Shohanur Rahaman
2015-08-18