The 2DIY script archive

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

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

Actionscript Tutorial No.3

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

This tutorial is an advanced tutorial and deals with creating 'apple' elements that will move away from your character as you approach them.

Download 2DIY_tut3

Outline
In a 2DIY platform, collecting, journey or maze game, you can make the challenge much harder by placing a collectable (apple) element that will move away as your character approaches it.

Code Explanation
2diy_tut3_img1
If you right click on an apple element, you will see the options for how to make the apple element behave (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 the apple move away from the main character as it approaches). 

Tutorial
Create a collecting activity. Add some apple elements to the screen, and then right click on each apple and look for the animations settings option (the second option). Choose the advanced (ADV) option. Unfortunately for this code you are unable to use the code editor (the spanner icon) to help you add the code. You will need to enter the following code  carefully line by line (If you are using this in a journey game you need to change _root.player with _root.car)

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;

} 

 

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)

Math.abs(_root.player._x-this._x)<100) (this means the absolute value of the horizontal position of the main character must be less than 100 pixels from the apple element)*

Math.abs(_root.player._y-this._y)<100) (this means the absolute value of the vertical position of the main character must be less than 100 pixels from the apple element)*

*the Maths.abs value simply means that if the value of the distance between the main character and the apple element is a negative number, it will be read as a positive number, ie -3 will be read as +3

and means that both conditions must be met for the code to run

{ all the code written between these brackets } (this is the code that will run IF the conditions set out above are met)

var angle = (this a variable called “angle” that is being defined)

Math.atan2 (this is a mathematical function - it works out the value of the tangent between two points to create an angle)

this._y - _root.player._y,  (the vertical position of the main character is subtracted from the vertical position of the apple element for one of the points in the tangent calculation)

this._x - _root.player._x (the horizontal position of the main character is subtracted from the horizontal position of the apple element for the other points in the tangent calculation)

this._x += (this gives the apple element a new horizontal value to appear on screen at)

Math.cos(angle)*2; (this mathematical function sets the horizontal value for the position of the apple element)

this._y += (this gives the apple element a new vertical value to appear on screen at)

Math.sin(angle)*2; (this mathematical function sets the vertical value for the position of the apple element)

Now press the play button, and move your character. You will notice that the apple moves away from the main character .

What does this do?
You are telling the computer to constantly check through a rule. The rule uses a condition, and it checks that the condition has been met before its code will run. It works a little like this;

IF ((the green man has come on) AND (the traffic has stopped))

{ now cross the road carefully }

In other words, the ‘cross the road’ part of the instruction will not take place unless the other two conditions (green man AND traffic)  have been met.

Task
Create a maze game with several ways to travel around the maze. Add some apple elements. Each apple element needs to be placed randomly around the screen. Place your character in the centre of the maze.

Now run your game - as you try to collect the apple elements they move away from you. Some may travel through the maze walls, making it harder for you to collect those elements quickly. 

Notes
If you are really unlucky, you can push the apple elements off the game screen - making them un-collectable. Try to place them away from the screen edge to avoid this.

You can find out more about this feature in this article

Posted by Xannov in Tutorials | Permalink

Reblog (0) | | Digg This | Save to del.icio.us |

Actionscript Tutorial No.2

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

This tutorial deals with creating 'monster' elements that will follow your horizontal position on screen, attempting to stop you passing.

Download 2DIY_tut2

Outline
In a 2DIY platform or maze game, you can make the challenge much harder by placing a monster element on a level that will attempt to stop you from passing by it. This is achieved by the monster following your movements with a slight delay. 

