Jump to content

Sprites with visible=false moving with camera


riosh
 Share

Recommended Posts

I'm working on a game project where I want to be able to switch between two "dimensions" in the same world. I though the easiest way to do this was to have a keyboard event, which toggles sprites' visibility when pressed.

This works fine, however, when the visibility is toggled off, and I move the character (and thus, the camera), the sprites seem to be keeping their position relative to the camera, and not the world. Which means that when I toggle the visibility on again, the sprites have moved from their original position relative to the world, but are in the same position relative to the camera. For example, if a sprite is off-camera while invisible and I move the character, the sprite gets pushed off the stage, and can't get back (because it falls down below the ground).

How do I stop this from happening? How can I "lock" them in place while visible? I read that the sprites are not being rendered when visible=false, so should I be using some other method of hiding them? 

 

Here is my relevant code for the keyboard event:

skey = this.game.input.keyboard.addKey(83);
		skey.onDown.add(this.sPressed,this);

Here is the method sPressed:

sPressed: function(key) {
		if (baddie.visible) baddie.visible = false;
		else baddie.visible = true;

		if (stars.visible) stars.visible = false;
		else stars.visible = true;
	}

And here is how I make the camera follow the character sprite:

this.game.camera.x = player.body.x-200;

I tried changing to camera.follow, and camera.focusOnXY, and that changed nothing.

 

I have tried googling, and can't find anything relevant to my problem.

Link to comment
Share on other sites

In case anyone has a similar problem, I solved this:

Using game.world.bringToTop and sendToBack works great instead of changing the visibility. By hiding the sprites behind the background, they appear hidden but are still rendered in the same place relative to the game world. Then I just have a global boolean variable which checks whether I have toggled the visibility on or off, and only do collision if the sprites are visible to the viewer :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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