PDA

View Full Version : What is the correct way of structuring IF statements?


p0c
02-25-2008, 10:39 AM
I recently had this problem but decided to make a new thread seeing as though I have fixed my original problem but in the typical flash way, ran straight into another

I have this function that is called fairly often throughout my animation...
function pauseMovieClips(pauseAll) {

stop();

if (stage.contains(skimmerWorker)){
trace("Skimmer Worker Stopped");
skimmerWorker.stop();
}

if (stage.contains(cloud)){
trace("Cloud Stopped");
cloud.stop();
}

if (stage.contains(wholeScreen)){
trace("Skimmer Worker Nested Stopped");
wholeScreen.skimmerWorker.stop();
}

if (stage.contains(warmAnim)){
trace("Warm Animation Stopped");
warmAnim.stop();
}


}


However it seems if the first movie clip isn't contained on the stage at that moment in time it seems to ignore all other contiguous if statements!? Almost like it has a break in it. Am I structuring this correctly to run one if statment whether it is true or false, i need it to go onto the next ones.

This is an example of what I get, when skimmerWorker is present and cloud (the next one) isn't...

Skimmer Worker Stopped
TypeError: Error #2007: Parameter child must be non-null.
at flash.display:: DisplayObjectContainer/contains()
at SiteSetUp_fla::MainTimeline/pauseMovieClips()
at SiteSetUp_fla::MainTimeline/cBarPause()

Even if warmAnim is present it will not stop.

Can anyone help me here, cheers!

Billy T
02-25-2008, 12:46 PM
its looks like one (or more) of you vars is set to null

cloud
wholeScreen
etc

need to equal something - even if it is not in the display list. One way around this is to check if the var is null and proceed if it isn't

eg

if(cloud!=null){
if (stage.contains(cloud)){
trace("Cloud Stopped");
cloud.stop();
}
}

should fix it. The structure of your if statements looks fine

cheers

GordonG
02-25-2008, 12:51 PM
I'm new to AS3, but the way I understand it ( and now understand your problem ) you cannot pass a non null value to contains(). Methods like this used to silently fail in AS2, but now it breaks the whole script. You must first check to make sure that your children to indeed exist before you call contains. so first make sure if(SkimmerWorker ! = null)

... and maybe that's all you need to test for anyway? ( since if it's null it definitely isn't contained by anything...)

p0c
02-25-2008, 01:04 PM
Gordon your a genius!! That sorted it nicely instead of ...

if (stage.contains(skimmerWorker)){
trace("Simmer Worker Stopped");
skimmerWorker.stop();
}

i simply use...
if (skimmerWorker != null){
trace("Skimmer Worker Stopped");
skimmerWorker.stop();
}

...That was sorted out very nicely, I've been stuck on this all day at work!!

GordonG
02-25-2008, 01:21 PM
Billy T had the right idea too - he's been spot on!