Akorna
Akorna

Reputation: 217

Canvas not creating text

So basicly I got this little bit of code that checks the end of the game, it should display victory when you killed all the enemies and GameOver when you didn't. Thing is, with this bit of code, the "victory" happens, but when defeated he doesn't show anything (the debugger shows the string is correct tho)

playing=false;
gameOver=true;

var resultText="You Lost!";

if(enemies.length==0){
    Debugger.log("VICTORY!");
    resultText="VICTORY!";
}

context.font = "70px Comic Sans MS";
context.fillStyle = grd;
context.textAlign = "center";
Debugger.log("ResultText: "+ resultText);
context.fillText(resultText, canvas.width/2, canvas.height/2);

Upvotes: 0

Views: 60

Answers (1)

JTerry
JTerry

Reputation: 101

The code you've provided appears correct. Do the declarations for the variables look something like this. This was what I added to get it to work.

var cxt = document.getElementById('canvasMain');
var context = cxt.getContext('2d')
var grd = "blue";
var enemies = ["schoolBully", "busDriver"];

And have you tested to make sure length actually hits 0?

Upvotes: 1

Related Questions