Faizy

body is undefined

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?

Share this post


Link to post
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.

Share this post


Link to post
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. ;)

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.