Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

bullets access to a specific bullet ?


espace
 Share

Recommended Posts

hi, 

i start using the bullet in phaser and i would to know how to access to a specific bullet. i don't see how to access for example the bullet[1] ?

could you give me the solution ?

thanks :)

 

bullet_group=function(game,frequency,_character){
	this._character=_character
	this.frequency=frequency

	Phaser.Group.call(this,game)
	this.enableBody=true
	this.physicsBodyType= Phaser.Physics.ARCADE
	this.bullet
	this.time=0
	this.canon=game.add.sprite(0,400,'rect')
	this.canon.alpha=0
	for (var i = 0; i < 8; i++)
	{
		var b = this.create(0, 0, 'rect');
		b.name = 'bullet' + i;
		b.tint=0x000000
		b.exists = false;
		b.visible = false;
		b.checkWorldBounds = true;
		b.explode=this.explode_bullet()
		b.events.onOutOfBounds.add(resetBullet, this);
	}
	this.fire()
}

bullet_group.prototype = Object.create(Phaser.Group.prototype);
bullet_group.prototype.constructor = bullet_group;

bullet_group.prototype.fireBullet = function() {
	if (game.time.now > this.time)
	{
		this.bullet = this.getFirstExists(false);

		if (this.bullet)
		{
			this.bullet.reset(this.canon.x, this.canon.y);
			this.bullet.body.velocity.x= 300;
			this.bullet_time = game.time.now + 550;
		}
	}

}
bullet_group.prototype.fire = function() {
	game.time.events.loop(this.frequency,this.fireBullet,this)
}

 

Link to comment
Share on other sites

easy peasy

    // other code goes here...
	this.canon=game.add.sprite(0,400,'rect')
	this.canon.alpha=0
	this.bullets=[]
	for (var i = 0; i < 8; i++)
	{
		this.bullets[i] = this.create(0, 0, 'rect');
		this.bullets[i].name = 'bullet' + i;
        // etc
	}
	this.fire()

That should solve your problems.

I would probably use a Group and createMultiple instead. http://phaser.io/docs/2.6.2/Phaser.Group.html#createMultiple

Read the documentation of Phaser.Group for other ways to add/remove things in a more efficient way.

Even if you don't want to, you can always just keep and array like I said. If you had a Group, you can make sure only bullets are added and access myGroup.children instead. 

Link to comment
Share on other sites

hi again, i have a question :)

when i create a new bullet the second bullet have the same angle than the first. changing my velocity.x and velocity.y change nothing ...

could you take a look ?


bullet_group=function(game,frequency,_character,xv,yv,posx,posy){
	this._character=_character
	this.frequency=frequency
	this.posx=posx
	this.posy=posy
	this.xv=xv
	this.yv=yv

	Phaser.Group.call(this,game)
	this.enableBody=true
	this.physicsBodyType= Phaser.Physics.ARCADE
	this.bullet
	this.time=0
	this.canon=game.add.sprite(this.posx,this.posy,'rect')
	this.canon.alpha=0
	this.bullets=[]
	for (var i = 0; i < 8; i++)
	{
		this.bullets[i] = this.create(0, 0, 'rect');
		this.bullets[i].name = i
		this.bullets[i].tint=0x000000
		this.bullets[i].exists = false;
		this.bullets[i].visible = false;
		this.bullets[i].checkWorldBounds = true;
	}
	this.fire()
}

bullet_group.prototype = Object.create(Phaser.Group.prototype);
bullet_group.prototype.constructor = bullet_group;

bullet_group.prototype.fireBullet = function() {
	if (game.time.now > this.time)
	{
		this.bullet = this.getFirstExists(false);

		if (this.bullet)
		{
			this.bullet.reset(this.canon.x,this.canon.y);
			this.bullet.body.velocity.y= this.xy;
			this.bullet.body.velocity.x= this.xv;
			this.bullet_time = game.time.now + 550;
		}
	}

}
bullet_group.prototype.fire = function() {
	game.time.events.loop(this.frequency,this.fireBullet,this)
}

var game_state = {
	create: function(){
		this.canon=[]
		this.canon[0]=new bullet_group(game,1800,this.hero,300,300,0,400)
		this.canon[1]=new bullet_group(game,1000,this.hero,-50,-200,500,800)

		for (var i = 0; i < this.canon.length; i++){
		game.add.existing(this.canon[i])
		}
		game.add.existing(this.hero)
	},
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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