espace

problem to access this.timer in prototype

Recommended Posts

hi,

I can access to this.timer_text in the bottom of my function.

Can you help me ?

 

//////////////////////////////////////////////////////////////////////////////////////////
//huds.js
Timer = function(game,Group){
	var timer_value="40"
	this.Group=Group
	this.timer_text = game.add.bitmapText(w2,h2,'lucky',timer_value, w*.15)
	this.flag = true
	game.time.events.loop(Phaser.Timer.SECOND, updateCounter,this)

	function updateCounter() {
		if (this.flag){
			if (timer_value == 0) {
			}
			else {
				timer_value--
			}
			this.timer_text.setText(timer_value)
		}
	}
	//modif anchors
	this.timer_text.anchor.x=.5
	this.timer_text.anchor.y=.4

	this.angle_array=[2820,2920,2740,2850,2760,1700,2800,2910,2020,2930]
	this.angular = this.angle_array[Math.floor(game.rnd.between(1,this.angle_array.length-1))];

	Phaser.Sprite.call(this,game,w2,h2,'roll_turn')
	this.anchor.x=.5
	this.anchor.y=.5

	//cache au debut	
	this.visible=false
	this.alpha=0
	//ajout aux groupes
	this.Group.add(this.timer_text)
	this.Group.add(this)

}

Timer.prototype = Object.create(Phaser.Sprite.prototype)
Timer.prototype.constructor = Timer

Timer.prototype.turn_chooce = function() {

	this.visible=true
	this.tween_main=game.add.tween(this).to({alpha:1},900,Phaser.Easing.Linear.None,true,0)
	this.tween=game.add.tween(this).to({angle:this.angular},1000,Phaser.Easing.Circular.Out,true,1000)
	
	this.tween.onComplete.add(move_timer,this)

///////////////////////////////////////////////////////////////////////
//////////////HERE MY ERROR I CAN ACCESS TO THIS.TIMER_TEXT > UNDEFINED
	function move_timer(){
		this.flag=true
		this.timer_text.visible=true
		var tween00=game.add.tween(this.Group).to({x:0,y:h2-150},300,Phaser.Easing.Linear.None,true)
		tween00.onComplete.add(next_tw,this)
	}

	function next_tw(){
		background.flag_close=true
	}

}

 

Share this post


Link to post
Share on other sites

hi drhayes,

maybe i think i was editing my post and you see this : "console.log(this);" but he don't exist in my example snippet.

the "turn_chooce" function show a circular arrow that rotate randomly and indicate who's beginning the game. i have put a screen capture of this.

how can you acces "this.timer_text" trough the function move_timer() ?

 

01.png

Share this post


Link to post
Share on other sites

hi, i do the same process with a simpliest jsfiddle and it works well....

https://jsfiddle.net/espace3d/f4nu9xfz/

 

Circle = function (game,Group) {
    Phaser.Sprite.call(this, game, 200, 200, 'circle')
    this.Group=Group
    Group.add(this)
};

Circle.prototype = Object.create(Phaser.Sprite.prototype)
Circle.prototype.constructor = Circle

Circle.prototype.hide = function() {

	var tw=game.add.tween(this).to({x:400},1000,Phaser.Easing.Linear.None,true)
	tw.onComplete.add(change_visibility,this)

	function change_visibility(){
           game.time.events.add(500, next, this)  
        }
   
        function next(){
           this.visible=false  
        }
};


var game = new Phaser.Game(800, 600, Phaser.CANVAS, 'phaser-example', { preload: preload,create: create, update: update, render: render })

var cercle
var group0

function preload() {
   game.load.image('circle', 'https://s13.postimg.org/xjhlzmiev/disc_png.png');
};

function create() { 
  group0=game.add.group()
  cercle=new Circle(game,group0)
  cercle.hide()
};

function update() {
};

function render() {
}

really i don't understand why i can access to "this.timer_text" in my previous snippet.

an assistance would be welcome.

 

 

Share this post


Link to post
Share on other sites

In your fiddle, when you call cercle.hide() , the "this" property is your "cercle" instance.

In your broken code, you might be calling "turn_chooce" from another scope, or context. If so, your "this" inside "turn_chooce" is not your Timer object, then you can't access "timer_text" - it doesn't exist.

That's why drhayes asked "What's calling turn_chooce?" Could you check drhayes's answer and shows us the result of the "console.log(this)"?

Share this post


Link to post
Share on other sites

hi Claudiovc,

I'm very sorry but i don't understand what you mean...i don't see the difference between Circle.prototype.hide() and Timer.prototype.turn_chooce() 

I have read these snippet a lot of times but i don't see the different scope.....

Share this post


Link to post
Share on other sites
	//in game.js	
....
var hud
hud=new Timer(this.game,G.timerGroup0)
....

//in transition.js

....
	this.move_timer_for_chooce=function(){	
		this.tween_move_timer_for_chooce=game.add.tween(this.g0).to({x:0,y:0},time_open_panel_background-200,Phaser.Easing.Linear.None,true,delay_for_chooce)
		//console.log(this.tween_move_timer_for_chooce, "tww")
		this.tween_move_timer_for_chooce.onComplete.add(next,this)
                
                //HERE
		function next(){
			hud.turn_chooce()
		}
	}
	this.move_timer_for_chooce()
}
.....

 

Share this post


Link to post
Share on other sites

The reason I was asking about "console.log(this);" wasn't because I saw your edited post, it was because I suspected that you were losing the context of the method from the call site. It's what Claudiovc was asking you, too: are you calling "turn_chooce" in some way that makes it lose its "this". From your snippet above, it looks like it should totally work, though.

So, yeah: what does "console.log(this);" say in the dev console when you put it at the beginning of "turn_chooce"? Of "move_timer"? That'll help figure out what objects those functions *think* their context is in and will help debug the issue.

Share this post


Link to post
Share on other sites

solved !

the error is this

//AFTER TRY A LOT OF THING SOMETIMES SNIPPET is POLLUTED :)

this.tween4.onComplete.add(next00,this)

function next00(){
check_angle()
}

check_angle=function(){
this.flag=false
}


MUST BE 

this.tween4.onComplete.add(check_angle,this)

check_angle=function(){
this.flag=false
}

Thanks both.

 

 

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.