Code Explanation
2diy_tut2_img1If you right click on a monster element, you will see the options for how to make the monster element move (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 the monster follow the horizontal position of the main character (but follow with a slight delay allowing time to pass by).

Tutorial
Create several platforms on screen for a character to jump on and rise up the screen. Place a character at the bottom of the screen, and a monster on the second to top platform. (Each platform layer will need a gap within it for the character to jump up through from the previous layer).

Right click on the monster and look for the animation settings option (the second option). Choose the advanced (ADV) option and  click on the spanner icon* to open the Actionscript help editor. This editor allows you to select the code you need to use in blocks; 

Using the editor you can select the following pieces of code;

 _this (this tells the computer that it will be this element - the monster - to apply the changes to)

._x (this tells the computer the horizontal position along the screen to put the  monster element)

+= (this sets the rule of what the value of x will be)

_root.player._x  (this tells the computer to use the value of the horizontal position of your character) 

- _this._x (this subtracts any difference between the horizontal position of your character and the monster)

/ 16 ( this value can be altered and affects how long the delay is between the position of the character and the monster. The smaller the number, the less the delay in movement)

2diy_tut2_img2

*If you do not see the spanner icon, you are using an early copy of 2DIY and you will have to type the code in by hand. In this case enter the following;

this._x+= (_root.player._x - this._x) / 16;

Now press the play button, and move your character. You will notice that the monster follows where your character goes, but there is a slight delay. 

What does this do?
You are telling the computer to constantly check the horizontal position of your character, and adjust the position of the monster accordingly to make it follow your characters horizontal position.

Task
Create a platform game with several layers. Each layer needs to have gaps within it to allow your character to leap up and onto the layer above.

Place your character at the bottom of the screen, and a monster on the second to top layer. You could also add additional monsters throughout other layers,  and you will need to add an ‘apple’ element to the top most layer. 

Notes
You could add a completely impassable monster within a game if you wished - maybe combined with the previous tutorial on teleporting - to confuse a player and make them think how solve the problem faced. To make an impassable monster, you would use the code

this._x=_root.player._x;

This tells the computer that whatever the horizontal position of your character (_root.player._x) is, make the monster’s horizontal position (this._x) the same.

You can find out more about this feature in this article

Posted by Xannov in Tutorials | Permalink

Reblog (0) | | Digg This | Save to del.icio.us |

Actionscript Tutorial No.1

Here's the first in what is going to be a whole series of tutorials aimed at helping 2DIY users to use, and understand, actionscript within their creations.

This first tutorial deals with creating 'teleport' hotspots on screen that will make a character instantly jump to a new location.

Download 2DIY_tut1

Outline
In a 2DIY platform or maze game, you can create an interesting effect when your character collides with a monster. Instead of the character losing a life or losing points, you can make the character ‘jump’ to a new location. In this task you are going to make a character jump to a different part of the screen. 

2diy_tut1_img1
Code Explanation
If you right click on a monster element, you will see the options for what to do during the ‘collision’ between the monster and character (the third 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 the character move to a new location on the screen by setting the x (horizontal) and y (vertical) positions.

Tutorial
Place a character and a monster on the screen. Right click on the monster and look for the collision settings option (the third option). Choose the advanced (ADV) option and  click on the spanner icon* to open the Actionscript help editor. This editor allows you to select the code you need to use in blocks; 

2diy_tut1_img2
Using the editor you can select the following pieces of code;

 _root.player (this tells the computer what is going to be affected)

._x or ._y (this tells the computer the position along / down the screen for the action)

= (this sets the rule of what the value of x or y will be)

The final block lets you set the position value of x or y using pixels (see the notes area below for an explanation of the values of both x and y)

*If you do not see the spanner icon, you are using an early copy of 2DIY and you will have to type the code in by hand. In this case enter the following;

_root.player._x=20;

_root.player._y=440;

Now press the play button, move the character onto the monster element and see what happens.

What does this code do?
You are telling the computer that when the character (_root.player) collides with the monster element, the x and y positions of the character are to be changed. This causes the character to jump to that new location instantly. It gives the effect of a ‘teleport’.

Task
Create a platform game with two separate areas on the screen. Place your character in one area, and an ‘apple’ element in the other area. The areas can be separated by using wall elements to block the characters route to an ‘apple’ element. 

You will need to add a monster element within the first area, and change the collision effect so that your character can use it to ‘teleport’ and reach the other side of the screen.

Notes
On the 2DIY screen, the x value is 0 on the left of the screen, and 640 on the right. The y value is 0 at the top of the screen and 480 at the bottom of the screen.

You can find out more about this feature in this article

Posted by Xannov in Tutorials | Permalink

Reblog (0) | | Digg This | Save to del.icio.us |

2SimpleTV Video Guide - Making a Shape Activity

Video Tutorials from 2Simple (hosted on their 2SimpleTV YouTube channel);

2SimpleTV

These videos are copyright 2Simple, and permission has been granted to link to them from the 2DIYarchive.

Posted by Xannov in Tutorials | Permalink

Reblog (0) | | Digg This | Save to del.icio.us |

2SimpleTV Video Guide - Making a Sequencing Activity

Video Tutorials from 2Simple (hosted on their 2SimpleTV YouTube channel);

2SimpleTV

These videos are copyright 2Simple, and permission has been granted to link to them from the 2DIYarchive.

Posted by Xannov in Tutorials | Permalink

Reblog (0) | | Digg This | Save to del.icio.us |

« Previous Articles | More Articles »

Search

Recent Posts

  • 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
  • ActionScript Tutorial No.7
  • ActionScript Tutorial No.6

Recent Comments

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

Archives

  • June 2011
  • May 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010

More...

2Simple Talk

login
Follow @xannov

| The 2DIY script archive |

Design by @xannov