PDA

View Full Version : If else a button is press


sharonj
01-07-2011, 12:23 AM
I have been trying all day to learn passing variables and if else statements and have gotten nowhere. So, please if you a generous enough to help me don't assume I understand this type of code. There is something very basic about it I am missing.

This is my problem:

There is a button on my website. When the button is pressed an event happens (gotoAndPlay...).

When another button is pressed, I want to be able to tell that that event happened. If the event happened, then gotoAndPlay..., if the event did not happen, then gotoAndStop...

How do I do that?

codeman22
01-09-2011, 09:40 PM
You need to declare a variable at the top of your code so the second button has access to it.

So something like this:

var condition:String="itdidnothappen";

button1.addEventListener(MouseEvent.CLICK, button1Handler);
button2.addEventListener(MouseEvent.CLICK, button2Handler);

function button1Handler(e:MouseEvent):void{
gotoAndPlay(insertframenumberhere)
condition="ithappened";
}

function button2Handler(e:MouseEvent):void{
if (condition=="ithappened"){
// actions here if button1 had been pressed
}else{
//actions here if button1 had not been pressed
}

}

depending on what your app is doing you may need to reset the variable.

Hope this helps,
codeman22

sher75
01-20-2011, 03:58 AM
You need to declare a variable at the top of your code so the second button has access to it.

So something like this:

var condition:String="itdidnothappen";

button1.addEventListener(MouseEvent.CLICK, button1Handler);
button2.addEventListener(MouseEvent.CLICK, button2Handler);

function button1Handler(e:MouseEvent):void{
gotoAndPlay(insertframenumberhere)
condition="ithappened";
}

function button2Handler(e:MouseEvent):void{
if (condition=="ithappened"){
// actions here if button1 had been pressed
}else{
//actions here if button1 had not been pressed
}

}

depending on what your app is doing you may need to reset the variable.

Hope this helps,
codeman22

that works but i would like to ask a few very simple and maybe quite stupid questions which are
What does :void do?
Why the use of var instead of (what i had learned) function?
Are those 2 similar at all or am i confused?
thank you for your time.

blank01
01-20-2011, 05:17 AM
:void is just good practice.. When a function is called it is like telling your employee to do a job. Either they do the job or they do the job and bring you back something. Functions can either do something or they can do something and then "return" something. Most functions don't return anything, they just do their jobs and that is it, so they return nothing, or "void". Sometimes (and you will use this often if you continue learning a bit about coding) they will return a value. Maybe it is a string of characters, so it would be "function myFunction(e:Event):String". Functions that come back with a certain something are called like this:

function learnAS():Number {
var a:Number = 3.42;
var b:Number = 2737.2674;
var bringBack:Number = a+b;
return(bringBack);
}

var aPlusB:Number = learnAS();
trace(aPlusB);

automator
01-20-2011, 10:12 AM
Why the use of var instead of (what i had learned) function?
Are those 2 similar at all or am i confused?


Variables (var) and functions are different things.

A function is a block of code that you have to call in order for it to run:
function someFunction():void
{
/* Do something.
This could be anything.
It will contain lines of code that will do whatever you need Flash to do.
For example: */

trace("hello there!");

/* All this does is display a message in the output window.
A function can have one line inside it, or it can have more. */
}

Using blank01's analogy, the function body (the lines of code inside the curly braces) would be the job that you want the person to do. You will need to call this function for it to run. Calling the function would be like calling that person and telling that person to do the job. This would be the function call:
someFunction();
You just type the name of the function, followed by parentheses (this is called the function call operator). And then end the statement with a semi-colon. Without the function call, the function will just sit there and do nothing... like a lazy employee.

Variables would be containers of data. You use the var keyword to declare a variable. When you declare a variable, you give it a variable name, which should be a name that's unique (meaning no other object within the same scope should have the same name):
var john:MovieClip = new MovieClip();
/* This creates a new movie clip. Its name is john. */

var mary:TextField = new TextField();
/* This is mary the text field */

These objects need names. Otherwise you won't be able to communicate with them. You can't just say, "hey, you!" They will ignore you. You must call them by name. Coz it's rude not to. lol :) And of course you probably want to use names that are a little more descriptive like inputText or circle_mc as opposed to john and mary.

sher75
01-20-2011, 02:29 PM
That was quite alot that i understood in mere seconds thank you both for your help. Thats a great analogy too ;). I will start playing around with these codes to get the hang of it better.