You wouldn't.
Printable View
You wouldn't.
Alex, it may be possible to do something like:
...but why would you want to do it this way? Using if's is pretty standard for basic UDLR movement.Code:switch(Key.getCode) {
case Key.RIGHT :
case Key.LEFT :
case Key.DOWN :
case Key.UP :
}
movement in two lines, no if's
_x += Key.isDown(Key.RIGHT) ? 5 : Key.isDown(Key.LEFT) ? -5 : 0
_y += Key.isDown(Key.DOWN) ? 5 : Key.isDown(Key.UP) ? -5 : 0
Might be useful for a tilebased I'm making... afterall, that's the whole spawn of this thread.Quote:
Originally Posted by mr_malee
i imagine switches are a bit more efficient than if's
that way, you only have to call on the variable once, instead of multiple times
mr_malee: wow, you can write if's that way?
Actually that's more old school than you can imagine.Quote:
Originally Posted by trogdor458
kudos to mr_malee for 2 lined movement.
I under stand how to use this but, where did the
come from, what is it called and where can I learn more about this. Thanks.Code:(condition) ? action : 0;
Quote:
Originally Posted by zervell
?: conditional operator
expression1 ? expression2 : expression3
Instructs Flash to evaluate expression1, and if the value of expression1 is true, it returns the value of expression2; otherwise it returns the value of expression3.
this baby was available in flash 4, so yeah, like ImprisonedPride said, its old school. To learn more just use flash help and go to operators, there's lots of other things as well that you might not know about
nice coding malee so thats how you do it thanks guys for the info. and thanks to you as well improsindpride i think thats ur name well thanks for your help as well.(i will start learning this code now)
Just a bit of warning. More compact code often results in worse performance. For instance,
is quite a lot slower than:Code:_x += Key.isDown(Key.RIGHT) ? 5 : Key.isDown(Key.LEFT) ? -5 : 0
_y += Key.isDown(Key.DOWN) ? 5 : Key.isDown(Key.UP) ? -5 : 0
The second example only perform an addition/subtraction when a key is pressed while the first example always performs it (with zero if no key is pressed). In this case it's probably not a big deal because you probably don't check the keys more than once every frame anyway.Code:if (Key.isDown(Key.RIGHT)) {
_x += 5;
} else {
if (Key.isDown(Key.LEFT)) {
_x -= 5;
}
}
if (Key.isDown(Key.DOWN)) {
_y += 5;
} else {
if (Key.isDown(Key.UP)) {
_y -= 5;
}
}
Not saying it's a good idea, but you can off course also use nested switches, or nest if's inside switches.Quote:
Originally Posted by iopred
You can also do things like this :
Which has been useful to me once or twice, but I can't remember when or why.Code:switch(true) {
case a==b : trace("1"); break;
case a!=c : trace("2"); break;
// ...
}
yeah, it does suck, i wish it would work better, i went through a massive conditional phase not long ago until i found out it has bad speed.Quote:
Originally Posted by strille
unfortunately you tend to have to sacrifice speed for size
technically, if you had every single possible output for a game, and just use gotoandstop, it would be ultra fast would it not?
youll probably prove me wrong, i just know it
The real power of Switch is in making alot of "or" comparisons. ie:
That's a little bit cleaner than doingCode:switch(a)
{
case 4:
case 8:
case 12:
//case 1
break;
case 0:
//case 2
break;
default:
// everything else;
}
Code:if((a==4) or (a==8) or (a==12)){
// case 1
} else if (a==0){
// case 2
} else{
// default
}