Pages

Tuesday, January 5, 2010

the freaking if conditional statement

Not sure how I can explain the situation in a professional way, I'll just explain it my way then...

Here I have a circle, and it's own properties:
Condition 1: If the size of the circle MovieClip is less than a certain amount, say 25p (pixel) by 25p, it'll turn red.

Condition 2: If the width of the MovieClip is between 25p and 45p, and it's height is higher than 25p, it;'ll turn orange.

Condition 3: If the size of the MovieClip is higher than 150p by 150p, it'll turn yellow.

So, while I was working on it using the IF conditional statement, I mis-insert an "else" at Condition 2, which causes the Condition 1 to stop working. It seems that if all the conditions are related, and "if" is inserted instead of "else if" in front either the 2nd or 3rd condition, will cause the 1st condition to stop working.

Below is example of what I mean:

Without "else":



Scripts:

function colourChange(evt:Event):void
{
if (pointer.width <= 35 && pointer.height <= 35)
{
pointer.gotoAndStop("red");
}
if (pointer.width <= 45 && pointer.height >= 35)
{
pointer.gotoAndStop("orange");
}
else if (pointer.height <=45 && pointer.width >= 35)
{
pointer.gotoAndStop("orange");
}
else
{
pointer.gotoAndStop("yellow");
}
}


With "else":


Scripts:

function colourChange(evt:Event):void
{
if (pointer.width <= 35 && pointer.height <= 35)
{
pointer.gotoAndStop("red");
}
else if (pointer.width <= 45 && pointer.height >= 35)
{
pointer.gotoAndStop("orange");
}
else if (pointer.height <=45 && pointer.width >= 35)
{
pointer.gotoAndStop("orange");
}
else
{
pointer.gotoAndStop("yellow");
}
}

No comments: