Sign in to follow this  
casey

game stage and image persistence

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

Share this post


Link to post
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');
  }

};

 

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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.