- Home /
Question by
TrewSx · Mar 12, 2013 at 02:49 PM ·
javascriptenumstate
EnumStates
Hello, i would like to create an Enum state for treasure chest, Default state will be Close since all chest's are being closed at the begining. Could u please tell me if i am heading the right direction and what is wrong in my code
#pragma strict
//Chest script
enum ChestState { Open, Close, InBetween } //Enum for tri-state values.
var stateOpen = ChestState.Open; //Chest Open
var stateClose = ChestState.Close; //Chest Close - set as default
var stateInBetween = ChestState.InBetween; //Somewhere in the process of open or close
function Start () {
//chestState = Chest.ChestState.Close;
}
function Update () {
}
function OnMouseEnter() {
Debug.Log("Enter");
}
function OnMouseExit() {
Debug.Log("Exit");
}
function OnMouseUp() {
Debug.Log("Up");
if(ChestState.Close)
Open();
else
Close();
}
private function Open() {
animation.Play();
ChestState.Open;
}
private function Close() {
animation.Play();
ChestState.Close;
}
Comment
ok i think i got the Start function working
function Start () {
var chestState = ChestState.Close;
}
Best Answer
Answer by Khada · Mar 12, 2013 at 04:08 PM
When you define an enum, you're really creating a new type of variable that you can make later on, not an instance of an existing type.
//this is a new enum variable type (think blueprint)
enum MyEnum
{
STATE_ONE,
STATE_TWO,
}
Once we have defined the new type, we can create an instance of it:
//we use the same structure as we do to define other variables
var myEnumInstance : MyEnum;
Then we can assign a value to our new enum instance:
//we can set it to the values contained within the enum type/blueprint
myEnumInstance = MyEnum.STATE_ONE;
And later on we can test our enum instance to see what its value is/isn't
if(myEnumInstance == MyEnum.STATE_ONE)
myEnumInstance = MyEnum.STATE_TWO;
That should provide you with enough understanding to properly implement an enum.