Jump to content

game stage and image persistence


casey
 Share

Recommended Posts

I am trying to test out persistence of objects--at the moment an image, but eventually a group that will be a UI component.

Right now, my image that I want to persist ("persist") goes from State1 to State2 just fine, but when I go back to State1, it disappears and I get an error ("uncaught typeError: cannot read property 'compressionAlgorithm of null in Phaser.js" and then points to a bunch of pixi.webgl render locations)

Why is the item disappearing if it's been added to the stage, and how do I overcome this problem?

 

Here is the code for State1:

var state1 = {
	
preload: function () {
		
		this.load.image('persist', 'persist.png');
		this.load.image('btn', 'btn.png');
	},
    
    create: function () {

		this.persist = game.add.image(this.world.centerX, this.world.centerY, 'persist');
		this.game.stage.addChild(this.persist);
		
		this.btn = this.add.image(200, 200, 'btn');
		this.btn.inputEnabled = true;
		this.btn.events.onInputDown.add(this.changeState, this); 		  
	},
	
	changeState: function() {
	 this.state.start("state2");
	}

 

And then State 2 is just taking us back to state1 on clicking the button

var state2 ={

	preload: function(){
			
		this.load.image('btn', 'inventory/btn.png');
		
	},

create: function(){
    	
		this.btn = this.add.image(200, 200, 'btn');
		this.btn.inputEnabled = true;
		this.btn.events.onInputDown.add(this.changeState, this); 
    
},
	
	changeState: function(){
		this.state.start("state1");
	}
   
    
};

Thanks in advance

Link to comment
Share on other sites

Use distinct keys for the two button images.

Either use checkImageKey in the preload calls or move all the preloading to a boot state (run only once), e.g., 

var boot = {

  preload: function () {
    this.load.image('persist', 'persist.png');
    this.load.image('btn', 'btn.png');
    this.load.image('btn-inventory', 'inventory/btn.png');
  },

  create: function () {
    this.state.start('state1');
  }

};

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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