Groups | Blog | Home
all groups > flash actionscript > september 2005 >

flash actionscript : Multi conditional IF statement?


J_Greene
9/21/2005 9:44:59 PM
i have 3 MovieClips i'm try'n to manipulate with 2 buttons.
2 are start forward. simple ._visible = true/false;

the third i want to only show up when both are visible and yet go away when
one of the first two is turned off.

how do i script a muli-conditional if statement into the 2 buttons that
control them?

mandingo
9/21/2005 10:16:41 PM
so... how do you want to do it...

btn_1.onRelease = function(){
clip1._visible = !clip1._visible ;
if(clip1._visible = true && clip2._visible == true){
clip3._visible = true;
}else{
clip3._visible = false;
}
}
btn_2.onRelease = function(){
clip2._visible = !clip2._visible ;
if(clip1._visible = true && clip2._visible == true){
clip3._visible = true;
}else{
clip3._visible = false;
}
}

now that will do it, but there are other ways...

cheers
NSurveyor
9/21/2005 10:44:36 PM
Or, (without the ifs):

btn_1.onRelease = function(){
clip3._visible = (clip1._visible = !clip1._visible) && clip2._visible;
}
btn_2.onRelease = function(){
clip3._visible = (clip2._visible = !clip2._visible) && clip1._visible;
}

NSurveyor
9/21/2005 10:46:12 PM
mandingo
9/21/2005 10:49:50 PM
See what happens when the coffee machine is broken...

mandingo
9/21/2005 11:13:47 PM
lol, the odd thing was I was going to say that when I did it... and I wouldn't
do it like that myself normally... then I went to get my coffee and was going
to come back and try... only to get told that no coffee and it should be fixed
by lunch... LUNCH!!! what are they thinking...

then I came back to see what I was thinking... threw that code into a window
and it didn't work and I couldn't even see my own error... I then checked for
other hot water devices on this floor...

I have found one now... and am sorry for any decaf errors.
Jeckyl
9/22/2005 12:00:00 AM
Two mistakes there...
[quoted text, click to view]

should be == ... not =

[quoted text, click to view]


should be == ... not =

[quoted text, click to view]

And overall a quite inefficient and complicated way to do it.

Other better ways would include:

btn_1.onRelease = function() {
clip1._visible = ! clip1._visible ;
clip3._visible = clip1._visible && clip2._visible;
}
btn_2.onRelease = function() {
clip2._visible = ! clip2._visible ;
clip3._visible = clip1._visible && clip2._visible;
}

which is much simpler and better all round.
--
Jeckyl

Jeckyl
9/22/2005 12:00:00 AM
mandingo: I won't ask what your other hand is clutching :):):):):)

BTW: the code from NSurveyor ...

[quoted text, click to view]
btn_1.onRelease = function(){
clip3._visible = (clip1._visible = !clip1._visible) && clip2._visible;
}
btn_2.onRelease = function(){
clip3._visible = (clip2._visible = !clip2._visible) && clip1._visible;
}
[quoted text, click to view]

.... is indeed shorter and "clever", BUT it is more complicated and not as
good overall. Assignment statements within an expression are VERY VERY bad
style.

The reason .. when you (or someone else) comes back later to maintain this
code, you'll see the line:

clip3._visible = (clip2._visible = !clip2._visible) && clip1._visible;

and immediately think: "Damn .. who wrote this, they should know you need an
== instead of an =" and "fix" the line to be:

clip3._visible = (clip2._visible == !clip2._visible) && clip1._visible;

and then sometime later, you'll look at it again and say "That's dumb .. how
can visible and not-visible ever be equal .. that's always going to be
false" and "fix" the line to be:

clip3._visible = false && clip1._visible;

and then immediately realise the doing an && with false will ALWASY give you
false, and so it gets optimised down to:

clip3._visible = false;

It is best to keep assignments as separate statements .. not ever put them
in the middle of an expression where they looks 'wrong' .. even if it may be
'clever' and concise to do so.

Long code can be complicated, short code can be complicated .. in between
there is an optimum simplicity and clarity. Concise enough not to be
overwhelming, and verbose enough to be easily understood and unambiguous.
Finding that balance is part of the art of programming.
--
Jeckyl

NSurveyor
9/22/2005 12:38:19 AM
Jeckyl, I agree with you completely. In fact, when I was reading your post, and
you said, "Damn .. who wrote this, they should know you need an == instead of
an =" and "fix" the line to be", I looked back at my code and thought I made an
error... but soon I realized that it works as it should. ;)

I was just showing an alternative, the very "clever" way... in fact, I've made
a hobby out of taking long code and making extremely short. For example, using
sort() to shuffle an array... very slow compared to the "shuffle" method.
AddThis Social Bookmark Button