Discuss Scratch

Explosive_Chaos
Scratcher
6 posts

Diagonal and Changing Direction

I am making a game called Dodge. In this game, I want the player to be able to go diagonally. The problem is when I go in one direction and I try to go in a different direction, it doesn't work. Here is an example: I press the left and up arrow to go north west. I then press the right arrow to go right. Instead of going right it goes in north east. I know this isn't the best explanation. The easiest way for you to understand is for you try it yourself. Here is the link to the project page: http://scratch.mit.edu/projects/23879593/
Look inside and see for yourself. Thank you for any help
DanloVorje
Scratcher
100+ posts

Diagonal and Changing Direction

I believe the issue is that the x and y velocities are only reset to 0 when no keys are pressed. This means that if the player holds down up and left and then lets go of left, the x velocity is still 6 and the sprite continues to move diagonally. My suggestion to fix this:

Split this piece of script:
if <<not <key [left arrow v] pressed?>> and <<not <key [down arrow v] pressed?>> and <<not <key [up arrow v] pressed?>> and <not <key [right arrow v] pressed?>>>>> then
set [x velocity v] to [0]
set [y velocity v] to [0]
end

into these two:

if <not <<key [left arrow v] pressed?> or <key [right arrow v] pressed?>>> then
set [x velocity v] to [0]
end
if <not <<key [up arrow v] pressed?> or <key [down arrow v] pressed?>>> then
set [y velocity v] to [0]
end

That way the script sets the velocity to 0 if none of the keys that directly affect that velocity (rather than no keys whatsoever) are pressed.

Hope this helps!

–Danlo
RPFluffy
Scratcher
1000+ posts

Diagonal and Changing Direction

Works just fine, what's the problem?
Explosive_Chaos
Scratcher
6 posts

Diagonal and Changing Direction

RPFluffy wrote:

Works just fine, what's the problem?
I have to stop pressing all of the arrows if I want to change direction. I can't just change direction by pressing a different arrow (which we all do, especially in a fast moving game).

DanloVorje wrote:

I believe the issue is that the x and y velocities are only reset to 0 when no keys are pressed. This means that if the player holds down up and left and then lets go of left, the x velocity is still 6 and the sprite continues to move diagonally. My suggestion to fix this:

Split this piece of script:
if <<not <key [left arrow v] pressed?>> and <<not <key [down arrow v] pressed?>> and <<not <key [up arrow v] pressed?>> and <not <key [right arrow v] pressed?>>>>> then
set [x velocity v] to [0]
set [y velocity v] to [0]
end

into these two:

if <not <<key [left arrow v] pressed?> or <key [right arrow v] pressed?>>> then
set [x velocity v] to [0]
end
if <not <<key [up arrow v] pressed?> or <key [down arrow v] pressed?>>> then
set [y velocity v] to [0]
end

That way the script sets the velocity to 0 if none of the keys that directly affect that velocity (rather than no keys whatsoever) are pressed.

Hope this helps!

–Danlo
Thank you very much, it works wonders! I would never have guessed that was the problem!

Powered by DjangoBB