Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

"if", "else" and "if else" statements

Contributor ,
Sep 27, 2006 Sep 27, 2006
Ok I'm not sure if i've stuffed this up but this is my 1st go at trying to utilise the "if", "else" and "if else" statements, after going through it in the "Actionscript for Flash 8" book.

What I have is a science student character standing outside a lab door. Hanging on a wall beside him is a coat and some goggles. Now the user can click on the coat, goggles or door. Depending on what people click on will send them off to different animations.

And so here is what AS I have:

stop();

coatHangerBtn._visible = 0;
charGogglesBtn._visible = 0;
charCoatBtn._visible = 0;

charGogglesHangBtn.onRelease = function() {
charGogglesHangBtn._visible = 0;
charGogglesBtn._visible = 1;
var charGoggles:Boolean = true;
trace(charGoggles);
};
charGogglesBtn.onRelease = function() {
charGogglesHangBtn._visible = 1;
charGogglesBtn._visible = 0;
var charGoggles:Boolean = false;
};
charCoatHangBtn.onRelease = function() {
charCoatHangBtn._visible = 0;
charCoatBtn._visible = 1;
charClothesBtn._visible = 0;
var charCoat:Boolean = true;
};
charCoatBtn.onRelease = function() {
charCoatHangBtn._visible = 1;
charCoatBtn._visible = 0;
charClothesBtn._visible = 1;
var charCoat:Boolean = false;
};

var charGoggles:Boolean = false;
var charCoat:Boolean = false;

labDoorBtn.onRelease = function() {
if (charGoggles && charCoat) {
gotoAndPlay("correct");
} else if (charGoggles) {
gotoAndPlay("goggles");
} else if (charCoat) {
gotoAndPlay("coat");
} else {
gotoAndPlay("nothing");
}
labDoorBtn.gotoAndPlay(2);
};
TOPICS
ActionScript
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Sep 28, 2006 Sep 28, 2006
use the following:

Translate
LEGEND ,
Sep 27, 2006 Sep 27, 2006

"twistedpancreas" <webforumsuser@macromedia.com> wrote in message
news:effm1i$68e$1@forums.macromedia.com...
> Ok I'm not sure if i've stuffed this up but this is my 1st go at trying to
> utilise the "if", "else" and "if else" statements, after going through it
> in
> the "Actionscript for Flash 8" book.
>
> What I have is a science student character standing outside a lab door.
> Hanging on a wall beside him is a coat and some goggles. Now the user can
> click
> on the coat, goggles or door. Depending on what people click on will send
> them
> off to different animations.
>


Without going through all of the code.. this did jump out at me.

> labDoorBtn.onRelease = function() {

Here you say if charGoggles evaluates to true... goto and play from frame
label correct.
the you say, else if charGoggles evaluates to true.... goto and play
goggles.

That is like saying:
if (2 + 2 == 4) {
do something;
} else if (2 + 2 == 4) {
do something else;
}

the 'do something else' will never get evaluated.

> if (charGoggles) {
> gotoAndPlay("correct");
> } else if (charGoggles) {
> gotoAndPlay("goggles");
> } else if (charCoat) {
> gotoAndPlay("coat");
> } else {
> gotoAndPlay("nothing");
> }
> labDoorBtn.gotoAndPlay(2);
> };
>

Also, further up in the onRelease handlers you are setting up a local
variable charGoggles when you use the line
var charGoggles:Boolean = whatever;

instead, defined charGoggles up where you define
charCoatBtn._visible = 0; // like
var charGoggles:Boolean = false;

Then, in the onRelease handlers you can use charGoggles without the var and
dataType.



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2006 Sep 28, 2006
no, the logic's ok. the first conditional is testing if both booleans are true. that will resolve to false and one of the "else if" statements will resolve to true if one condition is true and one is false.

addendum: i see twisted edited his original message and corrected the problem smb identified.

the problem is making those booleans local to the buttons so they are undefined (or false) when the if-statements execute.

to remedy, remove the prefix "var" that precedes your booleans.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 28, 2006 Sep 28, 2006
The logic is not ok, SMB is correct. The code will never get to frame
"goggles" because if charGoggles is true, you will always, and only, get to
frame "correct". Not sure what the OP is trying to do, as there was no
actual question, but it will need to be done a bit different.

--
Dave -
Adobe Community Expert
www.blurredistinction.com
http://www.adobe.com/communities/experts/


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 28, 2006 Sep 28, 2006

"kglad" <webforumsuser@macromedia.com> wrote in message
news:effti4$e20$1@forums.macromedia.com...
> no, the logic's ok. the first conditional is testing if both booleans are
> true. that will resolve to false and one of the "else if" statements will
> resolve to true if one condition is true and one is false.
>
> the problem is making those booleans local to the buttons so they are
> undefined (or false) when the if-statements execute.
>
> to remedy, remove the prefix "var" that precedes your booleans.
>kglad,

Forgive my ignorance here... but regarding the onRelease handler below:

labDoorBtn.onRelease = function() {
if (charGoggles) {
gotoAndPlay("correct");
} else if (charGoggles) {
gotoAndPlay("goggles");
} else if (charCoat) {
gotoAndPlay("coat");
} else {
gotoAndPlay("nothing");
}
}

How is the first conditional testing to see if both are true? It looks to
me like the first test is only against charGoggles and not anything else.

Thanks



Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2006 Sep 28, 2006
the problem is because of this forum and the difference between users that use the internet and the newsgroup to view this forum. i'm seeing twisted's edited message which has a correction (that i noted in my addendum above).

newsgroup users don't see the edited message and smb responded to twisted's message before the correction was made by twisted. i'm pretty sure smb should only see the edited message if he re-reads twisted original post.

here's what i saw when i responded and what i still see:

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 28, 2006 Sep 28, 2006
Geez.. more lame forum problems. Wonder if they will _ever_ get the forum
working properly.

--
Dave -
Adobe Community Expert
www.blurredistinction.com
http://www.adobe.com/communities/experts/


Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 28, 2006 Sep 28, 2006
yeah sorry guys,

i saw that mistake at the last minute and changed it from

if (charGoggles) {

to

if (charGoggles && charCoat) {

And sorry I should have asked why my code isn't getting past

if (charGoggles && charCoat) {
gotoAndPlay("correct");

if only the goggles have been selected

Kglad, when you say "to remedy, remove the prefix "var" that precedes your booleans." won't this cause errors? and do u mean remove the "var" that are in the button's code or the "var" for

var charGoggles:Boolean = false;
var charCoat:Boolean = false;

or should i remove these 2 lines completely (which i tried and didnt work)?

thanks for you're help so far guys and sorry to those not using the forum, i wont edit my posts anymore
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 28, 2006 Sep 28, 2006
use the following:

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Sep 28, 2006 Sep 28, 2006
ah of course,

gee i hate being a noob :D

for some reason i thought you had to include the boolean statement when setting the var to the other boolean setting.

thanks kglad you're a Lifesaver

😄
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 29, 2006 Sep 29, 2006
LATEST
you're welcome.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines