Copy link to clipboard
Copied
If I have funcitons that are based on boolean conditions, normally I would set up a bunch of boolean variables for each condition and then have funciton check to see which ones are true, but what if I have a a bunch of variables? Then I would have to manually switch which ones are true or false a whole lot, and I feel like there is a better way using arrays or something. Any suggestions on I would do this?
Example:
var condition1:Boolean;
var condition2:Boolean;
var condition3:Boolean;
oneBtn.addeventListener(MouseEvent.CLICK, dostuff);
function doStuff(event:MouseEvent) {
gotoAndStop("newFrame");
condition1=true;
condition2=false;
condiition3=false;
}
twoBtn.addeventListener(MouseEvent.CLICK, dostuff2);
function doStuff2(event:MouseEvent) {
gotoAndStop("newFrame2");
condition1=false;
condition2=true;
condiition3=false;
}
etc…
Then on the frame "newFrame"
If (condition1) {
//do function
}
If (condition2) {
//do function
}
If (condition3) {
//do function
}
Is there a way I could do this with an array or something. If I had 12 variables could I say myArray[0,1,2,3,4,5,6,7,8,9,10,11] and tie the variables to each number and then on the buttons just tell it to set a number 5 to true and make everything else false.
I am just trying to simply my code, I know a little about Arrays dont really use them, and if they could help I want to learn how to make them work in my favor.
Copy link to clipboard
Copied
You should assign the true/false status before you call the gotoAndStop command.
If there will always only be one value assigned as true, then you could create a function that assigns all the players to be false, and call that function first, then set the one that is to be true to true. If your buttons determine which value is to be set true, then you could probably just have one function for all the buttons to share and determine which button was clicked using the event.currentTarget
Copy link to clipboard
Copied
I don't think this is possible to give a solid advice not knowing more about your application and what you are trying to achieve.
However, it feels like you are trying to write/read application state(s). You approach would make sense only if several Boolean flags can be true at the same time. For example, if there is a state when, say, condition1 is true and condition3 is true.
Otherwise you, perhaps, are better off with using a single variable that hold value of the state.
For example:
var state:String = "whateverState";
oneBtn.addeventListener(MouseEvent.CLICK, dostuff);
function doStuff(event:MouseEvent)
{
gotoAndStop("newFrame");
state = "anotherState";
processState();
}
twoBtn.addeventListener(MouseEvent.CLICK, dostuff2);
function doStuff2(event:MouseEvent)
{
gotoAndStop("newFrame2");
state = "oneMoreState";
processState();
}
function processState():void
{
switch (state)
{
case "whateverState":
// do whatever
break;
case "anotherState":
// do something else
break;
case "oneMoreState":
// do thir thingy
break;
}
}
Copy link to clipboard
Copied
Or better yet:
var _state:String = "whateverState";
oneBtn.addeventListener(MouseEvent.CLICK, dostuff);
function doStuff(e:MouseEvent)
{
gotoAndStop("newFrame");
state = "anotherState";
}
twoBtn.addeventListener(MouseEvent.CLICK, dostuff2);
function dostuff2(e:MouseEvent)
{
gotoAndStop("newFrame2");
state = "oneMoreState";
}
function get state():String
{
return _state;
}
function set state(value:String):void
{
_state = value;
processState();
}
function processState():void
{
switch (state)
{
case "whateverState":
// do whatever
break;
case "anotherState":
// do something else
break;
case "oneMoreState":
// do thir thingy
break;
}
}
Copy link to clipboard
Copied
And to make it even more dynamic - you can create linkage of buttons to particular functionality and process everything in fewer functions:
import flash.display.DisplayObject;
import flash.utils.Dictionary;
var _state:String = "whateverState";
var buttons:Array = [oneBtn, twoBtn, threeBtn];
// links states to buttons
var clickStates:Dictionary = new Dictionary();
clickStates[oneBtn] = {state: "whateverState", frame: "newFrame"};
clickStates[twoBtn] = {state: "anotherState", frame: "newFrame2"};
clickStates[threeBtn] = {state: "oneMoreState", frame: "newFrame3"};
activateButtons();
function activateButtons():void
{
for (var key:Object in clickStates)
{
DisplayObject(key).addEventListener(MouseEvent.CLICK, handleStateChange);
}
}
function handleStateChange(e:MouseEvent)
{
var data:Object = clickStates[e.currentTarget];
gotoAndStop(data.frame);
state = data.state;
}
function get state():String
{
return _state;
}
function set state(value:String):void
{
_state = value;
processState();
}
function processState():void
{
switch (state)
{
case "whateverState":
// do whatever
break;
case "anotherState":
// do something else
break;
case "oneMoreState":
// do thir thingy
break;
}
}
Copy link to clipboard
Copied
Thanks I am defintitly gonna start doing this stuff in the future. Using strings instead of booleans has crossed my mind but for some dumb reason, though using booleans were better.
Copy link to clipboard
Copied
Just for the future references. If there are more than two states of anything, employing of boolean properties is a pretty cumbersome way to manage use case at least for two reasons.
Reason 1 is that it is quite inorganic to use what has only two states by design to describe more than two positions. Your case illustrates this point well. What happens in these approaches is that programmer gets into a situation when growing number of parameters must be kept in mind simultaneously as a single unit. This is not only mind boggling but also prone to errors and, besides initial impression, is very un-scalable.
Reason 2 is usage of conditional.
The best most efficient programs are the ones that use practically no conditionals. But if one has no choice, the second best is trying not use if...esle in favor of using switch...case. Switch...case is preferable (at least in AS3) because, unlike if...else all conditions are indexed - thus conditional execution is way faster.
For example, if I have three boolean parameters (p1, p2, p3), we have 7 possible combinations IF we are checking for value "true" only. Thus, in cases I need to check these combinations I will need 7-8 if...else statements:
if (p1 == true)
{
// blah
}
else if (p2 == true)
{
// blah
}
else if (p3 == true)
{
// blah
}
else if (p1 == true && p2 == true)
{
// blah
}
else if (p2 == true && p3 == true)
{
// blah
}
else if (p1 == true && p3 == true)
{
// blah
}
else if (p1 == true && p2 == true && p3 == true)
{
// blah
}
else
{
// blah
}
First of all, what is the most inefficient here is that in order for Flash to get to, say, the last else statement - it has to go through ALL previous ones every time runtime hits this chain of conditionals. This is a pretty big performance bottleneck.
With switch...case runtime jumps right into the condition related to the value that parameter is set to without checking any other conditionals. Again, this is because all possible values are indexed at compilation.
Also, switch...case statement is more scalable. If you need to upgrade a program to accommodate a new condition - you just write an extra case. With combinations of booleans you will need to deal with combinatorics in addition to putting additional overhead on the program performance.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now