gpt4 book ai didi

c# - 为什么 getkeydown 会触发几次? (团结,C#)

转载 作者:太空宇宙 更新时间:2023-11-03 12:44:13 25 4
gpt4 key购买 nike

我正在尝试通过 this course over on Udemy.com 自学 C# 和 Unity 知识.在第一个示例中,创建了一个非常基本的文本冒险。现在从头开始,我使用了那里使用的相同游戏结构,试图重新创建我自己的版本,我遇到了一个问题:

Input.GetKeyDown(KeyCode.R)

连续多次激活,我几个小时都没有找到解决方案。以下是代码的相关部分:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class TextEngine : MonoBehaviour {
public Text myText;
private int oxygen;


private enum States
{
wakeup, window_0, room_1, corridor_1, corridor_left,
corridor_right, corridor_right_left, locked_supply_door,
damaged_supply_door, supply_door_keypad
}
private States myState;

// Use this for initialization
void Start() {
myState = States.wakeup;
}





// Update is called once per frame
void Update()
{

// Basic gameplay loop:
//if myState equals scenario a, then call method a
if (myState == States.wakeup) wakeup();
if (myState == States.room_1) room1();
if (myState == States.corridor_1) corridor_1();
if (myState == States.corridor_right) corridor_right();
if (myState == States.locked_supply_door) locked_supply_door();

}



void wakeup()
{
oxygen = 100;
myState = States.room_1;
}


void room1()
{
myText.text = "Oxygen Level: " + oxygen + "\n\nYou wake up. Press W to see Window. "
+"Press C to go down the Corridor.";

if (Input.GetKeyDown(KeyCode.W))
{
myState = States.window_0;
}
else if (Input.GetKeyDown(KeyCode.C))
{
myState = States.corridor_1;
}


}




void corridor_1()
{

myText.text = "Oxygen Level: " + oxygen + "\n\nYou walk the corridor to the end."
+ "You can turn Left or Right.\nFrom the Left you hear two gun shots!"
+"\nFrom the Right side comes an eerie silence."
+ "\n\nPress L to turn Left, R to turn Right. Press B to go Back.";
if (Input.GetKeyDown(KeyCode.L))
{
myState = States.corridor_left;
}
else if (Input.GetKeyDown(KeyCode.R))
{
print("R1");
myState = States.corridor_right;
}
else if (Input.GetKeyDown(KeyCode.B))
{
print("B");
myState = States.room_1;
}
}



void corridor_right()
{

myText.text = "Oxygen Level: " + oxygen + "\n\nAnother T-junction!\n"
+ "You can turn Left or Right.\nFrom the left you still hear nothing."
+ "\nFrom the Right side you hear a deep and frightening buzz!"
+ "\n\nPress L to turn Left, R to turn Right. Press B to go Back.";
if (Input.GetKeyDown(KeyCode.L))
{
myState = States.corridor_right_left;
}
else if (Input.GetKeyDown(KeyCode.R))
{
print("R2");
myState = States.locked_supply_door;
}
else if (Input.GetKeyDown(KeyCode.B))
{
print("B");
myState = States.corridor_1;
}

}





void locked_supply_door()
{
myText.text = "Oxygen Level: " + oxygen + "\n\nYou stand in front of a rusty metal door. You step closer. At eye-level there is a dusty sign, "
+ "barely readable. You wipe off the dust. It reads: 'Supply Room'. Maybe you can find some Oxygen tanks and "
+ "some communication devices in there?"
+ "\n\nPress S to Search for a handle."
+ "\n\nPress R to Ram the door, it looks weak!\n\n"
+ "Press B to go Back.";
if (Input.GetKeyDown(KeyCode.S))
{
myState = States.supply_door_keypad;
}
else if (Input.GetKeyDown(KeyCode.R))
{
print("R3");
myState = States.damaged_supply_door;
}
else if (Input.GetKeyDown(KeyCode.B))
{
print("B2");
myState = States.corridor_1;
}
}

我期望发生的事情:

  1. 比赛开始。myState = States.wakeup; 初始化。

  2. wakeup() 被调用,myState = States.room_1; 通过 Update() 调用 room1() 和屏幕上显示的文本。

  3. 当按下 C 键时,room(1) 调用 myState = States.corridor_1; 通过 Update() 调用corridor_1() 并再次显示文本。

到目前为止,一切都按预期工作。但是现在一切都出了问题。我期待这种行为:4.A 我按下 R 键,“R1” 被打印,并通过 myState = States.corridor_right; 调用 corridor_right() 并且 < strong>等待另一个关键提示。

但是接下来会发生这种情况:4.B 我按下 R 键,"R1" 被打印出来,然后通过 myState = States.corridor_right; 调用 corridor_right() 然后不再输入立即打印“R2”,执行myState = States.locked_supply_door;,调用locked_supply_door() 通过 Update() 然后立即将 "R3" 打印到控制台,然后它更改 myState = States.damaged_supply_door; 它在哪里将继续通过 Update() 调用方法,但我还没有实现该方法。

这些步骤似乎可以正确执行,但是 GetKeyDown 的行为更像 `GetKey' 而不是它本身!

为什么 GetKeyDown 似乎被激活了多次,即使它应该只激活一次?

人们遇到的大多数问题似乎都调用了 GetKeyDown 两次,但似乎调用了几次。一个常见的问题似乎是该脚本在 Unity 中的某处多次附加,但这里不是这种情况:带有文本字段的 Canvas + attaced 脚本。

如果你已经做到了这一步,我要感谢你花时间帮助我。如果我能解决这个问题,我找不到解决方案!我的橡皮鸭在这里也帮不了我。

最佳答案

问题是你没有用ELSE做“更新”,所以当你从一个状态改变到另一个状态时,它不会等待1帧通过,会直接去other if ,因为你改变了并且是正确的,你输入另一个if,并且因为直到最后一个R下降才通过一帧,它仍然会说是真的...

因此,在您的“更新”功能中,使用ELSE IF 来创建它们,而不是所有IF。这样,当你输入一个if,里面的函数执行完后,会过1帧重新进入。

所以将Update改成这样:

void Update()
{

// Basic gameplay loop:
//if myState equals scenario a, then call method a
if (myState == States.wakeup) wakeup();
else if (myState == States.room_1) room1();
else if (myState == States.corridor_1) corridor_1();
else if (myState == States.corridor_right) corridor_right();
else if (myState == States.locked_supply_door) locked_supply_door();

}

关于c# - 为什么 getkeydown 会触发几次? (团结,C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37895945/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com