Javascript Array value -
Javascript Array value -
at checkclick function trying create rail type go 0 when exceed number of type, goes above number of types of rail. straight rail there 2 types: type 0 vertical , type 1 horizontal. curved rail there 4 types. when click rail, type increment , alter variable t value in render part , alter rail look.
//create canvas var canvas = document.createelement("canvas"); var ctx = canvas.getcontext("2d"); canvas.width = 1024; canvas.height = 640; document.body.appendchild(canvas); function init () { setinterval(main(), 100); } var mouse_x; var mouse_y; canvas.addeventlistener("mousemove", checkpos); canvas.addeventlistener("mouseup", checkclick); function checkpos (mouseevent) { if (mouseevent.pagex || mouseevent.pagey == 0) { mouse_x = mouseevent.pagex - this.offsetleft; mouse_y = mouseevent.pagey - this.offsettop; } else if (mouseevent.offsetx || mouseevent.offsety == 0) { mouse_x = mouseevent.offsetx; mouse_y = mouseevent.offsety; } } //this part problem function checkclick (mouseevent) { (i = 0; < all_rails.length; i++) { if (mouse_x < all_rails[i].x + 32 && mouse_x > all_rails[i].x && mouse_y > all_rails[i].y && mouse_y < all_rails[i].y + 32) { if (all_rails[i] !== 0) { all_rails[i].type++; if (all_rails[i] == 1 && all_rails[i].type > 1) { all_rails[i].type = 0; } else if (all_rails[i] == 2 && all_rails[i].type > 3) { all_rails[i].type = 0; } } } } } var bgimage = new image(); bgimage.src = "image/bg.png"; var stationimage = new image(); stationimage.src = "image/station.png" var s_r = new array(); s_r[0] = new image(); s_r[1] = new image(); s_r[0].src = "image/s_rail0.png"; s_r[1].src = "image/s_rail1.png"; var c_r = new array(); c_r[0] = new image(); c_r[1] = new image(); c_r[2] = new image(); c_r[3] = new image(); c_r[0].src = "image/c_rail0.png"; c_r[1].src = "image/c_rail1.png"; c_r[2].src = "image/c_rail2.png"; c_r[3].src = "image/c_rail3.png"; //start of rail //function makes rail function s_rail () { //straight rails this.x = column * 32; this.y = row * 32; this.type = s_type(); // type 0 downwards type 1 left right } function c_rail () { //curved rails this.x = column * 32; this.y = row * 32; this.type = c_type(); //type 0 right downwards type 1 left downwards type 2 right type 3 left } //draw rails s_rail.prototype.draw = function () { var t = this.type; ctx.drawimage(s_r[t], this.x, this.y); } c_rail.prototype.draw = function () { var t = this.type; ctx.drawimage(c_r[t], this.x, this.y); } //type randomize function s_type () { if (math.random() < 0.5) { homecoming 0; } else { homecoming 1; } } function c_type () { if (math.random() <= 0.25) { homecoming 0; } else if (math.random() > 0.25 && math.random() <= 0.5) { homecoming 1; } else if (math.random() > 0.5 && math.random() <= 0.75) { homecoming 2; } else { homecoming 3; } } //give rails position , type var all_rails = [0, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1,]; var n = 0; //the index of rails var row = 0; var column; function make_rail () { (n; n < 640; n++) { column = n % 32; if (column == 0 && n !== 0) { row++; } if (all_rails[n] == 1) { all_rails[n] = new s_rail(); } else if (all_rails[n] == 2) { all_rails[n] = new c_rail(); } } } //end of rails function render () { ctx.drawimage(bgimage, 0, 0); ctx.drawimage(stationimage, 0, 0); ctx.drawimage(stationimage, 0, 576); //render rails fail (a = 0; < all_rails.length; a++) { if (all_rails[a] !== 0) { all_rails[a].draw(); } } } function main () { make_rail(); render(); requestanimationframe(main); } var w = window; requestanimationframe = w.requestanimationframe || w.webkitrequestanimationframe || w.msrequestanimationframe || w.mozrequestanimationframe; init();
i think explanation hard understand. comparing object number after incremented type, conditions won't met
function checkclick (mouseevent) { (i = 0; < all_rails.length; i++) { if (mouse_x < all_rails[i].x + 32 && mouse_x > all_rails[i].x && mouse_y > all_rails[i].y && mouse_y < all_rails[i].y + 32) { if (all_rails[i] !== 0) { /* happens, all_rails[i] object, not number 0 */ all_rails[i].type++; // increment if (all_rails[i] == 1 && all_rails[i].type > 1) { /* never happens, all_rails[i] object, not value 1 */ all_rails[i].type = 0; // never resets } else if (all_rails[i] == 2 && all_rails[i].type > 3) { /* never happens, all_rails[i] object, not value 2 */ all_rails[i].type = 0; // never resets } } } } } you declared all_rails as:
var all_rails = [0, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1, 0, 1, 2, 0, 1, 2, 0, 1,]; but in make_rail assign object s_rail or object c_rail items, overriding initial numerical value. available fixes add together custom valueof c_rail , s_rail objects evaluates initial all_rails[n] value (not recommended) or add together new property holds initial numerical value , pass argument c_rail , s_rail constructors, prepare checkclick function compare property of all_rails[i] rather object itself.
(i recommend in javascript declare first before execute actual code. utilize capitalize first letter of constructor , utilize camel case)
javascript arrays game-physics
Comments
Post a Comment