Adobe Edge variables, counting coins [EN]
To create an interactive environment in Edge Animate, we need to have some code. And one of the most important things are variables. To declare a variable, select your stage and click on “open actions” button near the title of your project (properties). Select “creationComplete” and add all the following code below:
sym.setVariable("varName", 0); |
Where “varName” is your variable name. The number behind that is the start value of this variable.
When you have a coin you can collect, select the coin symbol and click on “open actions” in the properties window.
Add the following code:
// lets get the variable "varName" and put it into another variable named "Name" var Name = sym.getVariable("varName"); // this is the important part, counting. Add 1 to the value of the variable "Name" Name = Name + 1; |
This code gets the variable name varName and puts it in a new var “Name”. After that we add a plus 1 to this variable, so we know that someone has clicked on the coin and that we can keep track of it.
sym.setVariable("varName", Name); sym.$("TextField").html(Name); |
We want to keep counting, so we have to put the 1 back in our global variable. After that we want to show the variable in a textfield. This textfield has an id “TextField”.
// when you have clicked on the coin, make it disappear with a fadeout sym.$("coin").fadeOut(200); // if we have clicked on 3 coins, show a symbol. if (Name ==3){ sym.$("CongratsSymbol").show(); } |
When the user has clicked on the coin, it has to disappear. That’s where we use the “fadeOut” function. The number between the brackets is how long the fade can take.
If the user has clicked on 3 coins, we want to show a symbol. So that’s where the check if the varName is 3 comes into place. Use the same code for the other 2 coins and be sure to use other id’s for the coins.
This is one way to do this, there could be others. If you have another way to do the same, please let me know.
Another way to do this is:
This code has to be placed on the stage. Important is that every coin has a “class name”, coin. At the properties, in the top right you will find a button with a “s”.
// insert code to be run when the symbol is created here
var numberofCoins = 0;
//when the user clicks on a coin (class has to be coin)
$(“.coin”).click( function (){
//this says it has to remove the clicked coin
this.remove();
//numberofCoins++ ads 1 to the variable
numberofCoins++;
//with sym. we call on a unique element. Text places the var in a textfield
sym.$(“amount”).text(numberofCoins);
//testing if numberofCoins is 3
if (numberofCoins == 3){
//if it’s true show the congrats screen, hide for hiding
sym.$(“congrats”).show();
}
})