Repository navigation
Release 0.2.5: Movement bug fix
Pre-release
Pre-release
Bug fixes
- Fixed a bug where the player could rotate 180 degrees in the same column by swiping:
- For example, previously, if the snake was heading right and the player swiped down then immediately up, the snake would erroneously change direction to down and then up.
Technicalities
if (this.canChangeDirection && this.direction !== direction && this.direction !== Math.abs(180 - direction)) {
- The issue stemmed from incorrect direction comparison logic:
- Originally, I compared the current direction (
this.direction, which wasDOWNin the above example) against the opposite of the current direction (UP):DOWN !== UP. This comparison always returned true, allowing illegal movements. - Instead of the correct approach of checking the requested direction (
UP) with the opposite of the current direction (opposite ofDOWNisUP):UP !== UPisfalse).
- Originally, I compared the current direction (
- Furthermore,
Math.abs(180 - direction)didn't consistently return the expected result.- For instance, if the current direction (
this.direction) is 90 (right), the expected result is 270 (left) (the opposite of the current direction, right), but the calculation would return 90 instead. - Now, the result is determined via a separate function to guarantee a correct result:
- For instance, if the current direction (
function oppositeDegrees(degrees) {
if (degrees === 0) {
return 180;
} else if (degrees === 90) {
return 270;
} else if (degrees === 180) {
return 0;
} else if (degrees === 270) {
return 90;
}
}
What's Changed
- Bug can change direction 180 degree immediately after directin change by @IceTheCoder in #17
Full Changelog: 0.2.4...0.2.5
30 June 2024