1

I'm currently making a project, where I have a sprite that is controlled by the player, and it has to make its way through a maze-like stage:

enter image description here

The red box is the player. How do I make the player not be able to go through the black lines?

This question came up several times while I was mentoring at Scratch Day. I've provided a self-answer below, but I'd be interested in seeing other methods as well.

Mithical
  • 603
  • 1
  • 17
  • 28
  • Further reading: [Collision detection](https://en.wikipedia.org/wiki/Collision_detection). – 001 May 31 '18 at 11:05

2 Answers2

4

The way that I do this is through the use of what I call the "old X and old Y" method.

Essentially, you constantly record the current location of the sprite, and then go back to that when touching the lines.

To be a bit more helpful...

This is the entirety of the script that takes care of it:

enter image description here

Let's go through it and explain how it works.

First, we're using two variables - here named "OldX" and "OldY". A variable is a way of storing data used in a the project. It can hold numbers or letters. A variable is sort of like a box - you can store things in it, and look to see what's in it at different times. You can create a variable in the "Data" blocks section of the scripts tab.

So what is the point of these variables, and what are we putting in them?

These variables are storing the x and y positions of the sprite - where it currently is on the stage. Y is the vertical axis, and x is the horizontal axis.

So what these variables are doing are keeping note and storing where the sprite currently is, using the "x position" and "y position" blocks found in "Motion".

So we're storing the data. Now what do we need to do with this data? Why are we storing it?

We need to store this so that we can use it to go back to that point, when we're touching the lines.

So let's take a look for a second at the beginning of this script - we've got a "when green flag clicked", to get the script started, and a "forever", so that it's always checking to see if it's touching the lines.

Then we've got the "if". This is where we check if we're touching the lines - here I've opted to check if it's touching the color black, because I used the lines as part of the stage, and also checking for the edge. Depending on your project, you can change what it's checking that it's touching, to e.g. another sprite.

So we're checking to see if it's touching the stuff that it can't go through. Then we need to make it actually not go through.

That's why we're making the sprite go to where it was before. We've got the "set x" and "set y" blocks, to make it go to a specific location. We're setting the x and y to the "OldX" and "OldY" variables, which have been storing the location, constantly updating.

So we set the x and y to where it had been, and... it doesn't go through the black!


One last note - it's not obvious, but the order of the script matters here. If you put the "set OldX" and "set OldY" blocks above the "if" section, it won't work. If it does it that way, then it sets the variables to the location even if it's touching black, and so you end up just going through the lines like the script doesn't exist.

Mithical
  • 603
  • 1
  • 17
  • 28
0

You can make the player move the negative number of steps of its normal speed. For example if you have such a code:

when right arrow pressed
move (10) steps

Add in this code (note that I wrote rgb() but you use the pointer to detect and store the color):

when green flag clicked
if touching rgb(0, 0, 0) and key (right) pressed
move (-10) steps
Account_2
  • 53
  • 6