This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2009-07-08 15:36:03

The-Whiz
Scratcher
Registered: 2007-07-09
Posts: 1000+

Sensing Tutorial

Here is a sensing tutorial for projects:

1. Make the main sprite that needs to sense things. We'll call this 'main'.

2. Make 4 sensors to go on all sides of the sprite.

3. To make the sensors go to the right position, use this template for their scripts:

To the right:

Code:

[forever]
[][go to x: (([x position] of [main]) + (R)) y: (([y position] of [main]))]
[][if <touching color [grey]>]
[][][set [right-touching] to [1]]
[][else______________]
[][][set [right-touching] to [0]]
[][__________________]
[_______]

R equals the distance you want the right sensor to be from the sprite.


To the left:

Code:

[forever]
[][go to x: (([x position] of [main]) - (L)) y: (([y position] of [main]))]
[][if <touching color [grey]>]
[][][set [left-touching] to [1]]
[][else______________]
[][][set [left-touching] to [0]]
[][__________________]
[_______]

L equals the distance you want the left sensor to be from the sprite.


Up:

Code:

[forever]
[][go to x: (([x position] of [main])) y: (([y position] of [main]) + (U))]
[][if <touching color [grey]>]
[][][set [up-touching] to [1]]
[][else______________]
[][][set [up-touching] to [0]]
[][__________________]
[_______]

U equals the distance you want the up sensor to be from the sprite.


Down:

Code:

[forever]
[][go to x: (([x position] of [main])) y: (([y position] of [main]) - (D))]
[][if <touching color [grey]>]
[][][set [down-touching] to [1]]
[][else______________]
[][][set [down-touching] to [0]]
[][__________________]
[_______]

D equals the distance you want the down sensor to be from the sprite.

Now for the main sprites code:

Code:

[forever]
[][if <<key [right arrow] pressed?> and <(right-touching) = (0)>>]
[][][change x by (RM)]
[][_______________________________________________________]
[][if <<key [left arrow] pressed?> and <(left-touching) = (0)>>]
[][][change x by (LM)]
[][_____________________________________________________]
[][if <<key [up arrow] pressed?> and <(up-touching) = (0)>>]
[][][change y by (UM)]
[][___________________________________________________]
[][if <<key [down arrow] pressed?> and <(down-touching) = (0)>>]
[][][change x by (DM)]
[][________________________________________________________]
[______]

RM, LM, UM, and DM all stand for the amount you want that sprite to move when the corresponding key is pressed.

4. Run

Hope you find this helpful!

Last edited by The-Whiz (2009-07-08 15:37:21)

Offline

 

#2 2009-07-09 00:01:30

greenflash
Scratcher
Registered: 2009-05-27
Posts: 1000+

Re: Sensing Tutorial

That's so cool. Sensing is really useful


http://i48.tinypic.com/2wrkirk.pnghttp://i46.tinypic.com/6r5zk7.pnghttp://i45.tinypic.com/2vtxr1t.png

Offline

 

#3 2009-07-09 11:49:46

beanbug
Scratcher
Registered: 2009-06-04
Posts: 100+

Re: Sensing Tutorial

The-Whiz wrote:

To the right:

Code:

[forever]
[][go to x: (([x position] of [main]) + (R)) y: (([y position] of [main]))]

R equals the distance you want the right sensor to be from the sprite.

I used to use that method, but I now find this time-saving and a lot easier:
Have "go to [main]"
then offset the rotation center so the sensor ends up in the right place.
It's also cleaner  tongue


A Life? Cool! Where can I download one of those from?

Offline

 

#4 2009-07-09 11:52:39

The-Whiz
Scratcher
Registered: 2007-07-09
Posts: 1000+

Re: Sensing Tutorial

Yeah, I used that in a game, but I just didn't do it here. I'll change that...

Offline

 

Board footer