Copy link to clipboard
Copied
Is it possible to make a multiple condition case
I have 3 variables a,b,c and they can all go from 1-3. I want to be able to make a case for every situation possible.
For example :
if a =1, b=1, c=1
trace(1)
if a=2, b=2, c=2
trace(2)
....
I want a condition for every possible situation, but with if`s it takes a lot of code and doesnt look neat. I was wondering if you could use cases to do so.
var a:int = 1-3; (randomized form 1 to 3)
var b:int = 1-3; (randomized form 1 to 3)
var c:int = 1-3; (randomized form 1 to 3)
switch (a,b,c)
{
case (1,1,1):
trace("1")
break;
case (2,2,2)
trace("2")
and every other possible combination....
}
I tried it and it traced 1 when a,b or c was equal to 1, not all three of them at the same time.
Is it possible to do that with cases? If not what would be the easiest way to do trace something for every possible situation?
Thanks
Copy link to clipboard
Copied
you could but your code might be easier to understand and debug (or it might not) if you do it the long way. otherwise,
var n:int = a*100+b*10+c;
switch(n){
}
Copy link to clipboard
Copied
If your values are straight up numeric then I'd recommend kglads approach. If there's more complexity and you're not giving all the exact information then you're creating 3*3*3 (27) different possibilities so without anything thing can cross-relate (such as if the 3rd number is ever 3 then "always do this", etc), you'll need 27 different answers. I'd reconsider the logic you're using that would put you in this situation, chances are there might be a better approach. Otherwise, I think I'd opt for pushing an array with the 27 paths you need. If it starts going north of 27 different paths, nothing is really going to be ideal. At least with an array the lookup is fast, the creation is tedious but any path in along these lines will be..
e.g.:
// generate arr from arr[1][1] to arr[3][3]
var arr:Array = [];
for (var i:int = 1; i < 4; i++) {
arr = [];
for (var j:int = 1; j < 4; j++) {
arr
= []; }
}
function threeThreeThree() {
return 'threeThreeThree()';
}
arr[1][1][1] = '111';
// to 1 3 3
arr[2][1][3] = '213';
// to 2 3 3
arr[3][3][3] = threeThreeThree;
// etc
trace(arr[1][1][1]); // 111
trace(arr[2][1][3]); // 213
trace(arr[3][3][3]()); // threeThreeThree()
Again the goal would be to figure out why you need to do this and if possible refactor it to something more optimized. At least with this kind of approach you can program ranges of values at a time with a bit more sophistication than matching. Not much, but some.
Note: Since an Array is 0-based index, do not attempt to iterate on them unless arr[0][0][0] is a valid answer to you. Your question stated 1-1-1 to 3-3-3 so I presume 0 is out of your range. Those values will not be assigned so you can't loop on the array or all those values could break something.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now