phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
36.94k stars 7.08k forks source link

Changing states with box2d enabled causes crash #1884

Closed tom2strobl closed 9 years ago

tom2strobl commented 9 years ago

See http://www.html5gamedevs.com/topic/13753-changing-states-with-box2d-causes-crash/

When calling game.physics.startSystem(Phaser.Physics.BOX2D); and changing the state afterwards it crashes. Stack Tracing paths to this.world.GetGravity().Clone(); in World.js – it seems GetGravity() is not returning something and therefore crashing the following Clone() with .Clone is not a function.

Any ideas/hacks/workarounds?

photonstorm commented 9 years ago

Which version of the plugin is this testing with? because this bug was fixed some months ago now and a new zip file was sent to all customers at the time.

tom2strobl commented 9 years ago

Bought it yesterday (to the email weare@wild.as) and downloaded the link from the Paddle email I don't know how else to get to the codebase to be honest – where can I find it? package.json says it's 1.0.1

photonstorm commented 9 years ago

That is the current one, and state swapping definitely works in it. Here is a test case to try it with (the assets can be found in the examples repo if you need them)

var MyGame = {};

MyGame.StateA = function (game) {

    this.sprite;
    this.cursors;

};

MyGame.StateA.prototype = {

    preload: function () {

        this.load.image('atari', 'assets/sprites/atari130xe.png');
        this.load.image('mushroom', 'assets/sprites/mushroom.png');

    },

    create: function () {

        this.physics.startSystem(Phaser.Physics.BOX2D);

        this.sprite = this.add.sprite(200, 200, 'atari');
        this.physics.box2d.enable(this.sprite);

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

        this.input.onDown.add(this.changeState, this);

    },

    update: function () {

        this.sprite.body.setZeroVelocity();

        if (this.cursors.left.isDown)
        {
            this.sprite.body.moveLeft(400);
        }
        else if (this.cursors.right.isDown)
        {
            this.sprite.body.moveRight(400);
        }

        if (this.cursors.up.isDown)
        {
            this.sprite.body.moveUp(400);
        }
        else if (this.cursors.down.isDown)
        {
            this.sprite.body.moveDown(400);
        }

    },

    changeState: function () {

        this.state.start('StateB');

    }

};

MyGame.StateB = function (game) {

    this.sprite;
    this.cursors;

};

MyGame.StateB.prototype = {

    create: function () {

        this.sprite = this.add.sprite(200, 200, 'mushroom');
        this.physics.box2d.enable(this.sprite);
        this.sprite.body.fixedRotation = true;

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

    },

    update: function () {

        this.sprite.body.setZeroVelocity();

        if (this.cursors.left.isDown)
        {
            this.sprite.body.moveLeft(400);
        }
        else if (this.cursors.right.isDown)
        {
            this.sprite.body.moveRight(400);
        }

        if (this.cursors.up.isDown)
        {
            this.sprite.body.moveUp(400);
        }
        else if (this.cursors.down.isDown)
        {
            this.sprite.body.moveDown(400);
        }

    }

};

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example');

game.state.add('StateA', MyGame.StateA);
game.state.add('StateB', MyGame.StateB);

game.state.start('StateA');
tom2strobl commented 9 years ago

Turns out I copied the box2d-plugin-full.min.js from the examples/js folder which was the old version (compiled in march), the box2d-plugin-full.min.js from the regular dist folder is the newer version (compiled in may) - swapping them fixed it. Thank you so much and keep it up!

photonstorm commented 9 years ago

Ahh thanks for the heads-up! I will make sure that is replaced with the next release.

tom2strobl commented 9 years ago

Sorry for posting on a closed issue, but I want to take the opportunity to just say thank you for the endless work on this Framework, on the first glance I threw in a Vector library just to see that every Vector method you'd need is already in there, then I threw in a Random generation library, well, just to see that everything you'd need is already in there. Then before throwing in an Array-Utility library I looked into the docs – well it's already f***ing in there!

If there's another opportunity to throw money in your direction I won't hesitate.

clark-stevenson commented 9 years ago

I also want to repeat that.

Everytime I go to use something, it is magically there, and has always been there.... I just had not found it yet. It has been 18 months where I have been with Phaser in some capacity and I continue to find things which are just awesome!

:+1:

photonstorm commented 9 years ago

Thanks guys :)

Now I'm back at work properly I'll be wrapping up Phaser 2.4 and then (finally) releasing the Advanced Particles Plugin - which I'm sure everyone will love :)