Jump to content

Uncaught ReferenceError: function is not defined


zakuhtet
 Share

Recommended Posts

I know it's a stupid question but please bear with me for a second.

I was using the code from the asteroid example on the Phaser site. Everything was fine except for the screenWrap function.

Even though I clearly defined it, Chrome console kept saying "Uncaught ReferenceError: screenWrap is not defined."

What's going on right here?

Here's the code below:

var mainState = {
	preload: function() {
		game.load.image("player","assets/player.png");
	},
	
	create: function() {
		game.physics.startSystem(Phaser.Physics.ARCADE);
		
		this.player = game.add.sprite(250, 250, "player");
		game.physics.enable(this.player, Phaser.Physics.ARCADE);
		this.player.body.drag.set(100);
		this.player.body.maxVelocity.set(200);
		this.player.anchor.x = 0.5;
		this.player.anchor.y = 0.5;
		this.player.scale.x = 0.1;
		this.player.scale.y = 0.1;
		
		this.cursors = game.input.keyboard.createCursorKeys();
	},

	update: function() {
		if(this.cursors.up.isDown)
		{
			game.physics.arcade.accelerationFromRotation(this.player.rotation, 200, this.player.body.acceleration);
		}
		else
		{
			this.player.body.acceleration.set(0);
		}
	
		if(this.cursors.left.isDown)
		{
			this.player.body.angularVelocity = -300;
		}
		else if(this.cursors.right.isDown)
		{
			this.player.body.angularVelocity = 300;
		}
		else
		{
			this.player.body.angularVelocity = 0;
		}
		
		screenWrap(this.player);
	},
	
	screenWrap: function(sprite) {
		if(sprite.x < 0)
		{
			sprite.x = 512;
		}
		else if(sprite.x > 512)
		{
			sprite.x = 0;
		}
		
		if(sprite.y < 0)
		{
			sprite.y = 512;
		}
		else if(sprite.y > 512)
		{
			sprite.y = 0;
		}
	},
};

var game = new Phaser.Game(512, 512);
game.state.add('main', mainState);
game.state.start('main');

 

Link to comment
Share on other sites

You have a comma after your 2nd last bracket, it's probably messing up the function.

screenWrap: function(sprite) {
		if(sprite.x < 0)
		{
			sprite.x = 512;
		}
		else if(sprite.x > 512)
		{
			sprite.x = 0;
		}
		
		if(sprite.y < 0)
		{
			sprite.y = 512;
		}
		else if(sprite.y > 512)
		{
			sprite.y = 0;
		}
	},   //<<<<<<<<---- Should not be a comma, remove it and all should be well
};

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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