all groups > flash actionscript > april 2004 >
You're in the

flash actionscript

group:

Problem with 'if' statement in 'on(press)' script



Problem with 'if' statement in 'on(press)' script Marek FlashUser
4/14/2004 9:05:27 PM
flash actionscript: Hello,
I have been working on a game using Flash MX for the past couple of months and
I have encountered a problem I cannot seem to get around. I have a number of
movie clips that are supposed to be the objects in this game and the player is
supposed to be able to drag them around and drop them in their inventory. I
actually got that to work perfectly, except then I added and 'if' condition
statement to the 'on(press){' portion of the script for one of the objects.
Instead of simply checking to see if the condition was true and then evaluating
the rest, it actually evaluated the condition, changing the value for the
variable I was testing and messing everything else up afterwards. Here is an
example:
"on (press) {
...
depth = _root.game.items+1;
...
[b]trace(_parent);
if (_parent=_root.display.icdisplay.righthand[_parent._name]) {
trace("true "+_parent);
}
trace(_parent)[/b]
}"
What would happen here is that if the condition did not evaluate to be true,
then the value of _parent on the last trace would come up as "undefined."
Here is another example.
"on (press) {
...
depth = _root.game.items+1;
...
[b]trace(depth);
if (depth = 2) {
trace("true "+depth);
}
trace(depth)[/b]
}"
In this scenario, the first trace would give the value of depth as 5. The
condition would then evaluate to true and the depth would then be given as 2
from there on out.
What doesn't make sense is that I have the exact same 'if' statement in a 'on
(releaseOutside)' portion of the script and it works fine. Any ideas and
input? All reads and responses are greatly appreciated.
Thank you.
Marek

Re: Problem with 'if' statement in 'on(press)' script mandingo
4/14/2004 10:47:39 PM
The difference is that a single ' = ' is an assignment operator... it assigns
the first part of the equation, the value of the second part.

birdColour = "blue";

The double ' == ' is the equality operator... it checks that the first part is
equal to the second.

if(birdColour == "blue"){
trace("this bird is " + birdColour);
}

Similarly you have an inequality operator ( != ) to test for things that
aren't the same.

if(birdColour != "blue"){
trace ("This bird is " + birdColour);
}

Hope that clarifies it.
cheers,
Re: Problem with 'if' statement in 'on(press)' script Marek FlashUser
4/14/2004 11:00:01 PM
Thank you mandingo. That does clarify it a lot actually. The only thing that
still doesn't make sense to me is why the single "=" works later on in the 'if'
statements that I used. I tried putting in two and it started messing things
up, actually. I'll try to figure that out on my own... later though. For now
my attitude towards it is that 'if it ain't broke, don't fix it". Hmmm... or
"if (it != "broke") { it = "dont fix"}" :-P. I love nerdiness.
Thank you again though.
Marek
Re: Problem with 'if' statement in 'on(press)' script mandingo
4/14/2004 11:22:35 PM
What you may find, where you said that it works later on in your code with a
single ' = ' is that it probably already did equal that value... so the code
didn't seem to do anything... (even though in actuality it still did the
assignment) but then continued on...

Because the if condition was never truly evaluated, (instead it treated it as
an assignment) it behaves as though it didn't exist and your code runs as
though the condition was true.

That is a common mistake in conditional statements. When you start to write
your conditions, think carefully on what outcomes you expect the condition to
return... and usually, what I do in test mode is to make the first line inside
my condition a trace ...

if(this._x >400){
trace(this._x);
// rest of conditonal logic
}

then when it runs, if I am getting unexpected behaviour, my trace should give
me a headsup on what is wrong.

good luck with your coding
AddThis Social Bookmark Button