Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » How to make charecters move with the keyboard?
- shadowmouse
-
100+ posts
How to make charecters move with the keyboard?
Not very specific because there are at least two, maybe three ways in which a sprite could move with the arrow keys. And yes this can all be done with x velocities and y velocities… but that's complicated.
1)
when gf clicked
forever
if <key pressed?> then
move (5) steps
end
if <key pressed?> then
turn clockwise (5) degrees
end
if <key pressed?> then
turn counterclockwise (5) degrees
end
if <key pressed?> then
move (-5) steps
end
This is your normal forward to move forwards, left/right to turn, down to go backwards.
You can change the actual amounts of turn and move to speed up/slow down the sprite or make it turn tighter/wider.
2)
when gf clicked
forever
if <key pressed?> then
change y by (5)
end
if <key pressed?> then
change x by (5)
end
if <key pressed?> then
change x by (-5)
end
if <key pressed?> then
change y by (-5)
end
This moves the sprite in the direction of the arrow key that is pressed. Useful for moving something such as a pointer around the screen.
3)
when gf clicked
forever
if <key pressed?> then
change y by (5)
point in direction (0 v)
end
if <key pressed?> then
change x by (5)
point in direction (90 v)
end
if <key pressed?> then
change x by (-5)
point in direction (-90 v)
end
if <key pressed?> then
change y by (-5)
point in direction (180 v)
end
This also makes the sprite point in the direction that it is moving, useful for top-down movement.
1)
when gf clicked
forever
if <key pressed?> then
move (5) steps
end
if <key pressed?> then
turn clockwise (5) degrees
end
if <key pressed?> then
turn counterclockwise (5) degrees
end
if <key pressed?> then
move (-5) steps
end
This is your normal forward to move forwards, left/right to turn, down to go backwards.
You can change the actual amounts of turn and move to speed up/slow down the sprite or make it turn tighter/wider.
2)
when gf clicked
forever
if <key pressed?> then
change y by (5)
end
if <key pressed?> then
change x by (5)
end
if <key pressed?> then
change x by (-5)
end
if <key pressed?> then
change y by (-5)
end
This moves the sprite in the direction of the arrow key that is pressed. Useful for moving something such as a pointer around the screen.
3)
when gf clicked
forever
if <key pressed?> then
change y by (5)
point in direction (0 v)
end
if <key pressed?> then
change x by (5)
point in direction (90 v)
end
if <key pressed?> then
change x by (-5)
point in direction (-90 v)
end
if <key pressed?> then
change y by (-5)
point in direction (180 v)
end
This also makes the sprite point in the direction that it is moving, useful for top-down movement.
- PH-zero
-
100+ posts
How to make charecters move with the keyboard?
when green flag clicked
forever
change x by ( ( <key right arrow pressed> - <key left arrow pressed> ) * 3 )
change y by ( ( <key up arrow pressed> - <key down arrow pressed> ) * 3 )
/forever
a pretty complex but efficient solution.
hope i helped ^^
forever
change x by ( ( <key right arrow pressed> - <key left arrow pressed> ) * 3 )
change y by ( ( <key up arrow pressed> - <key down arrow pressed> ) * 3 )
/forever
a pretty complex but efficient solution.
hope i helped ^^
- shadowmouse
-
100+ posts
How to make charecters move with the keyboard?
There is no need to go into anything like that unless you have an extremely compicated game (upwards of about 40 sprites and scripts that require extensive scrolling to navigate) which lags greatly and needs as much chopping off as possible. My solutions have always worked for me, do exactly the same (at least the middle one does), are much mroe understandable and only slightly (usually unnoticably more laggy).
P.S. sorry for ranting and thank you for showing me that scratch 2.0 now lets you put boolean in reporter spaces.
P.S. sorry for ranting and thank you for showing me that scratch 2.0 now lets you put boolean in reporter spaces.

- pinkpretzelfox11
-
7 posts
How to make charecters move with the keyboard?
Okay, this is how it works - At the top of the script you'll need a little block thing that says when whatever key is pressed (for instance when right arrow key pressed). You'll find then pieces of the script in the block called events. Next underneath that you'll need a piece of the script from the blue block labeled motion - now this could be move how ever many steps or change x by any number if you want to move. Put a minus in front of the number if you want to move to the left. If you want to move up a down just use the change y by block thing and a minus in front of the number if you want to move down. Simplez. 

