Hi all,
Im 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.