Checkclick 함수에서 유형 수가 초과되면 레일 유형을 0으로 되돌리려하지만 레일 유형의 수를 초과합니다. 직선 레일과 마찬가지로 수직 형 제로 타입과 수평 형 제로 타입이 있습니다. 곡선 레일에는 4 가지 유형이 있습니다. 레일을 클릭하면 유형이 증가하여 렌더 파트에서 변수 t 값이 변경되고 레일 모양이 변경됩니다.자바 스크립트 배열 값
//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 is the problem
function checkClick (mouseEvent) {
for (i = 0; i < 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 that makes a rail
function s_rail() {
//straight rails
this.x = column * 32;
this.y = row * 32;
this.type = s_type();
// type 0 up down type 1 left right
}
function c_rail() {
//curved rails
this.x = column * 32;
this.y = row * 32;
this.type = c_type();
//type 0 right down type 1 left down type 2 up right type 3 up left
}
//draw the 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) {
return 0;
}
else {
return 1;
}
}
function c_type() {
if (Math.random() <= 0.25) {
return 0;
}
else if (Math.random() > 0.25 && Math.random() <= 0.5) {
return 1;
}
else if (Math.random() > 0.5 && Math.random() <= 0.75) {
return 2;
}
else {
return 3;
}
}
//give the rails its position and 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() {
for (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 the rails fail
for (a = 0; a < 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();
가 나는 부분을 좋아했다 이봐, 내 코드는 여기에 내 질문을 알아낼거야. 이것은 제퍼디가 아닙니다. – OozeMeister
문제를 나타내는 바이올린을 제공하십시오. – Fabricator