# Loop advise needed (Long snippet alert)

**URL:** <https://phaser.discourse.group/t/loop-advise-needed-long-snippet-alert/9714>\
**Category:** Phaser 3\
**Created:** [June 2, 2021, 11:51pm UTC](https://phaser.discourse.group/t/loop-advise-needed-long-snippet-alert/9714 "2021-06-02T23:51:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxiom](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/maxiom/32/5250_2.png) [@maxiom](https://phaser.discourse.group/u/maxiom)\
**Post date:** [June 2, 2021, 11:51pm UTC](https://phaser.discourse.group/t/loop-advise-needed-long-snippet-alert/9714/1 "2021-06-02T23:51:57Z")

</div>

Hi all,

I`m working on a learning game for my kids using Phaser. I`ve made the game with timetables and a rocket going up. All seems to be working fine, but only once 🙂. 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.

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [June 3, 2021, 5:59am UTC](https://phaser.discourse.group/t/loop-advise-needed-long-snippet-alert/9714/2 "2021-06-03T05:59:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![maxiom](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/maxiom/32/5250_2.png) [@maxiom](https://phaser.discourse.group/u/maxiom)\
**Post date:** [June 3, 2021, 11:52am UTC](https://phaser.discourse.group/t/loop-advise-needed-long-snippet-alert/9714/3 "2021-06-03T11:52:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [June 3, 2021, 2:49pm UTC](https://phaser.discourse.group/t/loop-advise-needed-long-snippet-alert/9714/4 "2021-06-03T14:49:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![maxiom](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/maxiom/32/5250_2.png) [@maxiom](https://phaser.discourse.group/u/maxiom)\
**Post date:** [June 3, 2021, 8:20pm UTC](https://phaser.discourse.group/t/loop-advise-needed-long-snippet-alert/9714/5 "2021-06-03T20:20:48Z")

</div>

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.
