Jump to content

body is undefined


Faizy
 Share

Recommended Posts

Can someone tell me why "body" is undefined? Console tells me: "Uncaught TypeError: Cannot set property 'enable' of undefined"

 

function create() {
	game.stage.backgroundColor = '#3598db'

	this.cursor = this.input.keyboard.createCursorKeys();
	this.player = this.add.image(200, 75, 'tank')
	this.physics.arcade.enable(this.player);
	this.player.body.enable = true;
	this.player.body.collideWorldBounds = true;
	
    for (var i = 0; i < enemiesTotal; i++)
    {
        enemies.push(new EnemyTank());
    }

}

Can someone help me out?

Link to comment
Share on other sites

this.physics.arcade.enable doesn't exist, you should try game.physics.arcade.enable. Also game.add.image, not this.add.image.

this refers to the scope of the function create(), whereas game it's a variable that has the functions you want.

Probably should also read about JS scope to avoid future pain.

Link to comment
Share on other sites

Thanks scheffgames.

 

I changed it, but I still run into the exact same problem.

function create() {
	game.stage.backgroundColor = '#3598db'

	this.cursor = game.input.keyboard.createCursorKeys();
	this.player = game.add.image(200, 75, 'tank')
	game.physics.arcade.enable(this.player);
	this.player.body.enable = true;
	this.player.body.collideWorldBounds = true;
	
    for (var i = 0; i < enemiesTotal; i++)
    {
        enemies.push(new EnemyTank());
    }
}

 

Edit: I fixed it. I used images instead of sprites...images dont support physics. ;)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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