Jump to content

Randomly Add Sprites to Game at Dynamic Intervals?


Philkin
 Share

Recommended Posts

Hello!
 
I have a game in which 'cloud' sprites are added to the game at a rate of 1 per second. They float across the screen from right to left. They are added off the screen horizontal, and between a random vertical range.
 
I want it so that as the player destroys these clouds, by clicking on them, clouds are added with less frequency.

 

This is what I have so far: 

function create(){        game.time.events.repeat(Phaser.Timer.SECOND * 1, 50, addCloud this);}function addCloud(){		cloudArray[cloudIndex] = game.add.sprite((Math.random() * (1000-900) + 900), (Math.random() * (600-200)), 'cloud1');	cloudArray[cloudIndex].anchor.x = 0.5;	cloudArray[cloudIndex].anchor.y = 0.5;	//Create tween and add to cloud object	var twCloud1 = game.add.tween(cloudArray[cloudIndex]);	twCloud1.to({x: -100}, (Math.random() * (7500 - 5000) + 5000));	twCloud1.start();	//Enable clicking	cloudArray[cloudIndex].inputEnabled = true;	cloudArray[cloudIndex].events.onInputDown.add(destroyCloud, {param: cloudArray[cloudIndex]});			//Destroy Cloud on out of bounds	cloudArray[cloudIndex].checkWorldBounds = true;	cloudArray[cloudIndex].events.onOutOfBounds.add(destroyCloud, {param: cloudArray[cloudIndex]});	cloudIndex++;	//if((Math.random()) > 0.5){		//Create cloud object and add to scene	//}} 

My question: Is it possible to change the interval of a timer event after it has been created?

 

Thanks

 

P

 

Link to comment
Share on other sites

Worked it out, I think.

Assigned the game.timer to a global object. Then accessed the .delay property.

timer = game.time.events.repeat(weighting, 50, addCloud, this);function updateWeight(){	if(weighting > 700){		weighting -= 100;		timer.delay = weighting;		scoreText.text = timer.delay;	}}

It works, but if there is a better way to do it I am all ears :)

P

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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