Stop a sprite at a specific location

I have a student that would like to have his sprite stop when it reaches a specific location. How do we do this?

@m_viscarri

Hi Misty,

It’s great that your students are asking this, because it’s going to be covered in the next couple of lessons on conditionals.

You’ll need to use a conditional to only change the sprite movement when it is not yet where you want it to stop. The instructions to the computer would look something like:

if the sprite is to the left of this position
     move it to the right

In Javascript, that would be:
if (sprite.x < stopCoordinate) {
     sprite.x = sprite.x + 5;
}

Here’s an example of how the code might look: Stop a Sprite Example

I also think this is a great example of when you can tell a student that it’s wonderful that she’s thinking along those lines, and that the class will be covering it so stay tuned. If you don’t have one, it might be fun to have a “Things we want to learn how to do” poster and cross things off as you get to them throughout the lessons. It could also be a differentiation tactic for the kids who are ready to explore on their own and answer some of the questions for the class.

(Hope the answer didn’t get too off track, but I really liked my poster for all those types of questions. :smiley: )

Elizabeth

4 Likes