Jump to content

Temporarily Hiding Buttons


Recommended Posts

Hi I am currently working on a game using phaser but having problems where I want to display a button that you click once to start charging a power bar and then click again to set the power but once you do this the button should hide until the item being launched comes to rest for you to then repeat the process as needed.


I have tried using button.visible = false, but nothing appears to change and outputting the value to the console shows it is false.


Thanks in advance for any help :)

Link to comment
Share on other sites

The button is created here:


 SwingB = this.game.add.button(1400, 830, "SwingButton", this.Swing, this, 0, 0, 0, 0);


The property is changed here:


Swing: function() {

        if (Started == true){

            //this.game.camera.follow(Ball, Phaser.Camera.FOLLOW_TOPDOWN);

            var VelocityX = (Power * Math.cos((Arrow.angle -90) * Radian) * 10);

            var VelocityY = (Power * Math.sin((Arrow.angle -90) * Radian) * 10);

            Ball.body.velocity.x += VelocityX;

            Ball.body.velocity.y += VelocityY;

            Started = false;

            this.PowerF.visible = false;

            this.PowerB.visible = false;

            this.SwingB.visible = false;



        if (Started == false) {

            this.PowerB = this.game.add.sprite(1400, 830, "PowerBar");

            this.PowerF = this.game.add.sprite(1649, 1080, "PowerFill");

            this.PowerF.anchor.setTo(0.5, 1);

            this.PowerB.fixedToCamera = true;

            this.PowerF.fixedToCamera = true;

            this.PowerF.rotation = 181 * Radian;

            Power = 0;

            Started = true;



        Block.body.onBeginContact.add(this.StartEmitter, this);



PowerF and PowerB both disappear fine though they are sprites if that makes a difference.

Link to comment
Share on other sites

A Button is just a sprite too, so I don't think it's that. You should use a strict equality check for booleans: if (Started === true) and === false, otherwise I believe the issue is elsewhere - perhaps Swing is being called multiple times (log it out and see), once hiding it and once showing it. Perhaps the button visibility is being reset somewhere else. Maybe the button you're seeing on-screen isn't the button you thought was hidden at all (i.e. it spawned a new one in its place). There are all kinds of things it could be I'm afraid.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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