Jump to content

Collision bugged when game is lagging


MusicMan
 Share

Recommended Posts

It seems that phaser's collision, specifically the body.blocked variable doesn't work consistently when my game is running at around 30 fps or lower. When I run against a wall, my player.body.blocked.left or right variable either doesn't get set to true, or changes rapidly from true to false. It doesn't affect me when playing on my desktop, but my slower laptop has major problems.

Here's an example game I made to show this:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no" />
<script type="text/javascript" src="phaser.js"></script>
<style>
    body {
        margin: 0;
    }
</style>
</head>
<body>
<script>

var playScene = new Phaser.Class({
    Extends: Phaser.Scene,
    initialize: function playScene() {
        Phaser.Scene.call(this, { key: 'playScene' });
    },

    preload: function() {
        this.load.image('sky', 'assets/sky.png');
        this.load.image('ground', 'assets/platform.png');
        this.load.image('star', 'assets/star.png');
    },

    create: function() {
        this.add.image(400, 300, 'sky');
        platforms = this.physics.add.staticGroup();

        platforms.create(160, 180, 'ground');
        platforms.create(550, 140, 'ground').setScale(2).refreshBody();

        this.physics.world.bounds.width = config.width;
        this.physics.world.bounds.height = config.height;

        player = this.physics.add.sprite(20, 20, 'star');

        player.setCollideWorldBounds(true);
        this.physics.add.collider(player, platforms);

        cursors = this.input.keyboard.createCursorKeys();

        text = this.add.text(16, 16, 'score: 0', { fontSize: '16px', fill: '#000' });
    },

    update: function(time, delta) {
        if (!(cursors.left.isDown || cursors.right.isDown) || (cursors.left.isDown && cursors.right.isDown)) {
            player.body.velocity.x = 0;
        }
        else if (cursors.left.isDown) {
            player.body.velocity.x = -80;
        }
        else if (cursors.right.isDown) {
            player.body.velocity.x = 80;
        }

        text.setText('blocked: ' + player.body.onWall());
    }
});

var config = {
    type: Phaser.AUTO,
    width: 320,
    height: 180,
    pixelArt: true,
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 500 },
        }
    },
    scene: [ playScene ]
};

var game = new Phaser.Game(config);

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

I also attached the game files.

It works fine normally, but when you open the game on about 5 separate windows to make it lag, the collision gets buggy.

Is there any fix for this?

CollisionBugExample.7z

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...