Groups | Blog | Home
all groups > flash actionscript > march 2004 >

flash actionscript : If statement trouble... weird


MaureenM
3/22/2004 11:09:18 PM
I'm having the hardest time with the following simple if statement. My variable
comes out correctly in the trace, but regarless if the variable comes out
correct or incorrect it always returns what is in the first if statement,
regardless of which one is first. I've also tried if(eval(_level0.Q1)
=="incorrect") and it returns the same result. Even the trace inside the if
(_level0.Q1 == "incorrect") comes up as "correct". I've emulated this in a
simpler flash file and it works fine - so there is something different I'm
doing in my movie, and I've spent too many hours trying to figure it out. Can
anyone think of anything I can fix or check? The if statement is on the first
frame of a movie clip which is placed on the main timeline. The variables are
defined on the main timeline on a button as:
on (release) {
_level0.Q1 = "incorrect";
trace(_level0.Q1);
gotoAndPlay("Q1Answer");
}

Here's the if statement that's not working.....

trace(_level0.Q1);
if (_level0.Q1 == "incorrect") {
trace(_level0.Q1);
trace("This shows even if the _level0.Q1 is correct");
gotoAndStop("incorrect");
} else {
if (_level0.Q1 == "correct") {
gotoAndStop("correct");
}
}
stop();


Many many thanks to anyone who can help
Maureen

pazzoboy
3/23/2004 2:59:41 AM
Another issue might be that you don't have Q1 on level0. Is it actually just
on the main timeline? If so, you could use _root instead. Also, have you
checked to see if there is anything else in the movie that might be setting Q1
to incorrect....perhaps a movieclip hidden behind something else or just
offstage? Or...you could try using booleans for these:
on (release) {
_level0.Q1 = true;
gotoAndPlay("Q1Answer");
}
//-----------
if (_level0.Q1 == false) {
gotoAndStop("incorrect");
} else {
gotoAndStop("correct"); //that second "if" is probably not
necessary either
}
stop();
Jeckyl
3/23/2004 10:52:38 AM
If could be that there are characters like spaces or tabs or newlines that
you cannot see also in the string. Then it would LOOK ok in the trace, but
still not exactly match what you want. Try tracing it with

[quoted text, click to view]

then if you see anything other than just the word [correct] (eg any spaces
etc in between the []) you'll know there is something wrong.

[quoted text, click to view]

MaureenM
3/23/2004 3:31:02 PM
Hi Jecky,
I've tried your trace("[" + _level0.Q1 + "]");
and it's returning a "0" - the 0.

Any ideas what that means?

pazzoboy
3/23/2004 4:17:51 PM
Maureen, maybe I'm not reading into your question well, or I'm missing
something, but here is what I did:
on the Main timeline, I put just this:
varQ1;
----------------
On one button, I put this:
on (release) {
_level0.Q1 = "correct";
if (_level0.Q1 == "incorrect") {
trace("this is wrong: " + _level0.Q1);
} else {
trace("This is right: " + _level0.Q1);
}
}
----------
On the other button, I put this:
on (release) {
_level0.Q1 = "incorrect";
if (_level0.Q1 == correct") {
trace("this is wrong: " + _level0.Q1);
} else {
trace("This is correct: " + _level0.Q1);
}
}

The above code is working. I'm back to where I said before...there is
probably something setting your variable even after you press the button. One
common error is to assign a value to the variable like this on the main
timeline:
var Q1="correct";

The above code will keep setting Q1 to correct, every time flash runs through
the frame. A better way would be:
this.onLoad=function(){varQ1="correct"};
It is set one time, and the buttons set it from there on.
Hope this helps!
AddThis Social Bookmark Button