PDA

View Full Version : Stuck with complex if conditions :S Need Help Here...



swerheatz
July 8th, 2009, 08:14 AM
I am trying to make an educational script and I must use conditionals to detect how my movieclips are gonna act. I have two buttons and if they are clicked their related variable equals to 1. if not pressed their variables are 0. I put a button to see their state (if 1 or 0) and trace the variables. I am sending Fla to check if I am making any mistakes..

:(:wasted::wasted:

even I click different buttons to change the variables, I always get the same trace...



var released:Number;
var pump_situation:Number;

serbestbirak.addEventListener(MouseEvent.CLICK, serbestbirakma);
function serbestbirakma(event:MouseEvent):void {

if (event.type=="click"){
released=1;
}
else{

released=0;
}
}

pump_outside.addEventListener(MouseEvent.CLICK, disari);
function disari(event:MouseEvent):void {

if(event.type=="click")
{
pump_situation==1;}
}
pump_inside.addEventListener(MouseEvent.CLICK, iceri);
function iceri(event:MouseEvent):void {

if(event.type=="click")
{
pump_situation=0;}

}

tracing.addEventListener(MouseEvent.CLICK, trascing);
function trascing(event:MouseEvent):void
{
if ((pump_situation==1) && (released!=1))
{
trace("pump_situation1, released is not 1 ");

}
if ((pump_situation ==1) && (released==1))
{
trace("pump_situation 1 released 1");
}

if ((pump_situation ==0) && (released==1))
{
trace("pump_situation 0 released 1");
}
if ((pump_situation ==0) && (released!=1))
{
trace("pump_situation 0 released is not 1");
}
else
{
trace("none of them");
}
}

IQAndreas
July 8th, 2009, 10:21 AM
Remove the semicolon after your if statements. :) That is a REALLY common mistake.



if(event.type=="click");{
pump_situation=0;}


to


if(event.type=="click"){
pump_situation=0;}


Also, I find it easier for one line if functions to display it like this, but It's just a personal thing, and won't make a hill of beans difference:


if(event.type=="click")
{ pump_situation=0; }



Also, this might work better to avoid spelling errors:


if(event.type==MouseEvent.CLICK)
{ pump_situation=0; }

swerheatz
July 8th, 2009, 12:22 PM
Thanks Andreas but Still not giving what I want...
Did you check my fla file?

Any other suggestions?

m90
July 8th, 2009, 02:06 PM
I think your mistake is at line 22 where you use "==" instead of "=". You compare the values instead of assiging a new value.

Should read:


pump_situation = 1;

Two more things I noticed:
Inside the event handlers you always check if the event is "click". You don't need to do this. If the event listener listens only for MouseEvent.CLICK there is absolutely no way that the passed event will not be "click".
Second I guess you should check out Booleans for describing your states. Instead of using a number that you set to 0 or 1 you use a Boolean that can only be true or false. Which is a lot easier to handle as you cannot make all the mistakes you can make with numbers.

IQAndreas
July 8th, 2009, 02:15 PM
Found it.

If you line up if statements in a row like that, Flash has no way of knowing that they go together. Basically, what your code did was:
* Run the first if statement, and since it was false, and there was no "else", it kept on going.
* Then it found the second if statement. This may have been true, so it would trace the value "pump_situation 1 released 1"
* Then it found the third if statement. This was false, and there was no "else", so it kept on going.
* Finally, if found the fourth if statement. This was false as well, but since there was an else statement, it traced out "none of them".

Understand?


If you want several ifs in a row to use the same else statement, use "else if", as shown below:

function trascing(event:MouseEvent):void
{
if ((pump_situation==1) && (released!=1))
{
trace("pump_situation1, released is not 1 ");
}
else if ((pump_situation ==1) && (released==1))
{
trace("pump_situation 1 released 1");
}
else if ((pump_situation ==0) && (released==1))
{
trace("pump_situation 0 released 1");
}
else if ((pump_situation ==0) && (released!=1))
{
trace("pump_situation 0 released is not 1");
}
else
{
trace("none of them");
}
}

Also, you can embed if statements to make the code run a little faster, and sometimes becomes more readable:

function trascing(event:MouseEvent):void
{
if (pump_situation==1)
{
if (released==1)
{
trace("pump_situation 1 released 1");
}
else
{
trace("pump_situation1, released is not 1 ");
}
}
if (pump_situation==0)
{
if (released==1)
{
trace("pump_situation 0 released 1");
}
else
{
trace("pump_situation 0, released is not 1 ");
}
}
else
{
trace("none of them");
}
}


A few pointers, the code might run faster if "released" is a Boolean of true or false, but I'm not sure if you have plans for that variable later.

It seems as though you just learned AS3, and have past AS2 experience. If you are going to use whole numbers for "pump_situation" and "released", try using "int" or "uint" or any of the other data types that were introduced in AS3. I can give you a few links if you need them.

Finally, if you add event listeners for the click event, you will only receive click events to that function, so the extra "if (event.type=="click")" is actually unnecessary. Of course, if you plan on using that function for more than just the click event, than what you have done is a good idea.

pump_inside.addEventListener(MouseEvent.CLICK, iceri);
function iceri(event:MouseEvent):void {
pump_situation=0;
}


If you have any more questions, or need me to clarify more, just ask.
Happy coding!

swerheatz
July 8th, 2009, 03:17 PM
Thank you for your helps :D