Answer the question
In order to leave comments, you need to log in
Why doesn't the value want to "equal"?
I decided to get confused with writing two separate movement functions, where one "processes" the received data, and the other calculates them.
Algorithm: activating a certain "move trigger" requires a state "work"(array [0]) and a key(of true and false)(array [1-2]) .
It is possible to do this with if, but the maximum optimization from if is needed.
Perhaps the problem is that instead of activating a specific trigger, it activates everything at once, and nothing works.
I'll throw the whole script to understand the essence. Attention should be paid to the uptade function (With the comment "calculation of physics and data").
Here is the whole script:
let canvas = document.getElementById('canvas');
let ctx = canvas.getContext('2d');
canvas.width = 450;
canvas.height = 450;
document.addEventListener('keydown', function (e) {keydown_about[0] = e.keyCode; keydowner = true} );
document.addEventListener('keyup', function (e) {keydown_about[0] = e.keyCode; keydowner = false;} );
let world = [
{x:10,y:30,t:50,c:'red'},
{x:50,y:100,t:50,c:'green'},
];
let x,y,t,c;
let keydowner = false;
let keydown_about = [,['',false,true],['',true,true],['',false,false],['',true,false]];
function get() {
//Обработка информации от пользователя
if (keydowner) {
switch(keydown_about[0]) {
case 87: //W
keydown_about[1] = ["work", false, true];//Сообщение, не по x(y), инверсия(--)
break;
case 65: //A
keydown_about[2] = ["work", true, true];//Сообщение, по x, инверсия(--)
break;
case 83: //S
keydown_about[3] = ["work", false, false];//Сообщение, не по x(y), не инверсия(++)
break;
case 68: //D
keydown_about[4] = ["work", true, false];//Сообщение, по x(y), не инверсия(++)
break;
}
} else {
switch(keydown_about[0]) {
case 87: //W
keydown_about[1] = ["", false, true];
break;
case 65: //A
keydown_about[2] = ["", true, true];
break;
case 83: //S
keydown_about[3] = ["", false, false];
break;
case 68: //D
keydown_about[4] = ["", true, false];
break;
}
}
}
function update() {
//Вычисление данных, физика
for(let i=1;i<keydown_about.length;i++) {
switch(true) {
case ((keydown_about[i][0] === "work")&!keydown_about[i][1]&keydown_about[i][2]): //x--
world[0].x-=1;
break;
case ((keydown_about[i][0] === "work")&keydown_about[i][1]&keydown_about[i][2]): //x++
world[0].x+=1;
break;
case ((keydown_about[i][0] === "work")&!keydown_about[i][1]&!keydown_about[i][2]): //y--
world[0].y-=1;
break;
case ((keydown_about[i][0] === "work")&keydown_about[i][1]&!keydown_about[i][2]): //y++
world[0].y+=1;
break;
}
}
}
function clear() {
//Рендеринг, обновление холста
ctx.fillStyle = "gray";
ctx.fillRect(0,0,canvas.width,canvas.height)
for(let i=0;i<world.length;i++) {
x = world[i].x;
y = world[i].y;
t = world[i].t;
c = world[i].c;
ctx.fillStyle=c;
ctx.fillRect(x,y,t,t);
}
}
function AllTogether () {
get();
update();
clear();
requestAnimationFrame(AllTogether);
}
AllTogether();
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question