In my simple JavaScript/HTML 5 game I have what I believe is an update error that I don't know how to fix. The thing is there is no real error, its just not responding to the code as how I wanted it to. The problem is, I want a on screen GUI that displays the number of enemies killed on the map. So far I have just that with the GUI there. Here is a picture of the game:
https://i.sstatic.net/0rx4d.png [This shows 3 enemies on the screen, the GUI in the top right corner, and the player.]
Now the problem is whenever I kill an enemy the number should raise, but of course it doesn't. Here is my code:
window.addEventListener("load",function() {
var EKL1 = 0; // Enemies Killed Level 1
var Q = window.Q = Quintus()
.include("Sprites, Scenes, Input, 2D, Anim, Touch, UI, TMX")
.setup({ maximize: true })
.controls().touch()
Q.Sprite.extend("Player",{
init: function(p){
this._super(p, {
sprite: "player",
sheet: "player",
speed: 150,
x: 410,
y: 90
});
this.add('2d, platformerControls, animation');
},
step: function(dt){
if(this.p.vx > 0){
this.play("walk_right");
this.p.direction = "right";
} else if(this.p.vx < 0){
this.play("walk_left");
this.p.direction = "left";
} else {
this.play("stand_" + this.p.direction);// + this.p.direction > 0 ? "right" : "left");
}
}
});
Q.Sprite.extend("Enemy",{
init: function(p) {
this._super(p, {
sprite: "enemy",
sheet: "enemy",
vx: 100
});
// Enemies use the Bounce AI to change direction
// whenver they run into something.
this.add('2d, aiBounce');
// Listen for a sprite collision, if it's the player,
// end the game unless the enemy is hit on top
this.on("bump.left,bump.right,bump.bottom",function(collision) {
if(collision.obj.isA("Player")) {
//Q.stageScene("endGame",1, { label: "You Died" });
collision.obj.destroy();
}
});
// If the enemy gets hit on the top, destroy it
// and give the user a "hop"
this.on("bump.top",function(collision) {
if(collision.obj.isA("Player")) {
this.destroy();
collision.obj.p.vy = -300;
//Increasing Enemies killed on destroy
EKL1++;
}
});
}
});
Q.scene("UIContainers", function(stage){
var container = stage.insert(new Q.UI.Container({
fill: "gray",
border: 2,
shadow: 3,
shadowColor: "rgba(0,0,0,0.5)",
y: Q.height/10,
x: Q.width/1.25
}));
stage.insert(new Q.UI.Text({
label: "Enemies killed: " + EKL1.toString(),
color: "black",
x: 0,
y: 0
}),container);
container.fit(20,20);
});
Q.scene("level1", function(stage){
stage.insert(new Q.Repeater({ asset: "background-wall.png", speedX: 0.5, speedY: 0.5 }));
Q.stageScene("UIContainers", 1);
stage.collisionLayer(new Q.TileLayer({ dataAsset: 'level.json', sheet: 'tiles' }));
var player = stage.insert(new Q.Player());
stage.insert(new Q.Enemy({ x: 700, y: 0 }));
stage.insert(new Q.Enemy({ x: 800, y: 0 }));
stage.insert(new Q.Enemy({ x: 500, y: 0 }));
stage.add("viewport").follow(player);
});
Q.load("sprites.png, sprites.json, enemy.png, enemy.json, level.json, tiles.png, background-wall.png", function(){
Q.sheet("tiles","tiles.png", { tilew: 32, tileh: 32 });
Q.compileSheets("sprites.png", "sprites.json");
Q.compileSheets("enemy.png", "enemy.json");
Q.animations("player", {
walk_right: {frames: [0, 1, 2, 3], rate: 1 / 4, flip: false, loop: true },
walk_left: {frames: [0, 1, 2, 3], rate: 1 / 4, flip:"x", loop: true },
stand_right: {frames: [0], rate: 1 / 4, flip: false, loop: true},
stand_left: {frames: [0], rate: 1 / 4, flip: "x", loop: true}
});
Q.stageScene("level1");
});
});
The code is of course written in JavaScript but it is run by the Quintus Gaming Engine which can be found here: http://html5quintus.com/
This is how I went about trying to get this to work, so I made a global variable called EKL1 (Enemies killed level 1) which returned/equaled 0. This global variable was accessed in the Enemy function at the bottom where the player to enemy collision takes place. It says if player hits top of enemy, enemy is destroyed. So what I did was I added this: EKL1++ inside the if statement on destroying the enemy. I converted this to a string and ran it to the console and it worked! I killed a enemy and it said 1, killed another then 2, and finally I killed the last one and it displayed 3. Now of course I had to implement this into the GUI on the screen. So what I did was I made a UIContainer function that would hold all my GUI's. I inserted a label onto the container as you can see then put the label text as:
"Enemies killed: " + EKL1.toString();
This converted without the need of a string variable, the integer to a string. I thought this would work, but when I killed a monster it didn't rise. I believe the reason for this is the container is drawn on the screen once, and so it doesn't update. Which means when I kill a monster it won't update the container. I don't know how to implement an update function or something to get this working properly.
If anyone knows the answer to fixing this please respond! I hope this isn't a problem that is to hard to fix.
Thanks for your time,
~Jackson Vandy