Thursday, January 17, 2013

Simple Key Press

In this tutorial we will cover basic key controls for a single sprite. If you're looking for something a little more advanced, check out the Smooth Key Movement tutorial.

Start with a new sketch and pick a sprite.  Or, simply make a new sprite within one of your projects.
From the Orange Control Menu:Select the "when [space] key pressed" script header. Click the small, black triangle to bring up a menu and select "up arrow" from the menu. Now, whenever you press the "up arrow," this script header will activate all the scripts under it. Cool!
From the Blue Motion Menu:
Select the "point in direction (90)" script...
...and place it directly under the "when [up arrow] key pressed" script header. Change the direction by clicking on the little black triangle and selecting "(0) up."
Again from the Blue Motion Menu, select the "move (10) steps" script and place it directly under the "point in direction (0)" script. Now, when we press the "up arrow," the sprite will point up and move 10 steps in that direction. Yay!
We can reuse the code we just made instead of going back to each of the menus by clicking and holding down on the top "when [up arrow] key pressed" script header and selecting "duplicate" from the menu that appears. Put the copy just below this version and change the key pressed and direction to each of the new versions you place.
When you are finished, your script area should look like this.
If your sprite is like mine and flips around so it's upside down when it moves, just go to the top of your screen and click one of the three buttons next to the image to make it only face left-right or not change facing at all. Name and save your project and you're done. Congratulations! You now have a sprite that moves around when you press the arrow keys!