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:
[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:
[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:
[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:
[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:
[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
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
Offline