How can we make the Alien go around the screen continuously?

Link to the project or level:(Game Lab - Code.org)
What I expect to happen: I would like the alien sprite to move around the outside of the screen continuously.
What actually happens: After the first time around, the alien goes off the top of the screen and never returns.
What I’ve tried: I’ve tried placing a copy of the first If block below the other If blocks to see if that would trigger the alien to move to the right again once it gets to the top of the screen. It made no difference.
Also, by accident one time, I left out the the “alien.velocityX = 0” in the last If block and to my surprise, the alien eventually came back around and re-entered the screen on the right-hand side moving downward.
But for the life of me, I can’t figure out why the given code (as is) does not make the alien move around the screen continuously.

Ok… this one took me several iterations to get it. Here is a remixed version that has the alien moving continuously around the outside edge. Mine is going in the opposite direction and includes more checks for the alien’s position. Take a look and let me know what you think.

1 Like

Thank you for this solution. I understand how it works, but I don’t understand why the original code doesn’t work to do the same thing. In my original code, why does the code work once but not the second time around? For example, in my original code, we only need to check if the Y property is less than 50, which triggers to be true and then moves the alien to the right. Why does this not work again when the alien comes back the second time? I don’t understand that.

Great question that puzzles many of us :slight_smile:

@terickson1 & @mriley

I believe I can answer that question for you. If you put some watchers in the debug section of the site (see screenshot below), you will see that after the alien rounds the 3rd corner, the alien.x position is 45. This means that that last conditional block you use is still being triggered. So when it reaches the 1st corner the 2nd time, the velocity momentarily becomes 5 and them immediately becomes -5 again due to the last conditional since it only looks at the alien.x value. If you clean up that last conditional or change some of the values of the first, I’ll bet it will behave more predictably.

Good luck!

Mike

1 Like

OOHHHH-kay! Yes, I get it now. Thank you very much!