Jump to content

Weapon with particles...lag, why?


espace
 Share

Recommended Posts

hi,

I would use a special effect with my prototype weapon. this special effect is : some particles when the weapon fire.

concern the lines :
        this.animate_when_fire()    
        game.time.events.loop( this.frequency,this.animate_when_fire,this )

When is use the function animate_when_fire with a loop, after a certain delay my game lag a lot.

However i  use only 3 particle each time of frequency (+/- 200ms).

I use the same process for my other particles and i don't see any lag... what i'm doing wrong ?

thanks for the assist :)

 

 

	_canon = function(delay,posx,posy,speed,frequency,variance,angular,_flag,kill_with_world){
		this.delay=delay
		this.posx=posx
		this.posy=posy
		this.speed=speed
		this.frequency=frequency
		this.variance=variance
		this.angular=angular
		this.kill_with_world=kill_with_world
		this._flag=_flag
		this.flag_for_fire=true
		this._flag=true
		//canon
		Phaser.Sprite.call(this,game,this.posx,this.posy,'canon')
		this.angle=this.angular

		this.animate_when_fire()	
		game.time.events.loop( this.frequency,this.animate_when_fire,this )

		game.physics.arcade.enable(this);
		this.weapon=game.add.weapon(9,'bullet')	

		if(this.kill_with_world){
			this.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
		}else{
			for (var i = 0; i <  9; i++) {
				this.weapon.bulletCollideWorldBounds=true
				this.weapon.bullets.children[i].body.bounce.setTo(1,1)
			}
		}
		//  Because our bullet is drawn facing up, we need to offset its rotation:
		this.weapon.bulletAngleOffset = 0;

		//  The speed at which the bullet is fired
		this.weapon.bulletSpeed = this.speed;

		//  Speed-up the rate of fire, allowing them to shoot 1 bullet every 60ms
		this.weapon.fireRate = this.frequency ;

		//  Add a variance to the bullet angle by +- this value
		this.weapon.bulletAngleVariance = this.variance;

		//  Tell the Weapon to track the 'player' Sprite, offset by 14px horizontally, 0 vertically
		this.weapon.trackSprite(this,0,0,true);
		game.time.events.add( this.delay,function(){this._flag=false},this )
	}

	_canon.prototype = Object.create(Phaser.Sprite.prototype)
	_canon.prototype.constructor = _canon
	_canon.prototype.update = function(){
		if(this._flag==false && this.flag_for_fire){
			this.weapon.fire() 
		}
	}

	_canon.prototype.fire = function() {
		this.flag_for_fire=true
		this.weapon.fireRate = this.frequency ;
		this.weapon.bulletSpeed = this.speed;
		this.angle=this.angular
		this.weapon.bulletAngleVariance = this.variance;
	}

	_canon.prototype.animate_when_fire = function() {
		if(this.visible){
			this.particlex = game.add.emitter(this.x+35,this.y)
			this.particlex.makeParticles("particle_canon")
			this.particlex.minParticleSpeed.setTo(100,-190)
			this.particlex.maxParticleSpeed.setTo(500,240)
			this.particlex.setAlpha(.4, .1)
			this.particlex.minParticleScale = .4
			this.particlex.maxParticleScale = .7
			this.particlex.minRotation = 0
			this.particlex.maxRotation = 0
			this.particlex.on=false
			this.particlex.start(true,200,null,3)
		}
	}

 

 

Link to comment
Share on other sites

Am I wrong or are you creating particle emitter on every frame? 

this.particlex = game.add.emitter(this.x+35,this.y)

you should probably create one emitter for all your effects and only call emit on that one emitter.

Also, read about object pooling phaser has some build in particle emitters but you can make your own pools for other sprites.

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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