Jump to content

phaser 3 disableBody problem


a.lead
 Share

Recommended Posts

Hi everyone,

 

I need some advise for my code. There is a problem with function disableBody - the mistake is "...not a function".

I prepared the objects from tiled map as below:

 foods = this.physics.add.group({
        allowGravity: true,
        immovable: true,
        moves: false
    });
    foods.addMultiple(
       map.createFromObjects('food-layer', 'food-place', {key:'tilesheet', frame: 6})
    );

as I found some information this method does not add 'bodies' to my objects, so can anyone advice a method to add the 'bodies'

 

thank you.

Link to comment
Share on other sites

  • 1 month later...

Hey @a.lead, I worked on a game where I took objects from Tiled and put them into sprite groups. Like you, I used the createFromObjects() function. I then looped through each object and enabled physics via the Scene object. After they were enabled, I added them to a sprite group.

The code structure may not be super clear without the full context, but I figure it would still help:

 

// This function makes a Tiled object a sprite with a physics body
// We need a scene object to enable physics
const createSpike = (spike, scene) => {
  spike.setOrigin(0.5, 0.5);
  scene.physics.world.enable(spike, Phaser.Physics.Arcade.DYNAMIC_BODY);
  spike.body.setSize(32, 30);
  spike.body.setImmovable(true);
  spike.body.setAllowGravity(false);
};

// The code below was called in my create() function of my game
let spikeObjects = tilemap.createFromObjects(
  TILED_SPIKES_LAYER, 
  TILED_SPIKES_Key,
  { key: SPIKE_KEY },
  this
);

// If the createFromObjects method fails, it returns null
// We just set it to an empty array to not deal with errors
 if (!spikeObjects) {
   spikeObjects = [];
}

spikeObjects.forEach((spike) => {
  createSpike(spike, this);
});

const spikes = this.physics.add.group(spikeObjects);

 

I get the body once I call scene.physics.world.enable() on my object. If your code is in one file, then instead of scene you can just reference this inside the create() or update() functions. 

 

Hope this helps!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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