Here's the fifth in my series of tutorials aimed at helping 2DIY users to use, and understand, actionscript within their creations.
This tutorial deals with allowing extra players to move around within your activity.
Outline
In several 2DIY activities, you can allow extra players to take part in the activity by specifying keys to move particular elements around the screen.
Code ExplanationRight click on an element (either a monster or an apple element) and click on the animation options (the second option). Within this option there is an ‘advanced’ (ADV) setting where you can enter ActionScript code, and here we can set a rule to make certain keys cause the element to move.
Tutorial
Create a collecting activity. Add an apple element to the screen, right click on it, and look for the animation settings option (the second option). Choose the advanced (ADV) option and type in the following code;
if(Key.isDown(80)==true) { this._x +=1; }
if(Key.isDown(79)==true) { this._x -=1; }
if(Key.isDown(81)==true) { this._y -=1; }
if(Key.isDown(65)==true) { this._y +=1; }
if (this tells the computer that there is a condition that will decide whether the code is to be run or not)
((all the code written between these double brackets)) (these are the conditions that need to be met before the code will run)
Key.isDown(80)==true (this means that a key with an identification value of 80 is pressed down)*
Key.isDown(79)==true (this means that a key with an identification value of 79 is pressed down)*
Key.isDown(81)==true (this means that a key with an identification value of 81 is pressed down)*
Key.isDown(65)==true (this means that a key with an identification value of 65 is pressed down)*
*the identification numbers for all keys is given at the end of this tutorial
{ all the code written between these brackets } (this is the code that will run IF the conditions set out above are met)
this._x +=1; (this means that the horizontal position of the element will increase by 1 pixel - ie move a little to the right of the screen)
this._x -=1; (this means that the horizontal position of the element will decrease by 1 pixel - ie move a little to the left of the screen)
this._y -=1; (this means that the vertical position of the element will decrease by 1 pixel - ie move a little up the screen)
this._y +=1; (this means that the vertical position of the element will decrease by 1 pixel - ie move a little down the screen)
Now press the play button. One person can move the main character using the arrow keys as normal, but a second player can move the apple around the screen using the Q and A keys to move up and down, and the O and P keys to move left and right.
What does this do?
You are telling the computer to check whether a particular key has been pressed, and if it that key has been pressed to respond by making a particular element move in a particular way on screen.
Task
Create a collecting game. Add a character, and then add an apple element and right click on the apple element and choose the animation (the second) option. Select the ‘advanced’ (ADV) option and enter the code above to respond to key presses. Now run your game - can one player avoid being caught by the other player?
Notes
You can alter the idea of the game and use a monster element instead of an apple element. In this case instead of the main character trying to collect an apple element, the main character will be try to avoid being caught by a monster element.
You do not have to limit it to 2 players either, you can include as many players as are physically able to press keys on the keyboard.
Why not try with three players - player one could be the main character who is trying to collect an apple element. Player two could be an apple element trying to avoid being caught by player one, whilst player three could be a monster element who is trying to catch player one.
Or, why not try creating a game with several players moving around a maze?
The identification values for the keys that can be used are as follows;
Alphabet key identification values; A (65) - Z (90)
Numerical key identification values; 0 (48) - 9 (57)
left arrow(37) / up arrow (38) / right arrow (39) / down arrow (40)
space bar (32) / enter key (13)
You can find out more about this feature in this article


Recent Comments