The 2DIY script archive

A resource containing Actionscript examples for 2Simples 2DIY software.

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

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 by webmaster on 04/20/2009 in Animation Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

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 by webmaster on 03/26/2009 in Animation Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

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 by webmaster on 03/23/2009 in Animation Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

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 by webmaster on 03/19/2009 in Animation Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

Make a monster orbit around a collectable item

In a platform activity, you can make a monster circle around a collectable item, making it difficult for a character to collect the item.

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

var angle = Math.atan2(this._y - _root.s13._y, this._x - _root.s13._x) + 0.05; 
this._x = _root.s13._x + Math.cos(angle)*100;
this._y = _root.s13._y + Math.sin(angle)*100;

Explanation: You need to understand trigonometry for this one! The first step works out the angle between the 2 objects by taking the arctan of the difference in y over the difference in x. The 0.05 adds slightly to the angle which results in the orbit effect. The next two lines calculate the x and y position of  the monster by starting off from the apple's position and taking the cos or sin of the angle, multiplied by the radius of the orbiting circle (in this case 100).

View example, Download Orbiting_monster

Posted by webmaster on 03/19/2009 in Animation Actionscript | Permalink

Reblog | | Digg This | Save to del.icio.us |

More Articles »
login

Recent Posts

  • Luke's Shark Attack
  • Lois and Paige's 2DIY drag game
  • Ellie-Jade and Jordan's Drag game!
  • Alfie and Savannah's Drag game
  • Chinese New Year
  • Haiti Earthquake Activities
  • Year 6 Little Red Riding Hood game
  • More from Sonning Common Primary
  • Collaborative learning and playing
  • Simon Haughton's 2DIY Planning

Recent Comments

  • webmaster on Discuss
  • Angela Canas on Discuss
  • webmaster on Discuss
  • Angela Canas on Discuss
  • webmaster on Discuss
  • Ali on Discuss
  • webmaster on Discuss
  • Max Wainewright on Discuss

Archives

  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • September 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009

More...

2simpleant's Blog

| The 2DIY script archive |

Design by @xannov