Jump to content

enemy spawning


TIS
 Share

Recommended Posts

Hi,

i just want to spawn an enemy. But i can't figure out whats wrong with my code.

It gives me the error "ReferenceError: game is not defined"

I highlited the parts where i stuck.

var theGame = function(game){};enemy = function(){	this.enemyRed2 = game.add.sprite(200, 200, 'enemyRed');};var player;var cursors;var enemies = [];theGame.prototype = {  	create: function()	{				this.game.stage.backgroundColor = '#c3c5a3';		player = this.game.add.sprite(100, 100, 'dude');		player.anchor.setTo(0.5, 0.5);		player.animations.add('walk', [0, 1, 2, 3], 10, true);			//player.animations.add('left', [0, 1, 2, 3], 10, true);		this.game.physics.arcade.enable(player);				cursors = this.game.input.keyboard.createCursorKeys();		this.wasd = 		{            up: this.game.input.keyboard.addKey(Phaser.Keyboard.W),            down: this.game.input.keyboard.addKey(Phaser.Keyboard.S),            left: this.game.input.keyboard.addKey(Phaser.Keyboard.A),            right: this.game.input.keyboard.addKey(Phaser.Keyboard.D),        };			enemies.push(new enemy());	},	update: function()	{		player.rotation = this.game.physics.arcade.angleToPointer(player);		player.body.velocity.y = 0;		player.body.velocity.x = 0;		if (cursors.down.isDown || this.wasd.down.isDown)		{			player.body.velocity.y = 150;		}		else if (cursors.up.isDown || this.wasd.up.isDown)		{			player.body.velocity.y = -150;		}		if (cursors.left.isDown || this.wasd.left.isDown)		{			player.body.velocity.x = -150;		}		else if (cursors.right.isDown || this.wasd.right.isDown)		{			player.body.velocity.x = 150;		}		if (player.body.velocity.y != 0)		{			player.animations.play('walk');		}		else if (player.body.velocity.x != 0)		{			player.animations.play('walk');		}		if (player.body.velocity.y == 0 && player.body.velocity.x == 0)		{			player.animations.stop();			player.frame = 0;		}	}}
Link to comment
Share on other sites

Looks like a reference error to me - can't be sure without seeing how you're setting up the rest of your game.

 

Also save yourself some typing: you don't need 'this.game.add' and 'this.game.input' and 'this.game.physics' - just use 'this.add', 'this.input', 'this.physics'. As long as you do that from within a Phaser State it will work just fine.

Link to comment
Share on other sites

Thank you. And thanks for the good tip.

Here is the rest of the game.

<!doctype html><html>	<head>    	<script src="js/phaser.min.js"></script>    	<script src="src/boot.js"></script>		<script src="src/preload.js"></script>		<script src="src/gametitle.js"></script>		<script src="src/thegame.js"></script>		<script src="src/gameover.js"></script>	    		<style>    			body{margin:0}    		</style>		<script>			(function() {				var game = new Phaser.Game(800, 600, Phaser.CANVAS, "game");				game.state.add("Boot",boot);				game.state.add("Preload",preload);				game.state.add("GameTitle",gameTitle);				game.state.add("TheGame",theGame);				game.state.add("GameOver",gameOver);				game.state.start("Boot");			})();    		</script>    </head>    <body>    </body></html>
var boot = function(game){	console.log("%cStarting the game", "color:white; background:red");};  boot.prototype = {	preload: function(){          this.game.load.image("loading","assets/loading.png"); 	},  	create: function(){		this.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;		this.scale.pageAlignHorizontally = true;		this.scale.setScreenSize();		this.game.state.start("Preload");	}}
var preload = function(game){}preload.prototype = {	preload: function()	{         var loadingBar = this.add.sprite(160,240,"loading");        loadingBar.anchor.setTo(0.5,0.5);        this.load.setPreloadSprite(loadingBar);		this.game.load.spritesheet("numbers","assets/numbers.png",100,100);		this.game.load.image("gametitle","assets/title.png");		this.game.load.image("play","assets/play.png");		this.game.load.image("gameover","assets/gameover.png");		this.game.load.spritesheet("dude", "assets/dude.png", 22, 40);		this.game.load.spritesheet("enemyRed", "assets/enemyRed.png", 22, 40);	},  	create: function()	{		this.game.state.start("GameTitle");	}}
var gameTitle = function(game){}var gameTitle;var playButton;gameTitle.prototype = {  	create: function()	{		gameTitle = this.game.add.sprite(400,160,"gametitle");		gameTitle.anchor.setTo(0.5,0.5);		playButton = this.game.add.button(400,320,"play",this.playTheGame,this);		playButton.anchor.setTo(0.5,0.5);	},	playTheGame: function(){		this.game.state.start("TheGame");	}}
Link to comment
Share on other sites

As Rich said eariler, i think the issue looks like it could be where you use these:

 

this.game.load.image("gametitle","assets/title.png");

 

where it could just be:

this.load.image("gametitle","assets/title.png");

 

This that may also solve your issues with reference. Maybe wrong though, as i'm still 'learning' the ropes as i go :)
 

Link to comment
Share on other sites

Thank you :)

But i think this is not the problem.

The problem is just there when i try it with

enemy = function(index, game){	this.enemyRed2 = game.add.sprite(200, 200, 'enemyRed');};

and

enemies.push(new enemy());

Then i get the error "ReferenceError: game is not defined". If i just type it like

this.enemyRed2 = add.sprite(200, 200, 'enemyRed');

i get the error: "add is not defined".

 

I tried a lot of things, but i can't figure it out :S

Link to comment
Share on other sites

I'm probably about to say something stupid but if you do :

enemies.push(new enemy());

That is, creating an enemy without passing any argument, then how can this code know what the game object is? (and the index also by the way)

enemy = function(index, game){	this.enemyRed2 = game.add.sprite(200, 200, 'enemyRed');};
Link to comment
Share on other sites

Well, the program tells you the problem is that game is not defined, I would check console.log(game) when inside your function to see what you are referencing to, then I would start adding a single enemy without any functions in create or update loop simple as this.newEnemy = this.add.sprite(x, y, 'enemyRed') to check if everything works ok.

 

I tried doing the same thing as you, and in my case it worked fine. Witch enemy constructor are you using?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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