When making the Robin Hood activity recently, I had the idea to create a neat "superjump" feature, as mentioned here.
I've spent the weekend thinking about this solution, and was convinced that there must be a better way to achieve it without using up two monster elements. I thought that it must be possible to...
- Set a condition so that when a character runs over a monster element it triggers the effect
- A timer begins that allows that effect to operate for a set amount of time
- When the time has expired, the character returns to their normal effect
I played about with this idea, placing a simple FOR loop effect on the monster element, and adding a time variable at the start of the activity, but alas it would not work, so I asked Dan at 2Simple who sent back the simplest, most straightforward solution;
In the startup script (right clicking on the green play button) the following code is added;
var timer:int = 0;
On the monster element, in the collision script area the following code is added;
_root.jumpSpeed=-26;
_root.timer=1;
Drag a sun element onto the page and in the animation script area add the following code;
if (_root.timer > 0) {
_root.timer++;
if (_root.timer >90) {
_root.jumpSpeed=-16;
_root.timer=0;
}
}
What does all this do?
var timer:int = 0;
- this sets a variable for the timer, with an initial value of 0
_root.jumpSpeed=-26;_root.timer=1;
- when the character collides with the monster element, the jump height is increased, and the timer value changes to 1 (this is important for the code below)
if (_root.timer > 0) { _root.timer++; if (_root.timer >90) { _root.jumpSpeed=-16; _root.timer=0; } }
- the first part of this code asks a question - 'is the value of the timer is greater than 0'?, if it is then continue, otherwise check again. If the value of timer is greater than 0, then the next part of the code asks another question 'is the value of the timer greater than 90?', (the maximum value for the effect to last). If it is then reset the jump height to the original value, and reset the timer to 0 ready to be triggered again.
Note: each second during the activity takes 30 frames, so for the effect to last for 3 seconds, the maximum value of 'timer' will be 90 (3seconds x 30frames)
This would also work with...
- falling speed (_root.maxFallingSpeed=xx;)
- height (_root.player._height=xx;)
- width (_root.player._width=xx;)
- thinning your character (_root.player._xscale=xx;)
- Stretching your character (_root.player._yscale=xx;)
- Invisibility (_root.player._visible=xx;)