Answer the question
In order to leave comments, you need to log in
Yandex Workshop JavaScript. How to fix the bug with the movement of the logo?
Good afternoon.
It is necessary to implement the moving Yandex.Practice logo with arrows.
I have already implemented everything needed for the state.
My solution is to cut the logo into 25px squares. When you click one of the arrow buttons, the squares fall in a 25px path in the corresponding direction. Or, if some of the squares get coordinates that are out of bounds corresponding to the pressed arrow button, those squares have coordinates equal to "opposite side minus 25 pixels" (their coordinates are calculated at the top left point). During testing, I get an error when the logo goes beyond one of the borders. The logo does not match the size or content. What can cause it? I guess it's because some of the parameters from the conditions above are not a multiple of 25. But I have no idea how to account for this in my code.
Below is everything I did.
https://jsfiddle.net/3bz1rft8/5/
Conditions for the logo:
Answer the question
In order to leave comments, you need to log in
I rewrote the code a little and the problem was solved:
const updateSquares = (axis, step, max) => {
clearRect();
const minAxis = `min${axis.toUpperCase()}`;
const maxValue = max - 12;
squares.forEach((square) => {
square[minAxis] = (maxValue + square[minAxis] + step) % maxValue;
});
updateRect(squares);
}
function moveRect(event) {
switch (event.keyCode) {
// left
case 37:
updateSquares('x', -25, WIDTH);
break
// up
case 38:
updateSquares('y', -25, HEIGHT);
break
// right
case 39:
updateSquares('x', 25, WIDTH);
break
// down
case 40:
updateSquares('y', 25, HEIGHT);
break
default:
break
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question