Search code examples
integerphaser-frameworkadditionphaserjs

Phaser Number won't increase where I think it should


I have something of a minigame within a scene, that game is working fine, but I'm having trouble telling the rest of the code that its complete.

if (this.mini == true){
        this.fixed = 4;
        if (this.houseRepair == true){
            this.fixed = this.fixed - 1;
        }
        if (this.plumbing == true){
            this.fixed = this.fixed - 1;
        }
        if (this.electricity == true){
            this.fixed = this.fixed - 1;
        }
        if (this.repair == true){
            this.fixed = this.fixed - 1;
        }
        this.finished = 0;
        if(Phaser.Input.Keyboard.JustDown(this.keyA)) {

            this.hammerCount = this.hammerCount+1;
            if(this.hammerCount == 1 || this.hammerCount == 3 || this.hammerCount == 5 || this.hammerCount == 7){
             this.hammer.setTexture('hammerGame2');
            }
            else if(this.hammerCount == 0 || this.hammerCount == 2 || this.hammerCount == 4 || this.hammerCount == 6){
                this.hammer.setTexture('hammerGame1');
               }
            if (this.hammerCount == 8){
             this.hammer.destroy();
             this.finished = this.finished + 1;
            }
 
        }
        if(Phaser.Input.Keyboard.JustDown(this.keyS)) {

            this.plumbCount = this.plumbCount+1;
            if(this.plumbCount == 1 || this.plumbCount == 3 || this.plumbCount == 5 || this.plumbCount == 7){
             this.plumb.setTexture('plumbingGame2');
            }
            else if(this.plumbCount == 0 || this.plumbCount == 2 || this.plumbCount == 4 || this.plumbCount == 6){
                this.plumb.setTexture('plumbingGame1');
               }
            if (this.plumbCount == 8){
             this.plumb.destroy();
             this.finished = this.finished + 1;
            }
            
        }

        if(Phaser.Input.Keyboard.JustDown(this.keyD)) {

            this.wireCount = this.wireCount+1;
            if(this.wireCount == 1 || this.wireCount == 3 || this.wireCount == 5 || this.wireCount == 7){
             this.wire.setTexture('wireGame2');
            }
            else if(this.wireCount == 0 || this.wireCount == 2 || this.wireCount == 4 || this.wireCount == 6){
                this.wire.setTexture('wireGame1');
               }
            if (this.wireCount == 8){
             this.wire.destroy();
             this.finished = this.finished + 1;
            }
 
        }

        if(Phaser.Input.Keyboard.JustDown(this.keyF)) {

            this.sewingCount = this.sewingCount+1;
            if(this.sewingCount == 1 || this.sewingCount == 3 || this.sewingCount == 5 || this.sewingCount == 7){
             this.sewing.setTexture('sewingGame2');
            }
            else if(this.sewingCount == 0 || this.sewingCount == 2 || this.sewingCount == 4 || this.sewingCount == 6){
                this.sewing.setTexture('sewingGame1');
               }
            if (this.sewingCount == 8){
             this.sewing.destroy();
             this.finished = this.finished + 1;
            }
            
        }

        if(this.fixed == this.finished){
            this.mini = false;
        }
    }

The issue lies in the fixed and finished variables. for the minigame to be considered finished, they have to be equal. So, when a section of the minigame is completed, it triggers a line of code that reads:

this.finished = this.finished + 1;

This would eventually add up to what fixed equals, allowing the minigame to be finished.

The problem is that this line is apparently not being read when the code is executed. The line above it, which destroys an associated image, works fine, so why is the line that adds 1 to finished not being triggered? Examples of working code are welcome.

If it helps, I'm using Phaser 3 in VSCode.


Solution

  • The problem is, if I understand your code correct, is that you are resetting this.finished on each run.

    I think if you set the this.finished = 0; once at the beginning of the mini game (where ever you are setting this.mini = true;), the problem should be solved.

    Assuming this code is in the update - function, or being called from the update - function