Jump to content

Properly deleting Text object.


sib_konst
 Share

Recommended Posts

Hi there.

I'm not shure, that I've correctly understood the way Object.destroy() works.

I have a language selection state at the begining of my project. This is a scene that contains two buttons and text label. Also, as every scene in project, it contains a simple loader.

 

index.htm

<!doctype html> <html> 	<head> 		<meta charset="UTF-8" />		<title></title>		<script type="text/javascript" src="src/phaser.min.js"></script>		<script type='text/javascript' src='src/langSelect.js'></script>		<script type='text/javascript' src='src/mainMenu.js'></script>		<script type='text/javascript' src='src/Credits.js'></script>		<script type="text/javascript" src="src/scene1.js"></script>		<style type="text/css">			body {				margin: 0;			}		</style>	</head>	<body>		<script type='text/javascript'>			//width and height of the game			var width=848;			var height=480;						window.onload=function(){				var game = new Phaser.Game(width, height, Phaser.AUTO, 'game');				game.state.add('langSelect',langSelect);				game.state.add('mainMenu',mainMenu);				game.state.add('Credits',Credits);				game.state.add('Scene1',Scene1);				game.state.start('langSelect');			};		</script>	</body></html>

langSelect.js

var langSelect=function(game){	var btnEng;	var btnRus;	var loadText;	var langText;	var pressed=false;	var loaded=false;};langSelect.prototype={	preload: function(){},	create: function(){		this.load.onFileComplete.add(this.fileComplete,this);		this.load.onLoadComplete.add(this.loadComplete,this);		this.loadText=this.add.text(50,this.game.world.height/2,'Loading...',{fill:'#FFFFFF'});			this.start();	},	start: function(){		this.game.load.image('btnEng','../pics/interface/english.png');		this.game.load.image('btnRus','../pics/interface/russian.png');		this.game.load.start();	},	fileComplete: function(progress,cacheKey,success,totalLoaded,totalFiles){		this.loadText.setText('File complete: '+progress+'% - '+totalLoaded+' out of '+totalFiles);	},	loadComplete: function(){		var self=this;		this.langText=this.add.text(50,this.game.world.height/2.5,'Please select your language:',{fill: '#ffa800'});		this.btnEng=this.game.add.button(0,this.game.world.height/2,'btnEng',function(){			localStorage.setItem('lang','eng');			self.pressed=true;		},null,0,0,0);		this.btnRus=this.game.add.button(0,this.game.world.height/2,'btnRus',function(){			localStorage.setItem('lang','rus');			self.pressed=true;		},null,0,0,0);		this.btnEng.x=this.game.world.width/2-this.btnEng.width;		this.btnRus.x=this.game.world.width/2+this.btnRus.width;				this.loaded=true;	},	update: function(){		if (this.loaded){			this.loadText.destroy();		};		if (this.pressed){			this.pressed=false;			this.loaded=false;			this.game.state.start('mainMenu');		}	}};

When I click any button, I'm getting that error:
 

Uncaught TypeError: Cannot set property 'font' of nullb.Text.updateText @ phaser.min.js:10b.Text.updateTransform @ phaser.min.js:3Object.defineProperty.set @ phaser.min.js:10b.Text.setText @ phaser.min.js:3langSelect.fileComplete @ langSelect.js:23b.SignalBinding.execute @ phaser.min.js:6b.Signal.dispatch @ phaser.min.js:6dispatch @ phaser.min.js:6b.Loader.nextFile @ phaser.min.js:13b.Loader.fileComplete @ phaser.min.js:13a.data.onload @ phaser.min.js:13

23 line of langSelect.js is:

this.loadText.setText('File complete: '+progress+'% - '+totalLoaded+' out of '+totalFiles);

But this text object destroyed right after scene load.

 

Please tell me what I'm doing wrong. Thanks

Link to comment
Share on other sites

Be careful, you're destroying your loadText every update().

 

So the first update() loop it will work fine, but after that you will try to destroy() undefined, thus the error. (Because this.loaded = true at the end of the loadComplete function, so "if (this.loaded)" is always true).

But if I change langSelect.js to

var langSelect=function(game){	var btnEng;	var btnRus;	var loadText;	var langText;	var pressed=false;	var loaded=false;};langSelect.prototype={	preload: function(){},	create: function(){		this.load.onFileComplete.add(this.fileComplete,this);		this.load.onLoadComplete.add(this.loadComplete,this);		this.loadText=this.add.text(50,this.game.world.height/2,'Loading...',{fill:'#FFFFFF'});			this.start();	},	start: function(){		this.game.load.image('btnEng','../pics/interface/english.png');		this.game.load.image('btnRus','../pics/interface/russian.png');		this.game.load.start();	},	fileComplete: function(progress,cacheKey,success,totalLoaded,totalFiles){		this.loadText.setText('File complete: '+progress+'% - '+totalLoaded+' out of '+totalFiles);	},	loadComplete: function(){		var self=this;		this.langText=this.add.text(50,this.game.world.height/2.5,'Please select your language:',{fill: '#ffa800'});		this.btnEng=this.game.add.button(0,this.game.world.height/2,'btnEng',function(){			localStorage.setItem('lang','eng');			self.pressed=true;		},null,0,0,0);		this.btnRus=this.game.add.button(0,this.game.world.height/2,'btnRus',function(){			localStorage.setItem('lang','rus');			self.pressed=true;		},null,0,0,0);		this.btnEng.x=this.game.world.width/2-this.btnEng.width;		this.btnRus.x=this.game.world.width/2+this.btnRus.width;				this.loaded=true;	},	update: function(){		if (this.loaded && this.loadText.visible){			this.loadText.visible=false;		};		if (this.pressed){			this.pressed=false;			this.loaded=false;			this.game.state.start('mainMenu');		}	}};

I still get the same error. In that case this.loadText not deleting at all.

And if I try to destroy it on on loadComplete():

var langSelect=function(game){	var btnEng;	var btnRus;	var loadText;	var langText;	var pressed=false;	var loaded=false;};langSelect.prototype={	preload: function(){},	create: function(){		this.load.onFileComplete.add(this.fileComplete,this);		this.load.onLoadComplete.add(this.loadComplete,this);		this.loadText=this.add.text(50,this.game.world.height/2,'Loading...',{fill:'#FFFFFF'});			this.start();	},	start: function(){		this.game.load.image('btnEng','../pics/interface/english.png');		this.game.load.image('btnRus','../pics/interface/russian.png');		this.game.load.start();	},	fileComplete: function(progress,cacheKey,success,totalLoaded,totalFiles){		this.loadText.setText('File complete: '+progress+'% - '+totalLoaded+' out of '+totalFiles);	},	loadComplete: function(){		var self=this;		this.loadText.destroy();		this.langText=this.add.text(50,this.game.world.height/2.5,'Please select your language:',{fill: '#ffa800'});		this.btnEng=this.game.add.button(0,this.game.world.height/2,'btnEng',function(){			localStorage.setItem('lang','eng');			self.pressed=true;		},null,0,0,0);		this.btnRus=this.game.add.button(0,this.game.world.height/2,'btnRus',function(){			localStorage.setItem('lang','rus');			self.pressed=true;		},null,0,0,0);		this.btnEng.x=this.game.world.width/2-this.btnEng.width;		this.btnRus.x=this.game.world.width/2+this.btnRus.width;				this.loaded=true;	},	update: function(){		if (this.pressed){			this.pressed=false;			this.loaded=false;			this.game.state.start('mainMenu');		}	}};

Error is still the same.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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