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

Actionscript Tutorial No.8

Here's the eighth in my series of tutorials aimed at helping 2DIY users to use, and understand, actionscript within their creations.

This tutorial deals with creating elements that will orbit around other elements.

Download 2DIY_tut8

Outline
In several 2DIY activities, you can place additional elements on the screen that can be used to orbit around other elements. You can make monster, apple or sun elements orbit around other elements, and even make your main character orbit around another element too.

image from simonwiddowson.typepad.com Code Explanation
Place an element on screen that will become the orbitee, and place another element on top of it that will become the orbiter. Right click on the orbiter element and click on the animation option (the second option). Within this option there is an ‘advanced’ setting where you can enter ActionScript code, and here we can set a rule to cause this element to circle around another element.

Tutorial
Create a platform activity. Add an apple element to the screen,  and then place  a monster on screen above the apple element. Right click on the monster element, and look for the animation settings option (the second option). Choose the advanced option and type in 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;

var angle  this sets up an variable that we are calling ‘angle’

Math.atan2 this is a trigonometrical function (remember your tan / sin / cos from schooldays?) that works out the arctan of the difference between the two elements you are using.

(this._y - _root.s13._y, this._x - _root.s13._x) this tells the activity which values to use to find the arctan above

*the value of _root.s13 will change - see notes below

+0.05;  this value adds a small amount to the variable ‘angle’ that results in the orbit effect. We can alter this to create a faster orbit (use a smaller value)

this._x = _root.s13._x + Math.cos(angle)*100;

this._y = _root.s13._y + Math.sin(angle)*100;  these two lines together are used to calculate the position of the object that is orbiting. It calculates the position of the orbitee, using the cosine or sine of the angle, and multiplies it by a value to create the orbit radius. The smaller the value, the closer the orbiter appears to the orbitee.

Now press the play button. Your character moves very slowly to begin with, but gradually speeds up as apples are collected, and the value of dx rises.

What does this do?
By using trigonometry to calculate the angle between the orbitee and the orbiter, the result is the ability to create an effect of one element orbiting around another object. 

Task
Create a platform game. Add a character, and then add some apple elements at various places. Place monster elements on top of the apple elements and add the orbiting code. Now each apple is being ‘protected‘ by a monster. Can you safely avoid them and collect all of the apple?

Notes
You can create a situation whereby the main character becomes the orbiter - and you can steer the orbitee, with your main character orbiting it, around the screen. To achieve this use a sun element and add the code to this (you cannot add actionscript to the main character).

Because you are adding the code in a different location, you do need to change the code slightly, and instead of using 

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

you need to use

var angle = Math.atan2(_root.player._y - _root.s22._y, _root._x - _root.s22._x) + 0.05; 

Make sure you place the _root.player code before the _root.s22 code, as you want the player to orbit the element, and not the other way around.

You also need to add some code to respond to key presses to move the sun element around the screen. This has been mentioned in other tutorials, but to recap;

if(Key.isDown (80)==true) {this._x +=1;} to use the P key to move right

if(Key.isDown (79)==true) {this._x -=1;} to use the O key to move left

if(Key.isDown (81)==true) {this._y -=1;} to use the Q key to move up

if(Key.isDown (65)==true) {this._y +=1;} to use the A key to move down

To work out the value of the orbitee element use the following;

In a collecting or journey game; _root.s2-_root.11 (monsters), _root.s12-_root.s21 (apples), _root.s22-_root.s31 (suns) Further objects added will be _root.s32, _root.s33 etc 

In a platform or snake game; _root.s3-_root.12 (monsters), _root.s13-_root.s22 (apples), _root.s23-_root.s32 (suns) Further objects added will be _root.s33, _root.s34 etc

In collecting, platform and snake games, use _root.player for the main character

In journey games use _root.car for the main character

You can find out more about this feature here

Posted in Tutorials | Permalink

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

ActionScript Tutorial No.7

Here's the seventh in my series of tutorials aimed at helping 2DIY to use, and understand, actionscript within their creations.

This tutorial deals with increasing the speed of your main character.

Download 2DIY_tut7

Outline
In 2DIY platform activities, you can make your character move really slowly, and gradually increase the speed they move at using monster elements.

image from simonwiddowson.typepad.com Code Explanation
Right 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 speed that a character moves at.