- PH-zero
-
100+ posts
How to make charecters move with the keyboard?
are much mroe understandableok, I'll give you that xD!
sorry for ranting and thank you for showing me that scratch 2.0 now lets you put boolean in reporter spaces.no problem and besides, you can do this also in 1.4 .
I put a boolean block into a reporter space by accident and thought… just.. 0o
But of course: true=1, false=0
I always use this cause you can extend this very much.
With this solution you can prevent diagonal movement or even make it as fast as straight moving!
(PS, tell me if u interested

- shadowmouse
-
100+ posts
How to make charecters move with the keyboard?
@PH-zero how do you do that (I could vastly improve my etch-a-sketch with it)?
@pinkpretzelfox11 it is generally a good idea to use as few different hat blocks as possible. This simulates more realisitic coding and can be easier to add other things or to debug.
@pinkpretzelfox11 it is generally a good idea to use as few different hat blocks as possible. This simulates more realisitic coding and can be easier to add other things or to debug.
- Walrus227
-
54 posts
How to make charecters move with the keyboard?
Have the script here:
https://sites.google.com/site/scratchobsoleteblocks/scripts
https://sites.google.com/site/scratchobsoleteblocks/scripts
- epicface10
-
54 posts
How to make charecters move with the keyboard?
Have the script here:
https://sites.google.com/site/scratchobsoleteblocks/scripts
Thanks, I made a test. http://scratch.mit.edu/projects/11531203/
- shadowmouse
-
100+ posts
How to make charecters move with the keyboard?
Two things, you shouldn't be using lots of hats(!!!!!!!!) and you should use change x by 10 not move 10 steps for the right arrow because the way it is now, if the sprite is rotated then that could mean that pressing the right and left arrows would do the same thing. Try this. If you want I'll give you scripts that make the chosen way of movement smooth (acceleration and decelleration rather than on/off). Also if you intend on making a top down game it might be that you only want the sprite to turn if it is moving and if so in proportion to its speed (like a car) this is also possible. I have made a rather large game that uses the above but unfortuantely scratch 2.0 can't handle it (1.4 could
).

Last edited by shadowmouse (July 22, 2013 14:42:33)
- epicface10
-
54 posts
How to make charecters move with the keyboard?
this. If you want I'll give you scripts that make the chosen way of movement smooth (acceleration and decelleration rather than on/off). Also if you intend on making a top down game it might be that you only want the sprite to turn if it is moving and if so in proportion to its speed (like a car) this is also possible. I have made a rather large game that uses the above but unfortuantely scratch 2.0 can't handle it (1.4 couldTwo things, you shouldn't be using lots of hats(!!!!!!!!) and you should use change x by 10 not move 10 steps for the right arrow because the way it is now, if the sprite is rotated then that could mean that pressing the right and left arrows would do the same thing. Try).
What do you mean don't use hat's? Also, I stored that games script in my backpack.
- shadowmouse
-
100+ posts
How to make charecters move with the keyboard?
I mean it's a bad habit to just use lots of hat blocks rather than one script which has a lot of ifs. Of course sometimes it is necessary to use lots of hat blocks (broadcasts e.t.c.) though broadcasts can be worked around, and you will need at least one. This is because ‘real’ programming languages are structured as one: beginning -> code -> end.
- rara23
-
4 posts
How to make charecters move with the keyboard?
if you guys still need any help here is a script for moving around with velocity
hope that helps
PS: that was a jumping script reply of you want a moving script
hope that helps

- redfeather11
-
9 posts
How to make charecters move with the keyboard?
My script for platforming!
Last edited by redfeather11 (April 11, 2015 22:12:17)
- redfeather11
-
9 posts
How to make charecters move with the keyboard?
My script for platforming!
The change x by (velocityx) and change y by (velocityY) go at the bottom of forever, not in any of the ‘ifs’.
- Discussion Forums
- » Help with Scripts
-
» How to make charecters move with the keyboard?