Jump to content

Body Phaser class doesn't work in other classes OOP


Salvatore
 Share

Recommended Posts

Hi!

 

I'm following the Tutorial: How to organize your Javascript code into classes using Phaser HTML5 game framework - Toastedware but, the body class appears "undefined" in other classes files.

 

Using the Firefox Javascript console, this message pop in the Level.js:

'ground.body is undefined'

How fix this? Everything works fine, but this cracks the whole execution.

 

P.S.: My Level.js file:

Level = function(game){    this.game = game;}Level.prototype = {    preload: function()    {        this.game.load.image('background', 'assets/game01/sky.png');        this.game.load.image('ground', 'assets/game01/platform.png');    },    create: function()    {        this.game.add.sprite(0, 0, 'background');        this.platforms = game.add.group();        var ground = this.platforms.create(0, game.world.height - 64, 'ground');        ground.scale.setTo(2, 2);        ground.body.immovable = true; // The trouble line    }    }
Link to comment
Share on other sites

  • 1 month later...

I'm confused. Did this issue get fixed in the sample download code?

 

Everything works fine for me in FF, but these two lines don't appear anywhere in the code I downloaded:

 

game.physics.startSystem(Phaser.Physics.ARCADE);
game.physics.arcade.enable(ground);

 

However, it does contain this line:

ground.body.immovable = true;

 

​Is it not necessary to have those first two lines anymore?

Link to comment
Share on other sites

game.physics.startSystem(Phaser.Physics.ARCADE);

This line is usually not needed - Phaser starts with Arcade enabled by default. You do however need to enable physics on the sprites you wish to use, either by enabling it directly on the sprites or setting the group they're added to up accordingly:

var group = game.add.group();group.enableBody = true;group.physicsBodyType = Phaser.Physics.ARCADE;// ... all sprites added to this group will now automatically have their body property enabled
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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