Jump to content

Enemy Firing


noodlesdefyyou
 Share

Recommended Posts

I'm sure I'm overlooking simple something here. To teach myself, I've been using invaders.js as a point of reference, and expanding upon it.

 

 

As of right now, I've got 4 different enemy sprites grouped up loading up in 4 separate rows (similar to invaders), and I'm currently trying to get just one of the rows to shoot.

 

In this spoiler is the entire javascript code I've got so far. Ignore all the comments, They're mostly there as placeholders and reminders for myself. In the end I'm going to try to have all 4 groups fire randomly and indepentantly of the other groups, though for now I just want ANY of them to shoot, and can't figure out what I've got wrong.

 

 

Also, what do these values do/edit in invaders.js (lines 105/106 for reference) :

 

enemies.x = 100;
enemies.y = 50;

 

var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'spacegame', { preload: preload, create: create, update: update } );		function preload(){				game.load.image('background', 'assets/bg.png');		game.load.image('pBlaster', 'assets/pblaster.png');		game.load.image('eBlaster', 'assets/eblaster.png');		game.load.spritesheet('ship', 'assets/ship.png', 50, 40);		game.load.spritesheet('drone1', 'assets/enemydrone1.png', 32, 50);		game.load.spritesheet('drone2', 'assets/enemydrone2.png', 80, 51);		game.load.spritesheet('fighter', 'assets/enemyheavyfighter.png', 80, 70);		game.load.spritesheet('boss1', 'assets/enemyboss1.png', 150, 191);		game.load.spritesheet('medic', 'assets/enemymedic.png', 60, 50);		game.load.audio('gunshot', 'sounds/gunshot.wav');		game.load.audio('engine', 'sounds/engine.wav');		game.load.audio('hit', 'sounds/explode.wav');			}		//Player	var cursors;	var player;		//Enemy Sprites	var enemy1;	var enemy2;	var boss1;	var medic;	var fighter;		//Enemy Groups	var Enemies;	var livingDrone1 = [];			//Sound	var shot;	//var engine;	var hit;		//Blaster	var pBlasters;	var pBlasterTime = 0;	var fireButton;	var firingTimer = 0;	var eBlasters;		//Score+Status	var score = 0;	var scoreString = '';	var scoreText;	var shotsfired = 0;	var shotsfiredString = '';	var shotsfiredText;	var eshotsfired = 0;	var eshotsfiredText;	var eshotsString = '';		//var scoreStyle = { font: "34px Arial", fill = "#fff000" };			function create()	{		game.add.sprite(0, 0, 'background');				player = game.add.sprite(300, game.world.height - 100, 'ship');		player.anchor.setTo(0, 0);		//enemy1 = game.add.sprite(100, game.world.height - 470, 'drone1');		//enemy1.body.setPolygon(0, 0, 32, 0, 16, 50);		//enemy2 = game.add.sprite(180, game.world.height - 470, 'drone2');		//fighter = game.add.sprite(480, game.world.height - 470, 'fighter');		//medic = game.add.sprite(600, game.world.height - 470, 'medic');		//boss1 = game.add.sprite(300, game.world.height - 580, 'boss1');				shot = game.add.audio('gunshot');		//engine = game.add.audio('engine');		hit = game.add.audio('hit');				cursors = game.input.keyboard.createCursorKeys();		fireButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);				//Player Blaster Bullet Group		pBlasters = game.add.group();		pBlasters.createMultiple(30, 'pBlaster');		pBlasters.setAll('anchor.x', -3.6);		pBlasters.setAll('anchor.y', 1);		pBlasters.setAll('outOfBoundsKill', true);				//Enemy Blaster Bullet Group		eBlasters = game.add.group();		eBlasters.createMultiple(30, 'eBlaster');		eBlasters.setAll('anchor.x', 0.5);		eBlasters.setAll('anchor.y', 1);		eBlasters.setAll('outOfBoundsKill', true);				//Enemy Groups		Enemies = game.add.group();		Drone1 = game.add.group();		createDrone1();		createDrone2();		createFighter();		createMedic();			scoreString = 'Score : ';		scoreText = game.add.text(10, 10, scoreString + score, { font: '34px' , fill: '#fff000' });		shotsfiredString = 'Shots Fired : ';		shotsfiredText = game.add.text(10, 20, shotsfiredString + shotsfired, { font: '34px' , fill: '#fff000' });		eshotsfiredString = 'Enemy Shots Fired : ';		eshotsfiredText = game.add.text(10, 30, eshotsfiredString + eshotsfired, { font: '34px' , fill: '#fff000' });		}	function createDrone1() 	{		for (var y = 0; y < 1; y++)		{			for (var x = 0; x < 10; x++)			{				var Drone1 = Enemies.create(x * 32, y * 50, 'drone1');				Drone1.anchor.setTo(-7, 0);			}		}	}		function createDrone2() 	{		for (var y = 1; y < 2; y++)		{			for (var x = 0; x < 10; x++)			{				var enemy = Enemies.create(x * 80, y * 52, 'drone2');				enemy.anchor.setTo(0, 0);			}		}	}		function createFighter() 	{		for (var y = 2; y < 3; y++)		{			for (var x = 0; x < 10; x++)			{				var enemy = Enemies.create(x * 80, y * 70, 'fighter');				enemy.anchor.setTo(0, 0.5);			}		}	}		function createMedic() 	{		for (var y = 3; y < 4; y++)		{			for (var x = 0; x < 10; x++)			{				var enemy = Enemies.create(x * 60, y * 50, 'medic');				enemy.anchor.setTo(-1.5, -0.5);			}		}	}	function update()	{		    player.body.velocity.x = 0;		player.body.velocity.y = 0;		//engine.play();				//Player Controls		if (cursors.up.isDown)		{			player.body.velocity.y = -200;		}		if (cursors.down.isDown)		{			player.body.velocity.y = 200;		}		if (cursors.left.isDown)		{			player.body.velocity.x = -250;		}		if (cursors.right.isDown)		{			player.body.velocity.x = 250;		}		if (fireButton.isDown)		{			firepBlaster();		}				if (game.time.now > firingTimer)		{			eBlasterFire();		}				//game.physics.overlap(bullets, enemy2, collisionHandler, null, this);				//function collisionHandler (bullets, enemy2);		//{		//	bullet.kill();		//	hit.play();		//}			}		function firepBlaster () 	{				if (game.time.now > pBlasterTime)		{			pBlaster = pBlasters.getFirstExists(false);			if (pBlaster)			{				pBlaster.reset(player.x, player.y + 8);				pBlaster.body.velocity.y = -400;				pBlasterTime = game.time.now + 200;				shotsfired += 1;				shotsfiredText.content = shotsfiredString + shotsfired;				shot.play();			}		}	}		function eBlasterFire()	{		eBlaster = eBlasters.getFirstExists(false);		livingDrone1.length = 0;		Drone1.forEachAlive(function(Drone1) 		{			livingDrone1.push(Drone1);		});				if (eBlaster && livingDrone1.length > 0)		{			var random=game.rnd.intergerInRange(0,livingDrone1.length);			var shooter=livingDrone1[random];			eBlaster.reset(shooter.body.x, shooter.body.y);			eBlaster.body.velocity.y = 400;			firingTimer = game.time.now + 2000;			eshotsfired +=1			eshotsfiredText.content = eshotsfiredString + eshotsFired;			shot.play();		}	}		//enemies.x = 100;	//enemies.y = 50;		function resetpBlaster(pBlaster) 	{		pBlaster.kill();	}	function reseteBlaster(eBlaster)	{		eBlaster.kill();	}	//function render()	//{	//	game.debug.renderPhysicsBody(enemy1.body);	//	game.debug.renderPhysicsBody(enemy2.body);	//}

 

 

