Jump to content

dat gui can't modify a custom variable ?


espace
 Share

Recommended Posts

hi,

i'm searching to modify a variable with dat gui.

Dat.gui appears on my screen and i have no error.

The option "speed" appears also and i can modify the value but that do noting on my object. in fact i must access to this.weapon.fire() to update the result but i can't acess to it. What's the true syntax to access this function ?

thanks for your help. 

 

	_weapon = function(delay,posx,posy,speed,frequency,variance,angular,_flag,kill_with_world,special_color){
		this.special_color=special_color
		this.kill_with_world=kill_with_world
		this.delay=delay
		this.posx=posx
		this.posy=posy
		this.flag_explode=false
		this.speed=speed
		this.angular=angular
		this.frequency=frequency
		this._flag=_flag
		this.variance=variance
		this.sound_pop=game.add.audio('pop')
		this._flag=true
		//canon
		Phaser.Sprite.call(this,game,this.posx,this.posy,'canon')
		this.anchor.setTo(.5,.5)
		this.angle=this.angular
		this.inputEnabled=true
		this.input.enableDrag(true)
		this.events.onDragStop.add(logic_position,this)
		this.events.onDragStart.add(show_grid_on_logic_position,this)
		this.input.enableSnap(40,40,true,true)

		game.physics.arcade.enable(this);
		if(this.special_color=="vrai"){
			this.weapon=game.add.weapon(9,'bullet_color')
		}else{
			this.weapon=game.add.weapon(9,'bullet')	
		}

		if(this.kill_with_world=="faux"){
			for (var i = 0; i <  9; i++) {
				this.weapon.bulletCollideWorldBounds=true
				this.weapon.bullets.children[i].body.bounce.setTo(1,1)
			}
		}else{
			this.weapon.bulletKillType = Phaser.Weapon.KILL_WORLD_BOUNDS;
		}
		//  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 )
	}

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


//in state Level1//
//var gui
//var canon=[]
canon[0]=new _weapon(800,200,800,900,2990,0,180,hero.flag_level_complete,"vrai","faux")


gui=new dat.GUI()
gui.add(canon[0],'fire')
gui.add(canon[0],'speed',0,50)
//

 

Link to comment
Share on other sites

I'm not 100% sure (been a while since I touched dat.gui) but I think due to how dat.gui works it ends up passing by value rather than passing by reference (in some, if not all, cases) so its mutating a variable, just not one referencing your object.

Pretty sure you can give dat.gui a callback though to do what you want.

Link to comment
Share on other sites

hi  mattstyles,

thanks for your response. in fact i found this solution. i put a fire function that reinitialyze the variable. Hope it helps someone.

	_weapon.prototype.fire = function() {
		this._flag=true
		this.weapon.fireRate = this.frequency ;
		this.weapon.bulletSpeed = this.speed;
		this.angle=this.angular
		this.weapon.bulletAngleVariance = this.variance;
		game.time.events.add( 10,function(){this._flag=false},this )
	}

//

gui=new dat.GUI()
	if(canon[0].visible){
			gui.add(canon[0],'name')
			gui.add(canon[0],'fire')
			gui.add(canon[0],'speed',0,5000)
			gui.add(canon[0],'frequency',0,5000)
			gui.add(canon[0],'angular',0,360)
			gui.add(canon[0],'variance',0,1000)
	}
//

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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