0

In short The sheep won't turn right. You can see the project here. (screenshot at the bottom)

https://scratch.mit.edu/projects/75999804/#editor

Longer Story

I went slightly beyond the scope of pset0 but really wanted to create a great foundation for a platformer (and a little nostalgia for this old sheep that ran around my desktop in my school days).

I got gravity, jumping and everything working perfectly. Since adding the detectors and altering my code (so that I can recognise collisions with the environment), turning right has broken.

For the life of me I cannot see what I've done wrong logically. Anyone else see?

Scratch Editor

1 Answer 1

0

I understand that you want to set the horizontal velocity to 3 only if you pressed the right arrow and you're not already at the right edge (BTW, where do you change touchingRight?). But when do you want to set it to 0? Only if the right arrow is pressed and you are at the edge, do I get that right? So you would have to use nested ifs, like

if key right arrow pressed
    if touchingRight = false
        set hor_velocity_sheep to 3
    else
        set hor_velocity_sheep to 0
else if key left arrow pressed
    if touchingLeft = false
        set hor_velocity_sheep to -3
    else
        set hor_velocity_sheep to 0
else
    set hor_velocity_sheep to hor_velocity_sheep*0.92
2
  • So actually, the problem was not with the collisions yet (which were actually triggered in another sprite at the moment). I figure out that the problem was actually due to my if/else loop to do with the keypresses. The else block for the left key not being pressed cancels the value the right key had set. if right key and variable=false set value to 3 else if left key and variable=false set value to -3 else set value to 0 Mar 8, 2017 at 17:06
  • Correct. One way to solve this problem would be to check only for the key press, and inside, have an if-else check for the edge. But the code in your comment would cause the velocity to be set to 0 if no key is pressed.
    – Blauelf
    Mar 9, 2017 at 5:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .