The 2DIY Script Archive

A resource of Actionscript examples for 2Simple's 2DIY software

  • Home
  • ActionScript
  • Animation Script
  • Collision Script
  • Start Button Script
  • Examples
  • Ideas
  • Help / Videos
  • Discuss

Time limited effects

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)

Download 2DIY file

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;)

Posted in Animation Actionscript, Collision Actionscript | Permalink

Reblog (0) | | | | Pin It! |

Sun elements are fun

The sun element (in platform activities) is a useful little tool. It can be used to initiate other actions during an activity (see previous posts in this section) but can also do lots more...

  • If you make the sun element transparent, you can cause it to create a sound effect / background music when a character passes over it.
  • You can use it to create items that move across (or up or down) the screen that won't interfere with the activity but add to the experience. For example, in an underwater activity create bubbles that rise, or create clouds that drift across the sky.
  • The sun element can also be used to create "special" platforms that are really gaps. Simple copy the image used for the platform in an activity and paste it into a sun element. When placed next to a normal platform a character will walk along then fall through - providing hidden entrances to sections.
View example: Download Sound_effect
View example: Download Fake_platform
View example: Download Moving_cloud

Posted in Animation Actionscript | Permalink

Reblog (0) | | | | Pin It! |

Creating a gravity effect

This code allows elements on the screen to be pulled towards the ground, and bounce several times as gravity works on them.

Within a platform activity if you right click on the "test activity" (green triangle) you can add the following code;
 
var gravity:Number = 2;
var velocity:Number = 0;

These are two defined variables - gravity and velocity. Gravity is a constant downward acceleration, set to 2. Velocity is the current vertical speed of the object in question. 

Now select an element. Right click on it then click on the green rotation arrow below the image to view the animation options that are attached to the apple. Click on the red fish (for animation settings) and click on the "adv" option. in the script box that appears, enter the following code;

if ( this._y >= 453 and _root.velocity < 2.5 and _root.velocity >= 0 ) { this._y = 460; _root.velocity =0; }
else {
if ( this._y >= 460 and  _root.velocity>0 ) { _root.velocity = -_root.velocity * .7 ; }
this._y += _root.velocity; 
_root.velocity += _root.gravity;
}

Here is an explanation of how this works from Dan at 2Simple;

Look at the last 2 lines first. Every moment, unless the object is near the floor, (1) that object should move up or down by its current velocity, and (2) its velocity should be changed by the gravity value.

Look at the 3rd last line: If the object has reached the floor and is still heading downward, make it "bounce" - change its velocity be the negative of what it currently is, multiplied by a damping factor so it loses height each time it bounces.

Look at the first line: If the object is close to the floor and is heading downward *slowly*, make it come to rest on the floor.

View example; Download Gravity

Posted in Animation Actionscript | Permalink

Reblog (0) | | | | Pin It! |

Respond to Key Presses

This code allows objects (monsters / collectables / animations) to be moved manually around the screen during an activity. It works well when used with a 2-player activity. Select an element. Right click on it then click on the green rotation arrow below the image to view the animation options that are attached to the apple. Click on the red fish (for animation settings) and click on the "adv" option. in the script box that appears, enter the following code;

if(Key.isDown(65)==true) { this._x +=1; }

the codes for all keys are: A (65) - Z (90) / 0 (48) - 9 (57) / left (37) / up (38) / right (39) / down (40) / space (32) / enter (13) You can use multiple lines to create multi-directional movement.

View example; Download Move_object_with_keys

Posted in Animation Actionscript | Permalink

Reblog (0) | | | | Pin It! |

Make the collectable items run away

In a platform activity, you can make a collectable run away from a character.

Within a platform activity select an "apple". Right click on it then click on the green rotation arrow below the image to view the animation options that are attached to the apple. Click on the red fish (for animation settings) and click on the "adv" option. in the script box that appears, enter the following code;

if (( Math.abs(_root.player._x - this._x) < 100) and ( Math.abs(_root.player._y - this._y) < 100)) {
var angle = Math.atan2(this._y - _root.player._y, this._x - _root.player._x); 
this._x += Math.cos(angle)*2;
this._y += Math.sin(angle)*2;
} 

The first step specifies that the apple should only run away if the player is closing in. The 2nd step works out the angle between the 2 objects by taking the arctan of the difference in y over the difference in x. The next two lines calculate the x and y position of  the apple by taking the cos or sin of the angle. 

View example; Download Object_runs_away

Posted in Animation Actionscript | Permalink

Reblog (0) | | | | Pin It! |

More Articles »

Recent Posts

  • Coding with 2DIY - a 6 week unit of lessons
  • 2DIY and the New Computing Curriculum
  • 2DIY with Gifted and Talented groups
  • Making characters fly (and other effects)
  • High Lawn Primary Games
  • Creative Learning
  • Gallons of Games
  • Games Pod Creations
  • Kensington Avenue Primary 'Games Pod'
  • Actionscript Tutorial No.8

Categories

  • 2DIY Examples
  • Actionscript Code Names
  • Animation Actionscript
  • Collision Actionscript
  • General
  • Lesson Plans & Ideas
  • Start Button Actionscript
  • Tutorials
See More

Search

| The 2DIY script archive |

Maintained by the Digital Learning Coordinator, Porchester Junior School, Nottingham.

The actionscript codes that are used within this archive were provided by
Max Waineright and Dan Ziskind and reproduced with their permission.