Edit: I forgot to add, the game loads and runs just fine right now, debug console doesn't show any conflicts (that I can see at least)

Link to comment
Share on other sites

I believe it's 

aliens.x = 100;aliens.y = 50;

This will position the aliens (enemies) group at the specified x and y, translating all the individual enemies. Other than that I can't really figure out what's the problem. Could you describe what happens? Also, try adding some console.log() statements in the eBlasterFire() function to see if everything is ok.

 

EDIT: Could you trace the eBlaster.body.x and eBlaster.body.y values just after the eBlaster.reset(shooter.body.x, shooter.body.y); statement? 

Link to comment
Share on other sites

nothing happens, thats the problem. the topmost group of enemies (drone1) are supposed to randomly fire, but nothing happens. i added a counter just to see if the loop was working and not displaying a graphic, and nothing. the counter sits at 0, the enemies dont fire. ill look over my code again, but i have a feeling im feeding the random line to the wrong group somehow.

Link to comment
Share on other sites

the debugger is your friend. set a breakpoint in eBlasterFire() and see if it gets called. if it does, step through it and see what's going on. are you getting an eBlaster and does livingDrone1 have objects in it? is the random number you're getting fall into the right range? etc

 

learn to use the debugger. it will pay off more than you can imagine!

Link to comment
Share on other sites

