javascript - Else If statement never runs -
so i'm trying implement platforms game i'm making project (similar falldown) , have created multiple arrays have contain possible platforms (canvas 360 there if platform[i] == 1 draws rect)
var canvas; var ctx; var isplaying = false; window.onload = function(){ canvas= document.getelementbyid("gamescanvas"); ctx = canvas.getcontext("2d"); var fps = 60; setinterval(function(){ }, 1000/fps); createmenu(); canvas.addeventlistener('click', getclicks.bind(this), false) //canvas.addeventlistener("mousemove", getpos) } function initialise(){ isplaying = true; ctx.clearrect(0, 0, canvas.width, canvas.height); createrect(0,0, canvas.width, canvas.height, 'black'); createplatforms(); } function createplatforms(){ x = randint(1,2); console.log(x) var i; var pos = -60; var platform1 = [0,1,1,1,1,1]; var platform2 = [1,0,1,1,1,1]; var platform3 = [1,1,0,1,1,1]; var platform4 = [1,1,1,0,1,1]; var platform5 = [1,1,1,1,0,1]; var platform6 = [1,1,1,1,1,0]; if(x==1){ (i=0; i<platform1.length; ++i) { var pos = (pos+60); if(platform1[i] == 1){ createrect(pos, 60, 60,5, 'white'); } } } else if(x==2){ (i=0; i<platform2.length; ++i){ var pos = (pos+60); if (platform2[i] ==2){ createrect(pos,60,75,5,'white'); } } } } function randint(min, max) { return ~~(math.random() * (max - min + 1) + min); } function background(color) { ctx.fillstyle = color; ctx.fillrect(0, 0, canvas.width, canvas.height); } function createmenu(){ background("black"); if (!isplaying) { ctx.font = "60px monospace"; ctx.fillstyle = "white"; ctx.filltext("falldown", 40, 130); ctx.font = "34px arial"; ctx.fillstyle = "white"; ctx.filltext("play", 130, 260); ctx.font = "34px arial"; ctx.fillstyle = "white"; ctx.filltext("leaderboard", 50, 340); ctx.font = "34px arial"; ctx.fillstyle = "white"; ctx.filltext("settings", 90, 420); } } function createrect(leftx, topy, width, height, color){ ctx.fillstyle = color; ctx.fillrect(leftx, topy, width, height); } function getclicks(evt) { var x = evt.offsetx; var y = evt.offsety; if ((x > 110 && x < 240) && (y > 220 && y < 275) && !isplaying) { initialise() } } <html> <head> <title>falldown</title> </head> <body> <canvas id="gamescanvas" width="360" height="640"></canvas> <!--script src="test.js"></script--> </body> </html> however, if x>1 (so else if statement required run) doesn't draw anything.
i testing see whether fix, however, managed realise if if statement has got contents of else if statement draw rects in right position in case (platform2) drawn.
i've managed narrow down problem i'm not sure how fix it. have experience python have never experienced this
just letting know can't use else statement have implement 6 platforms , if use if , else mean draw 2 of 6 platforms
your initial problem wasn't if / else.. if inside..
but -> if (platform2[i] ==2){ wanted if (platform2[i] == 1){
but saying this, createplatforms creating single platform. didn't need if/else or arrays.
below i've modified createplatforms, using 2 loops.
var canvas; var ctx; var isplaying = false; window.onload = function(){ canvas= document.getelementbyid("gamescanvas"); ctx = canvas.getcontext("2d"); var fps = 60; setinterval(function(){ }, 1000/fps); createmenu(); canvas.addeventlistener('click', getclicks.bind(this), false) //canvas.addeventlistener("mousemove", getpos) } function initialise(){ isplaying = true; ctx.clearrect(0, 0, canvas.width, canvas.height); createrect(0,0, canvas.width, canvas.height, 'black'); createplatforms(); } function createplatforms(){ (var y = 0; y < 8; y ++) { var x = randint(0, 5), pos = 0; (var = 0; < 6; ++) { if (i !== x) { createrect(pos, 60 + y*60 ,75,5,'white'); } pos += 60; } } } function randint(min, max) { return ~~(math.random() * (max - min + 1) + min); } function background(color) { ctx.fillstyle = color; ctx.fillrect(0, 0, canvas.width, canvas.height); } function createmenu(){ background("black"); if (!isplaying) { ctx.font = "60px monospace"; ctx.fillstyle = "white"; ctx.filltext("falldown", 40, 130); ctx.font = "34px arial"; ctx.fillstyle = "white"; ctx.filltext("play", 130, 260); ctx.font = "34px arial"; ctx.fillstyle = "white"; ctx.filltext("leaderboard", 50, 340); ctx.font = "34px arial"; ctx.fillstyle = "white"; ctx.filltext("settings", 90, 420); } } function createrect(leftx, topy, width, height, color){ ctx.fillstyle = color; ctx.fillrect(leftx, topy, width, height); } function getclicks(evt) { var x = evt.offsetx; var y = evt.offsety; if ((x > 110 && x < 240) && (y > 220 && y < 275) && !isplaying) { initialise() } } <html> <head> <title>falldown</title> </head> <body> <canvas id="gamescanvas" width="360" height="640"></canvas> <!--script src="test.js"></script--> </body> </html>
Comments
Post a Comment