Tutorial
Create a platform activity. 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.dx=1;

_root.dx=1;  this tells the activity to alter the speed of the main characters movement (by default ‘4’ is the normal value. Lower values are slower, and higher values are quicker)

Place the monster element in the same place as the main character element - this will ensure that when the activity starts the code is activated and the speed of the character is instantly slowed - and also make it transparent by colouring it in with the chequerboard pattern (you do not want to see the monster element on screen).

Place more monster elements at various points within the activity with the same code, but each time increase the value of the _root.dx= by 1 (so use 2, then 3, then 4). Above each transparent monster element you use, place a collectable apple element. This will give the effect of every time an apple is collected the characters speed will increase.

Now press the play button. Your character moves very slowly to begin with, but gradually speeds up as apples are collected, and the value of dx rises.

What does this do?
By increasing the value of dx you are increasing the speed that the main character moves at. 

Task
Create a platform game. Add a character, and then add some apple elements at various places. Add some transparent monster elements below some of the apple elements using the code mentioned above.

Notes
You can use this in reverse  - and begin with a fast moving character, but each time an apple element is collected the code is written so that the value of dx is reduced by 1 each time.

Posted in Tutorials | Permalink

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

ActionScript Tutorial No.6

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".

Download 2DIY_tut6

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 Explanation
2diy_tut3_img1Right 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

 

Posted in Tutorials | Permalink

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

Actionscript Tutorial No.5

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.

Download 2DIY_tut5

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 Explanation
2diy_tut3_img1Right 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

Posted in Tutorials | Permalink

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

Actionscript Tutorial No.4

Here's the fourth in my series of tutorials aimed at helping 2DIY users to use, and understand, actionscript within their creations.

This tutorial deals with creating elements that you can make appear transparent within your activity.

Download 2DIY_tut4

Outline
In several 2DIY activities, you can make elements appear see-through with just a small amount of code. This is great for Hallowe’en style activities.

Code Explanation
If you right click on the green ‘play’ triangle you can add additional code to the start up script of any activity, and here we can set a rule to make particular elements appear see through. 

2diy_tut4_img1
Tutorial
Create an activity. Add an apple element to the screen, and then right click on the green ‘play’ arrow at the top of the activity. Add the following code below the code that is already present;

s13._alpha=50;

s13 (this is the identification value for each element you add to your activity - see the table at the end of this tutorial for the identification value of elements in various activities)

._alpha= (this is the amount of transparency that you are going to give each element. Setting the alpha value to be 0 will result in a transparent element that cannot be seen, setting the alpha value to 100 will make it a solid colour)

NOTE: there is no _ before the s13 part of the code.

Now press the play button, and watch the apple elements on screen - they are partly see through. By varying the value of ‘alpha’ you can make the elements appear more or less see through. 

What does this do?
You are telling the computer to set a transparency value to each of the identified element (the s2 element). This has to be done in the start code section, so that each element can still be given animation, as normally you would enter the animation code in the “ADV” option of each elements animation settings.

Task
Create a platform game themed around a haunted house. Add some apple elements, and give each an animation property by right clicking on each apple and choose the animation (the second) option. Choose from the options available. Place your character at the bottom of the house.

Now run your game - the apple elements will appear slightly see through, and look ghostly as they move about the screen. 

2diy_tut4_img3

Notes
The value of each element (starting with ‘s’) is as follows for each different type of activity;

In Collecting and Journey Activities; 

  • The first 10 monsters are labelled as s2-s11
  • The first 10 apples are labelled as s12-s21
  • The first 10 apples are labelled as s12-s21
  • The first 10 suns are labelled as s22-s31 
  • Further objects added will be s32, s33 etc

In Platform, Snake and Maze Activities;

  • The first 10 monsters are labelled as s3-s12
  • The first 10 apples are labelled as s13-s22
  • The first 10 suns are labelled as s23-s32
  • Further objects added will be s33, s34 etc

As you can see from the image above right, the ID values start with the first column and work their way down, and then move onto the second and third columns. However, if you are struggling to identify an element, right click on it, choose the animation settings, and select “ADV”. Use the code editor (the spanner), and click on the first drop down menu. You should then see the ID for that element.

2diy_tut4_img2

You can find out more about this feature in this article

Posted in Tutorials | 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.