since it appears that function fireeblaster() is not working, i added a console.log(); to the end of the function like this:

 

	function fireeBlaster()	{		eBlaster = eBlasters.getFirstExists(false);		livingDrone1.length = 0;		Drone1.forEachAlive(function(Drone1) 		{			livingDrone1.push(Drone1);			console.log();		});						if (eBlaster && livingDrone1.length > 0)		{			var random=game.rnd.intergerInRange(0,livingDrone1.length);			var shooter=livingDrone1[random];			eBlaster.reset(shooter.body.x, shooter.body.y);			eBlaster.body.velocity.y = 400;			firingTimer = game.time.now + 2000;			eshotsfired +=1			eshotsfiredText.content = eshotsfiredString + eshotsFired;			shot.play();		}	console.log();	}	

 

And when I check the debugger, all I get is a fast moving 'repeat' counter on the line that shows console.log() with no messages or anything. I guess my next step is to completely comment out that function, and see if it breaks.

Link to comment
Share on other sites

your next step should be to put a breakpoint in 'fireeblaster' and step through the function, examining the values of your variables as you go

 

- open the 'developer tools' window in your browser (I'm going to assume chrome - other browsers vary somewhat, but all modern browsers offer similar functionality)

 

- go to the 'sources' pane/tab. press Ctrl-O to open your source file

 

- navigate to the function / line where you want to stop the execution

 

- click in the left-hand 'gutter' of this line - a glyph will appear to indicate that you have placed a breakpoint. code execution will now STOP at this location and let you examine variables etc.

 

- (re)load the page, if necessary. depending on where you put the breakpoint you may have to wait or take some action (press the 'fire' key, whatever)

 

- execution of code should stop with a message "paused in the debugger" appearing and the line with the breakpoint will be highlighted in the 'sources' window.

 

- if the execution never stops then your function is never being called. place a new breakpoint somewhere earlier in the code and try again

 

- assuming execution stops you can examine the values of your variables in the "scope variables" pane on the right of the 'sources' pane, and use the 'step over' and 'step into' buttons to step over the execution of code line-by-line, examining variable values as you go and figuring out what your code is doing.

 

using console.log and commenting out blocks of code is an extremely poor substitute for actually using the debugger

 

in my experience, the primary difference between a good developer and a mediocre one is the skill that he/she has with their tools. a debugger is one of the most important tools a developer has!

Link to comment
Share on other sites

for firefox, try this:

 

https://developer.mozilla.org/en-US/docs/Tools/Debugger

 

(the layout etc is highly similar to chrome though, it really only differs in the details. I do highly recommend chrome, if only for the debugging tools. I'm a long-time firefox user and fan myself, but I also believe in using the best tools available, and for HTML5 development, at the moment, chrome wins :/ )

Link to comment
Share on other sites

The console.log() should have a message passed as a parameter. It will trace out that message to the console, nothing else. Since you say the counter is incrementing in the console then the function works just fine. I think it's a problem with the starting position for your bullets. In the createDrone1() function you set an anchor point of -7 on the x axis. Why so high? I think the  bullets start out of bounds from the beginning hence they are destroyed as soon as they are added to the stage. But to be sure add the following line (indicated by the comment) in the eBlasterFire() function and tell me what values you get.

function eBlasterFire(){   eBlaster = eBlasters.getFirstExists(false);   livingDrone1.length = 0;   Drone1.forEachAlive(function(Drone1)   {      livingDrone1.push(Drone1);   });   if (eBlaster && livingDrone1.length > 0)   {      var random=game.rnd.intergerInRange(0,livingDrone1.length);      var shooter=livingDrone1[random];      console.log('Shooter pos: ', shooter.body.x, shooter.body.y);  // ADD THIS LINE      eBlaster.reset(shooter.body.x, shooter.body.y);      eBlaster.body.velocity.y = 400;      firingTimer = game.time.now + 2000;      eshotsfired +=1      eshotsfiredText.content = eshotsfiredString + eshotsFired;      shot.play();   }}
Link to comment
Share on other sites

 

The console.log() should have a message passed as a parameter. It will trace out that message to the console, nothing else. Since you say the counter is incrementing in the console then the function works just fine. I think it's a problem with the starting position for your bullets. In the createDrone1() function you set an anchor point of -7 on the x axis. Why so high? I think the  bullets start out of bounds from the beginning hence they are destroyed as soon as they are added to the stage. But to be sure add the following line (indicated by the comment) in the eBlasterFire() function and tell me what values you get.

function eBlasterFire(){   eBlaster = eBlasters.getFirstExists(false);   livingDrone1.length = 0;   Drone1.forEachAlive(function(Drone1)   {      livingDrone1.push(Drone1);   });   if (eBlaster && livingDrone1.length > 0)   {      var random=game.rnd.intergerInRange(0,livingDrone1.length);      var shooter=livingDrone1[random];      console.log('Shooter pos: ', shooter.body.x, shooter.body.y);  // ADD THIS LINE      eBlaster.reset(shooter.body.x, shooter.body.y);      eBlaster.body.velocity.y = 400;      firingTimer = game.time.now + 2000;      eshotsfired +=1      eshotsfiredText.content = eshotsfiredString + eshotsFired;      shot.play();   }}

http://i.imgur.com/uqrO1WE.png

 

As you can see, nothing gets called to console now. I even set the pos to 0,0 (Drone1.anchor.setTo(0, 0);), and you'll notice the top row got pushed over to the far left corner. For some reason that I've yet to figure out, setting it to -7 'centered' the line.

Link to comment
Share on other sites

Could you add just above that if statement the following code and tell me what it traces out, if it traces out anythig.

console.log('Blaster: ', eBlaster);console.log('# Living drones: ', livingDrone1.length);

If this does not trace anything then maybe the problem is outside the function. In this case add the commented line in your update loop:

console.log('Now: ', game.time.now, ' - Fire:', firingTimer); // ADD THIS LINEif (game.time.now > firingTimer) {    fireeBlaster(); }
Link to comment
Share on other sites

Adding the 2nd one created a whole lot of this extremely fast:

 

05:11:20.978 "Blaster: " [object Object] spacegame.js:239
05:11:20.978 "# Living drones: " 0 spacegame.js:240
05:11:20.996 "Now: " 15678 " - Fire:" 0 spacegame.js:194
05:11:20.996 "Blaster: " [object Object] spacegame.js:239
05:11:20.996 "# Living drones: " 0 spacegame.js:240
05:11:21.113 "Now: " 15787 " - Fire:" 0 spacegame.js:194
05:11:21.113 "Blaster: " [object Object] spacegame.js:239
05:11:21.114 "# Living drones: " 0 spacegame.js:240
05:11:21.145 "Now: " 15819 " - Fire:" 0 spacegame.js:194

 

 

So from what I can gather, livingDrone1 is not being updated correctly.

 

I'm going to go out on a limb and guess that this following code is incorrect:

		Drone1.forEachAlive(function(Drone1) 		{			livingDrone1.push(Drone1);		});

and needs to be updated like this:

 

Edit: Nope. Tried this change. doesn't work. so for some reason its not pushing livingdrone properly.

		livingDrone1.forEachAlive(function(Drone1) 		{			livingDrone1.push(Drone1);		});
Link to comment
Share on other sites

Ah, I've missed that. You woud need to get the living drones from your enemy group so it would be:

Enemies.forEachAlive(function(enemy) {     livingDrone1.push(enemy); });

By the way, all your enemies are added to the Enemy group as far as I notice. If you want to have them separated in groups you whould instantiate new groups for enemy types. 

Link to comment
Share on other sites

Great. Now it's saying TypeError: game.rnd.intergerInRange is not a function. GAH. Why did I ever decide to start this!? Lol.

 

Good news is #Living Enemies (in console log) is now showing 40. SO THATS WORKING AT LEAST. I'm also getting "Blaster: " [object Object] from this console line: console.log('Blaster: ', eBlaster); so ill have to figure that out too.

 

 

I've got some things I need to take care of IRL so it'll be a day or two before I can get back at this. Thanks for your help though! At least the string is updating correctly now.

Link to comment
Share on other sites

so i figured out the errors! first, interger isnt a word. i dont know how that happened. changed it back to integer. created 4 separate groups for the separate sprites. got one group to even fire! the sprites are broken-ish right now, because i reset the anchors back to 0, 0, but at least THEY SHOOT NOW.

 

 

now i just need to add that same function for the other 3 groups :)

Link to comment
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...
 Share

  • Recently Browsing   0 members

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