碰撞不正常

时间:2015-08-17 18:37:15

标签: javascript phaser-framework

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

整个代码:

<!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 个答案:

答案 0 :(得分:4)

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

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