Loop advise needed (Long snippet alert)

Hi all,

Im working on a learning game for my kids using Phaser. Ive made the game with timetables and a rocket going up. All seems to be working fine, but only once :slight_smile:. The cut of the snippet below makes the background, rocket, randomly generated timetables and allows you to answer the question and move the rocket a bit up. But I want them to able to keep moving up until it`s at the top.

create() {

    const width = this.scale.width;
    const height = this.scale.height;

    let bg = this.image = this.add.image(width * .5, height * .5, 'back_table');
    bg.displayHeight = game.config.height;

    let board = this.add.image(width * .6, height * .2, 'board_tbl_bt');
    board.setScale(bg.scaleX);

    this.rocket = this.add.image(width * .2, height * .75, 'rocket');
    this.rocket.setScale(bg.scaleX);

    this.makeButton(button, .1, .1, 'math_tbl_bt', "Table", bg.scaleX, 0);

    this.nameInput = this.add.dom(width * .6, height * .6).createFromCache("form");

    //Start Loop here
    
    const multiNum = Phaser.Math.Between(1, 12);
    console.log(multiNum);

    this.message = this.add.text(width * .45, height * .4, this.choice + " multiply " + multiNum, {
        color: "#FFFFFF",
        fontSize: 60,
        fontStyle: "bold"
    });

    const result = this.choice * multiNum;
    console.log(result);

    this.returnKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.ENTER);

    this.returnKey.on("down", event => {
    const answer = this.nameInput.getChildByName("answer");
    const value = answer.value;

        if (value == result) {

            this.say = this.add.text(width * .45, height * .7, answer.value + " is correct!", {
                color: "#FFFFFF",
                fontSize: 60,
                fontStyle: "bold"
            });

            if (this.rocket.y == height *.25) {
                this.scene.start("Winner", {
                    "nameSC": "Table"
                });
            }
            else if (this.rocket.y > height * .25) {
                this.rocket.y -= height * .05;
                this.time.addEvent({
                    delay: 2000,
                    callback: ()=>{
                        this.say.destroy();
                        this.message.destroy();
                    },
                    loop: false
                });
            }
            else {
                this.time.addEvent({
                    delay: 1000,
                    callback: ()=>{
                        this.say.destroy();
                        this.message.destroy();
                    },
                    loop: false
                });
            }
        }

        else {
            this.say = this.add.text(width * .45, height * .7, answer.value + " is not correct!", {
                color: "#FFFFFF",
                fontSize: 60,
                fontStyle: "bold"
            });

            if (this.rocket.y < height * .75) {
                this.rocket.y += height * .05;
                this.time.addEvent({
                    delay: 2000,
                    callback: ()=>{
                        this.say.destroy();
                        this.message.destroy();
                    },
                    loop: false
                });
            }
            else {
                this.time.addEvent({
                    delay: 1000,
                    callback: ()=>{
                        this.say.destroy();
                        this.message.destroy();
                    },
                    loop: false
                });
            }
        }
    });
}

//Finish loop here

Any advice is very welcome. I`ve tried a few options, like create the method and calling it, but it causes havoc. Tried addEvent and placing the snippet in the update section. It just goes none stop))

Oh, forgot to mention, I`m new to Phaser and probably missing something simple.

Your ‘loop’ is the returnKey “down” event. So make sure everything you want inside the loop, is inside that callback. So recreate message after you destroy it, and generate a new multiNum and result. The addKey should not be in a loop, once is enough.

1 Like

Oh, I see, that makes sense. Im an old school (Pascal and Delphi) programmer and was looking for the "goto label" option. Let me try it later today. Kids already cant wait to play “daddy’s” game :slight_smile:

Goto is like an assembly JMP. I guess we’re elite programmers :slight_smile:
I did Pascal and Delphi too. My father wrote a chess program in Pascal, I was rather fascinated…

1 Like

Spot on mate. It works now. I actually had another issue with multiNum which was inside addEvent and wasn`t working properly. Basically, I generate this.multiNum straight after I saved the variable result.