I don’t think you need the breaks since you have a return. And I think it’s ok to pile cases as you did, that’s the point of a switch.
All this I know for sure from C#, but I guess JS is the same.
I’d try to compile it to see what’s wrong but I don’t feel like re-writing your enum
In fact, you can’t do it in C#, either. You get “error CS0163: Control cannot fall through from one case label to another” if you try. Maybe you’re thinking of C/C++, in which falling through from one case to another is allowed.
EDIT: just found this useful page, which tells you how to explicitly fall through cases in C#. Don’t know if that works for Javascript, though. I seem to recall that Unity’s Javascript doesn’t have goto at all.
EDIT 2: and after all that, I can’t get the fallthrough behaviour described above to work for code compiled with the command line Mono tools. I haven’t tried it in Unity itself, but I expect it will behave in the same way. Maybe this is something that Mono doesn’t implement in the usual way.
To NCarter: scroll to the bottom of the page you just sent:
If it doesn’t work in Unity’s C#, it’s probably Mono’s fault. Same with JS, should work, but OTEE’s implementation is still incomplete.
Also, to podperson, last paragraph of that same page:
As a result of the C# rules requiring explicit flow-control to occur at the end of a case block (most usually a break), many people question why the behavior simply wasnt changed such that fall-through didnt occur. That is, dont make break required, simply change the semantics of switch to not have fall-through for cases. The reason this wasnt done was so that developers who were very used to C++ wouldnt have a hard time understanding what a switch statement was doing.
So writing
return false;
break;
is an overkill: return already is a flow-control keyword.
Oops, I just realised that I accidentally omitted the break statement in the final case (!), and that was why it wasn’t working for me. Therefore, that page I linked is indeed correct for C# with Mono/Unity, and you were right in the first place.
I thought it was odd that the Mono guys had missed something fundamental like that.
Wow this is an old post… nether the less… i think i found a work around…
in javascript:
var value : int;
function Update(){
switch(value){
case 0:; // fall through
case 1:; // fall through
case 2:
print("I can handle case: " + value);
break;
default:
print("I can not handle case: " + value);
break;
}
}
Simply putting a line enders after the case, stops it from complaining and makes it work