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


Recent Comments