- Home /
Switch case using enum javascript
Hi everyone.
I'm trying to do a switch case using enum in javascript. I get this error when I save the code: "Assets/Scripts/BoxManagerJS.js(186,32): BCE0019: 'spell01' is not a member of 'Object'. ". The error is linked to the switch case.
Here's my code:
enum spellName {spell01, spell02, spell03, spell04};
function Update()
{
if(Input.GetKeyDown(KeyCode.Keypad6))
{
checkInASpell(spellName.spell01);
}
if(Input.GetKeyDown(KeyCode.Keypad8))
{
checkInASpell(spellName.spell02);
}
if(Input.GetKeyDown(KeyCode.Keypad4))
{
checkInASpell(spellName.spell03);
}
if(Input.GetKeyDown(KeyCode.Keypad2))
{
checkInASpell(spellName.spell04);
}
}
function checkInASpell (spellName)
{
switch(spellName)
{
case spellName.spell01:
Debug.Log ("spell01");
break;
case spellName.spell02:
Debug.Log ("spell02");
break;
case spellName.spell03:
Debug.Log ("spell03");
break;
case spellName.spell04:
Debug.Log ("spell04");
break;
}
}
I want to call the function checkInSpell with a diffrent arg in relation to the keypad I pressed. For example, If I press the keypad6, I want to call checkInSpell using the arg spellName.spell01 to enter the case spellName.spell01
Thanks for your help :)
Answer by Dave-Carlile · Feb 15, 2013 at 02:45 PM
The problem is one of scope.
Your enum is named spellName
, but you're also passing in a parameter with the name spellName
. The compiler will use the most local scope it can when there are duplicate names. In this case, your case statement is using the parameter, and looking for class properties of spell01
, spell02
, and so on, which don't exist on the parameter. This is what the error is complaining about.
You need to either rename your enum, or rename your parameter, or you can possibly include the script name when when you want to refer to the enum...
YourScriptName.spellName.spell01
But I don't know if that works in Javascript. Your best bet would be to rename one of them. I personally always use upper case the initial character for my enum names, so SpellName
.
Answer by zye · Feb 15, 2013 at 03:34 PM
Thanks a lot for your explication, now it's working perfectly !
For next readers, here's the code with the modification thanks to Dave Carlile. This code works perfectly.
enum SpellName {spell01, spell02, spell03, spell04};
function Update()
{
if(Input.GetKeyDown(KeyCode.Keypad6))
{
checkInASpell(SpellName.spell01);
}
if(Input.GetKeyDown(KeyCode.Keypad8))
{
checkInASpell(SpellName.spell02);
}
if(Input.GetKeyDown(KeyCode.Keypad4))
{
checkInASpell(SpellName.spell03);
}
if(Input.GetKeyDown(KeyCode.Keypad2))
{
checkInASpell(SpellName.spell04);
}
}
function checkInASpell (spellName : SpellName)
{
switch(spellName)
{
case spellName.spell01:
Debug.Log ("spell01");
break;
case spellName.spell02:
Debug.Log ("spell02");
break;
case spellName.spell03:
Debug.Log ("spell03");
break;
case spellName.spell04:
Debug.Log ("spell04");
break;
}
}
Thanks Dave Carlile ! :)
Your answer
Follow this Question
Related Questions
Operator '==' cannot be used! 2 Answers
js switch case warning 1 Answer
How to check if an enum’s case has changed 3 Answers
What is a more efficient way to write this Switch Statement? 3 Answers
Cycling through enum with key press 1 Answer