Jump to content

Same code different working


d0zzerr
 Share

Recommended Posts

/*
 *  https://labs.phaser.io/edit.html?src=src\physics\matterjs\advanced%20shape%20creation.js
 */


const config = {
    type: Phaser.AUTO,
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 600,
    height: 800,
    scene: {
        preload: preload,
        create: create
    },
    physics: {
        default: "matter",
        matter: {
            // debug: true
        }
    }
};


var game = new Phaser.Game(config);

function preload() {
    // Load sprite sheet generated with TexturePacker
    this.load.atlas('sheet', 'assets/physics/fruit-sprites.png', 'assets/physics/fruit-sprites.json');

    // Load body shapes from JSON file generated using PhysicsEditor
    this.load.json('shapes', 'assets/physics/fruit-shapes.json');
}

function create() {
    var shapes = this.cache.json.get('shapes');

    this.matter.world.setBounds(0, 0, game.config.width, game.config.height);
    this.add.image(0, 0, 'sheet', 'background').setOrigin(0, 0);

    // sprites are positioned at their center of mass
    var ground = this.matter.add.sprite(0, 0, 'sheet', 'ground', {shape: shapes.ground});
    ground.setPosition(0 + ground.centerOfMass.x, 0 + ground.centerOfMass.y);  // position (0,0)

    this.matter.add.sprite(200, 20, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(250, 20, 'sheet', 'banana', {shape: shapes.banana});
    this.matter.add.sprite(360, 20, 'sheet', 'orange', {shape: shapes.orange});
    this.matter.add.sprite(400, 20, 'sheet', 'cherries', {shape: shapes.cherries});

     this.matter.add.sprite(200, 200, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(250, 200, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(300, 200, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(350, 200, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(400, 200, 'sheet', 'crate', {shape: shapes.crate});

    this.input.on('pointerdown', function (pointer) {
        this.matter.add.sprite(pointer.x, pointer.y, 'sheet', 'banana', {shape: shapes.banana});
    }, this);
   
}

 

 

/*
 *  This example show how to load complex shapes created with PhysicsEditor (https://www.codeandweb.com/physicseditor)
 */


const config = {
    type: Phaser.AUTO,
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 600,
    height: 800,
    scene: {
        preload: preload,
        create: create
    },
    physics: {
        default: "matter",
        matter: {
            // debug: true
        }
    }
};


var game = new Phaser.Game(config);

function preload() {
    // Load sprite sheet generated with TexturePacker
    this.load.atlas('sheet', 'assets/physics/fruit-sprites.png', 'assets/physics/fruit-sprites.json');

    // Load body shapes from JSON file generated using PhysicsEditor
    this.load.json('shapes', 'assets/physics/fruit-shapes.json');
}

function create() {
    var shapes = this.cache.json.get('shapes');

    this.matter.world.setBounds(0, 0, game.config.width, game.config.height);
    this.add.image(0, 0, 'sheet', 'background').setOrigin(0, 0);

    // sprites are positioned at their center of mass
    var ground = this.matter.add.sprite(0, 0, 'sheet', 'ground', {shape: shapes.ground});
    ground.setPosition(0 + ground.centerOfMass.x, 0 + ground.centerOfMass.y);  // position (0,0)

    // changed order add sprite 
    this.matter.add.sprite(200, 200, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(250, 200, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(300, 200, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(350, 200, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(400, 200, 'sheet', 'crate', {shape: shapes.crate});
    
    
    this.matter.add.sprite(200, 20, 'sheet', 'crate', {shape: shapes.crate});
    this.matter.add.sprite(250, 20, 'sheet', 'banana', {shape: shapes.banana});
    this.matter.add.sprite(360, 20, 'sheet', 'orange', {shape: shapes.orange});
    this.matter.add.sprite(400, 20, 'sheet', 'cherries', {shape: shapes.cherries});

   

    this.input.on('pointerdown', function (pointer) {
        this.matter.add.sprite(pointer.x, pointer.y, 'sheet', 'banana', {shape: shapes.banana});
    }, this);
   
}

 

the codes are the same but the results are different. When I run the first code block, the objects are continuously moving, but when I run the second code it works correctly. The objects stop after a while. Could it be a bug or something else I did not notice

you can try these codes on https://labs.phaser.io/edit.html?src=src\physics\matterjs\advanced shape creation.js

 

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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