Jump to content

Problem with loops


Floki
 Share

Recommended Posts

Hi everybody!

I have a problem with a loop in my game. The problem is this:

I have a loop that after certain seconds it executes and generates some sprites in the game (it takes it from a group), it works fine the first time. But after the specified seconds pass and the loop executes again it throws me this error: 

"Uncaught TypeError: Cannot read property 'apply' of undefined"

Here's the Code (there are 3 loops in total, but for the example i just focused in the first).

/Creation of the fire traps
		this.fireballs = game.add.group();
		this.fireballs.createMultiple(100, 'fireball');
		this.fireballs.callAll('animations.add', 'animations', 'shoot', [0,1,2,3], 16, true);
		game.physics.arcade.enable(this.fireballs);
		this.fireballs.enableBody = true;

//Multiple Fire traps
		this.loop1 = game.time.events.loop(2000, this.activateMultipleFireTraps('1'), this);
		//this.loop2 = game.time.events.loop(4000, this.activateMultipleFireTraps(2), this);
		//this.loop3 = game.time.events.loop(6000, this.activateMultipleFireTraps(3), this);

activateMultipleFireTraps: function(position){
		if(position == '1'){
			var firePositions = [[528, 7488], [528, 7360], [528, 7232], [528, 7088]];
			var index = 0;
			for(index = 0; index < firePositions.length; index++){
				var fire = this.fireballs.getFirstDead();
				fire.anchor.setTo(0.5);
				fire.scale.setTo(0.5);
				fire.scale.x = -1;
				fire.reset(firePositions[index][0], firePositions[index][1]);
				fire.body.velocity.x = -150;
				fire.animations.play('shoot');
			}
		}
		else if(position == 2){
			var firePositions = [[0, 7552], [0, 7424], [0, 7296], [0, 7168]];
			var index = 0;
			for(index = 0; index < firePositions.length; index++){
				var fire = this.fireballs.getFirstDead();
				fire.anchor.setTo(0.5);
				fire.scale.setTo(0.5);
				fire.scale.x = 1;
				fire.reset(firePositions[index][0], firePositions[index][1]);
				fire.body.velocity.x = 150;
				fire.animations.play('shoot');
			}
		}
		else{
			var firePositions = [[112, 7040], [256, 7040], [400, 7040]];
			var index = 0;
			for(index = 0; index < firePositions.length; index++){
				var fire = this.fireballs.getFirstDead();
				fire.anchor.setTo(0.5);
				fire.scale.setTo(0.5);
				fire.scale.y = -1;
				fire.reset(firePositions[index][0], firePositions[index][1]);
				fire.body.velocity.y = 150;
				fire.animations.play('shoot');
			}
		}	
	},

I dont know why it doesnt work.

 

Thanks!

Link to comment
Share on other sites

You're executing the function and passing the return (which is null) to the loop function.

In your case it is not working fine first time, it simply executing the first function immediately, when the loop actually tries to fire it has no callback to call (you have inadvertently passed it null by invoking the function) and you get the error.

this.loop1 = game.time.events.loop(2000, this.activateMultipleFireTraps, this);

Pass the function identifier to the event loop rather than invoking it. This will require some refactoring of your code if you need to pass values in to the function. You can either wrap the function in another function and pass the `position` parameter through, this is called a thunk, or you could create separate functions for each `position`, in your case this is actually perfectly valid as you are using `position` solely as an ID (or enum) but in other cases this does not work so the general case solution is to use a thunk but its probably easiest just for you to create separate functions for each event loop (I'd actually recommend passing through the positions to a function, then you can refactor your code to be DRYer).

Link to comment
Share on other sites

16 hours ago, mattstyles said:

You're executing the function and passing the return (which is null) to the loop function.

In your case it is not working fine first time, it simply executing the first function immediately, when the loop actually tries to fire it has no callback to call (you have inadvertently passed it null by invoking the function) and you get the error.


this.loop1 = game.time.events.loop(2000, this.activateMultipleFireTraps, this);

Pass the function identifier to the event loop rather than invoking it. This will require some refactoring of your code if you need to pass values in to the function. You can either wrap the function in another function and pass the `position` parameter through, this is called a thunk, or you could create separate functions for each `position`, in your case this is actually perfectly valid as you are using `position` solely as an ID (or enum) but in other cases this does not work so the general case solution is to use a thunk but its probably easiest just for you to create separate functions for each event loop (I'd actually recommend passing through the positions to a function, then you can refactor your code to be DRYer).

Holy Shit!!! Now i understand why it doesnt work! You where very very clear, I have fixed the problem already!! , i did 3 diferents functions (it was the most easy but no elegant :P ).

 

Thank you so much!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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