简体   繁体   English

在Unity 4.6中的文本冒险中遇到状态更改问题

[英]Having issues with State changes in a Text Adventure in Unity 4.6

Basically, I'm trying to get Unity to change the game's state when the player clicks a button. 基本上,当玩家点击按钮时,我正试图让Unity改变游戏的状态。 For whatever reason though, it isn't reading the second key press that actually changes the state. 无论出于何种原因,它并没有读取实际改变状态的第二次按键。 The code looks like this: 代码如下所示:

void state_Cell () {
    if (CelP == 0) {
        text.text = "Some text";
        if (Input.GetKeyDown (KeyCode.C)) {            //This works
            ++CelP;
            text.text = "Some more text.";
            if (Input.GetKeyDown (KeyCode.B)) {        //This doesn't work
                ++CelP;
                MyState = States.Bed;
            }
        }
    }
}

If it will help, this is the code for the Bed state that I can't access in the game: 如果它有帮助,这是我在游戏中无法访问的Bed状态的代码:

void state_Bed () {
    if (BedP == 0) {
        text.text = "Still more text.";
        if (Input.GetKeyDown (KeyCode.D)) {
            MyState = States.Delivery;
            BedP++
        }
    }
}

The most confusing part about this for me is that Unity's compiler isn't giving me any errors. 对我来说最令人困惑的部分是Unity的编译器没有给我任何错误。 I encountered this problem with a different state before, and what I did was move the "++Variable" to a different location, but that didn't work for this statement. 我以前遇到过这个问题的状态不同,而我所做的就是将“++ Variable”移到另一个位置,但这对于这个语句不起作用。

Aside from that, I've also tried to removing the "if statement" in the Bed state, but that didn't work either. 除此之外,我还尝试删除Bed状态中的“if语句”,但这也不起作用。

Another thing that I attempted was changing "B" in the "KeyCode" to different keys, but that didn't work at all. 我尝试的另一件事是将“KeyCode”中的“B”改为不同的键,但这根本不起作用。

I also tried to include a "print();" 我还尝试包括“print();” statement at the very beginning of the Bed state, but it didn't trigger either. 在床状态开始时的声明,但它也没有触发。

The last thing that I've tried was putting a "print();" 我尝试的最后一件事是放一个“print();” statement after the "(KeyCode.B))", but that didn't work either. “(KeyCode.B))”之后的语句,但这也不起作用。 So from that I know that the problem is that Unity isn't reading or responding to the key press . 因此我知道问题在于Unity没有阅读或回应按键

Any help would be appreciated. 任何帮助,将不胜感激。 And you need anymore information than what I've supplied, tell me and I'll give it to you. 而且你需要的信息比我提供的更多,告诉我,我会把它给你。

Thanks in advance. 提前致谢。

You should create two states for Cell, first one listens to C and changes the state to second, second one listens to B and changes the state to Bed. 您应该为Cell创建两个状态,第一个侦听C并将状态更改为第二个,第二个侦听B并将状态更改为Bed。

The reason is that state machines do not follow after where they left in the middle of the code inside one state. 原因是状态机在一个状态内的代码中间离开之后不会跟随。 You need to be more specific when you are defining your states. 在定义状态时,您需要更具体。

I assume that state_Cell is being called from an Update or a similar Coroutine which is being called every x frame. 我假设state_Cell是从Update或类似的Coroutine调用的,每个x帧都会调用它。 so local variables inside state_Cell is reset every time it is called. 因此,每次调用state_Cell时,都会重置局部变量。 so machine does not know if user previously pressed C or did not press anything. 所以机器不知道用户以前是否按过C或没有按任何东西。

When user presses a key then technically the state has been changed. 当用户按下键时,从技术上讲,状态已经改变。 when user presses another key then again state changes. 当用户按下另一个键时,再次声明更改。

Edit 编辑

In case you don't want to add extra states this is the simplified version of your state_Cell 如果您不想添加额外状态,则这是state_Cell的简化版本

You have to be careful when to set the extra Boolean to false. 将额外的布尔值设置为false时必须要小心。

bool c_pressed = false;
void state_Cell () {
        if (c_pressed || Input.GetKeyDown (KeyCode.C)) {            
           c_pressed = true;
            if (Input.GetKeyDown (KeyCode.B)) {
               c_pressed = false;
                MyState = States.Bed;
            }
        }
        else
             c_pressed = false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM