Jump to content

Show/Hide Sprites & Texts


azdev
 Share

Recommended Posts

You can use game.add() to create new objects like:

game.add.sprite(0,0,'mySprite');

More info here:

http://docs.phaser.io/Phaser.GameObjectFactory.html

 

If you just want to make an object invisible you can change it's alpha property like this:

mySprite.alpha = 0;

or you can change it's visible property to false like this:

mySprite.visible = false;

You can find more info on sprites on the same page that I linked before. Good Luck!

Link to comment
Share on other sites

  • 1 year later...

Hello,
and text? Alpha or visible properties are not working for me with text.

            Create method:            
            this.text = this.game.add.bitmapText(540, 60, 'gosmick','Hello Word', 112); //It's fine. Text shows.            
../..
            Update method: 
            this.text.alpha = 0; //It's not working
            this.text.visible = false; //It's not working

And the text still shows in screen. ¿? ¿? ¿?

Thanks for your help.

Link to comment
Share on other sites

OMG. The mistery has a solution. ¡It was my fault! The scope of variables were in bad place. :-<

Anyway, the behavior is strange because the "sprite variable" was in the same place "text variable", and sprite was working and text not.

https://phaser.io/examples/v2/text/remove-text

Sorry, I'm newbie. I love this framework. It's absolutely great!!
Thanks you.

Link to comment
Share on other sites

Hi, guys. another newbie here.

I have a key listener in my update loop that listens for key to show and hide a menu.

Problem is, the toggle isnt a hard toggle but a fully automatic toggle. The menu flashes if you hold the bound key down.

How can I keep the listener in the update loop but make it a hard toggle without the full auto flash effect? Thank you!

Link to comment
Share on other sites

On 11/2/2016 at 4:08 PM, charlieRobinson said:

Hi, guys. another newbie here.

I have a key listener in my update loop that listens for key to show and hide a menu.

Problem is, the toggle isnt a hard toggle but a fully automatic toggle. The menu flashes if you hold the bound key down.

How can I keep the listener in the update loop but make it a hard toggle without the full auto flash effect? Thank you!

Don't put the listener in the update loop. That's just not how to do it. 

Simply enable input on the sprite (with sprite.inputEnabled = true) and add an event listener on the sprite with:

sprite.events.onInputDown.add(function() {

    if (menuSprite.visible) {

        menuSprite.visible = false; 

    } else {

        menuSprite.visible; 

    }

}); 

It doesn't have to be more complicated than that :). 

 

Link to comment
Share on other sites

Thank you. If I wanted to bind the 'E' key to that sprite listener, how we do I do that? 

I am currently using: E = this.game.input.keyboard.isDown(Phaser.Keyboard.E);

But I am not sure if that code will work with your example? Thank you!

Also, the tutorials I am finding show binding the key to the variable and then detecting it in the update loop. I am not sure how to create a listener for a key that is outside the update loop. Thank you!

http://phaser.io/examples/v2/input/key

 

 

EDIT:::: I think I am on to something with the example code below

 key1 = game.input.keyboard.addKey(Phaser.Keyboard.ONE);
    key1.onDown.add(addPhaserDude, this);

function addPhaserDude () {
    game.add.sprite(game.world.randomX, game.world.randomY, 'phaser');
}

Link to comment
Share on other sites

ok... new problem... so I have the function bound key press working. Thank you!

Problem now is that I can no longer use the 'E' key while chatting.

the menu toggle is contingent on if (!isChatting) so if player is not chatting they cant toggle the menu so as to not interfere with using the 'E' key while chatting. But now the 'E' key seems bound only to the menu toggle and wont appear in the text field to chat.

Do I need to toggle the key listener off and on when the player (isChatting) boolean is true? 

All of the other keys being detected in the update loop are working fine in chat, BTW. Thank you!

EDIT:::::
Looks like this is the code I am looking for: game.input.keyboard.removeKey(Phaser.Keyboard.E);

The idea now is to add and remove 'E' key when player is filling out text boxes. Any better way to do this? Thank you!

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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