PDA

View Full Version : How to name instances of Boolean values



setzer9999
April 19th, 2008, 09:42 PM
Hello, I had a good response on a problem from user jon_bla on a previous issue where he introduced me to using Boolean values.

My problem is that I want to use Boolean values to create functionality for buttons to allow the movie to skip to specified frames based on which button is pressed.

Currently I have three buttons.

Each button precedes a motion tween that continues for a few frames.

1. I placed the Boolean var like jon_bla indicated on each button. Each button has the following code:


on(press){
gotoAndPlay(2);
var skip:Boolean = new Boolean();
skip=true;
}

2. The tween animation begins on frame 2 and ends on frame 10

3. On frame 10 I have the following code in the actions layer:


stop();
if(skip == true){
gotoAndPlay(21);
}

if(skip == true){
gotoAndPlay(28);
}

if(skip == true){
gotoAndPlay(35);
}


4. Obviously, I realize that this code is incorrect. What I want to know is how to code each button on frame 1 to a different instance of a Boolean value. In other words, I want button 1 to take you to frame 21 after completing the tween, button 2 to take you to frame 28 after completing the tween, and button 3 to take you to frame 35 after completing the tween.

What is the syntax for naming and referencing a Boolean value?

glosrfc
April 19th, 2008, 09:55 PM
Because they're variables, you can give them any name that makes sense. However, as you've discovered, you do have to keep track of whether the variable is flagged as true or false.

You can use different variables for each button, e.g. skip1, skip2, and skip3. Then your code on frame 10 would read:

stop();
if(skip1 == true){
gotoAndPlay(21);
}

if(skip2 == true){
gotoAndPlay(28);
}

if(skip3 == true){
gotoAndPlay(35);
}

One of the problems with that method is that the variables are initialised in different timelines (i.e. they're declared in the timelines of the individual buttons) so you might run into scoping issues. In that case you will have to target the variables, e.g. if btn1.skip1 == true

An easier option is to simply copy the 8 frames containing your tween into frames 28 and 35 and do away with the boolean variable altogether.

glosrfc
April 19th, 2008, 10:02 PM
Another option is to store the desired frame number rather than a boolean value:


on(press){ // code attached to button 1
frameNo = 21;
gotoAndPlay(2);
}
on(press){ // code attached to button 2
frameNo = 28
gotoAndPlay(2);
}
on(press){ // code attached to button 3
frameNo = 35
gotoAndPlay(2);
}
gotoAndPlay(frameNo);// code in frame 10

setzer9999
April 19th, 2008, 10:15 PM
Thank you very much glo.

I learned something about buttons I didn't even know to ask from your second post.

I want to use the boolean values even if it is not the best way, because I am trying to learn to use code instead of just animations to do things, and I want to expand this movie to have dozens of buttons and I don't want to copy and paste hundreds of frames. Thank you so much for your responses so quickly. This helped me out a great deal.

glosrfc
April 19th, 2008, 11:02 PM
No problem. If you're planning on adding dozens of buttons, I'd definitely consider abandoning a boolean value and just storing the target frame number in a variable instead. If you use the former method, you not only have to edit the code in every button, but you will also have to include a load more if statements in frame 10.