Here's the sixth in my series of tutorials aimed at helping 2DIY users to use, and understand, actionscript within their creations.
This tutorial deals with creating time limited effects such as higher than normal "power jumps".
Outline
In 2DIY platform activities, you can make your character jump higher for a limited time using a monster element, as well as a sun element.
Code ExplanationRight click on a monster element and click on the collision options (the third option). Within this option there is an ‘advanced’ setting where you can enter ActionScript code, and here we can set a rule to increase the height that a character can jump.
Tutorial
Create a platform activity. Right click on the play button (the triangle) and add the following code below the code that you see (DO NOT REMOVE THE CODE THAT IS ALREADY THERE!)
var timer: int = 0;
var this tells the computer that you are setting a variable
timer is the name of the variable you are setting
int = 0; this gives a value of 0 to the variable timer
Add an monster element to the screen, right click on it, and look for the collision settings option (the third option). Choose the advanced option and type in the following code;
_root.jumpSpeed=-20;
_root.timer=1;
_root.jumpSpeed= (this tells the computer the height that the character can jump up. It is a negative value as the character has to rise UP the screen)
_root.timer=1; (this sets the condition to start a timer that determines how long the extra jump height lasts)
Now add a sun element to the screen and add look for the ‘animation’ settings (the second option). Choose the advanced (ADV) option and enter the following code;
if (_root.timer >0 ) {
_root.timer++;
if(_root.timer >90) {
_root.jumpSpeed=-16;
_root.timer=0;
}}
if (_root.timer >0 ) asks whether the timer has been activated by the monster element code. If the timer value has reached 1 (when the character touches the monster element) then the next line of code is activated;
_root.timer++; the value of ‘timer’ increases by 1 each time this runs
if(_root.timer >90) asks whether the value of the timer has reached 90 ( about 3 seconds). If it has, then the next line of code is activated)
_root.jumpSpeed=-16; and _root.timer=0; the jump height is returned to normal, and the timer value is reset to 0
Now press the play button. Try jumping and see how high you jump. Now move onto the monster element and jump again. You are jumping higher. Move off the monster element, wait a few seconds and jump again. The jump has returned to its normal height.
What does this do?
By using the sun element to run the code, you are telling the computer to constantly check what the value of a timer value is. Initially this is 0, and remains 0 until the character collides with a monster element. When this happens the value of the timer becomes 1, and this triggers the code in the sun element to run making the jump higher. The timer value rises and rises until it reaches 90, and then both the jump height and timer reset themselves to what they were at the beginning of the activity.
Task
Create a platform game. Add a character, and then add some apple elements on a high platform - a platform too high to reach. Add a monster element and make it look like a spring / canon. Add the code to this element and use it as a ‘powerboost’
Notes
The sun element that contains the code can either be made to appear invisible (colouring it with the chequerboard pattern), or add some clipart and place it on screen as a static element.
You can find out more about this feature in this article


Recent Comments