Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 团结中的牢不可破的循环,不知道如何修复。使团结崩溃_C#_Loops_Unity3d - Fatal编程技术网

C# 团结中的牢不可破的循环,不知道如何修复。使团结崩溃

C# 团结中的牢不可破的循环,不知道如何修复。使团结崩溃,c#,loops,unity3d,C#,Loops,Unity3d,我试图在unity中创建一个“组合”系统,但我已经创建了一个牢不可破的循环,我正在努力理解它。我尝试过使用while循环,但遇到了这个问题,所以我也想尝试一下for循环,但我得到了相同的结果 组合系统的工作方式是,当玩家与敌人进入一个条件时,他们可以在控制器上输入一系列按钮来触发组合。如果玩家输入正确的组合,效果将被应用。到目前为止,我只担心组合系统能否正常工作 我已经为组合创建了一个预定义的字符数组,然后将玩家的输入与该数组进行比较 using System.Collections; usin

我试图在unity中创建一个“组合”系统,但我已经创建了一个牢不可破的循环,我正在努力理解它。我尝试过使用while循环,但遇到了这个问题,所以我也想尝试一下for循环,但我得到了相同的结果

组合系统的工作方式是,当玩家与敌人进入一个条件时,他们可以在控制器上输入一系列按钮来触发组合。如果玩家输入正确的组合,效果将被应用。到目前为止,我只担心组合系统能否正常工作

我已经为组合创建了一个预定义的字符数组,然后将玩家的输入与该数组进行比较

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComboSystem : MonoBehaviour
{
    public char currentButton;
    public char[] combo_arr = { 'A', 'A', 'B', 'B', 'X' };

    PlayerLockOnSystem plos;

    private void Start()
    {
        plos = GetComponent<PlayerLockOnSystem>();
    }

    private void Update()
    {
        if (plos.lockedOn)
        {
            Combo();
        }
    }

    void DetectComboButtons()
    {
        if (Input.GetButton("Joystick A"))
        {
            currentButton = 'A';
        }

        if (Input.GetButton("Joystick X"))
        {
            currentButton = 'X';
        }

        if (Input.GetButton("Joystick B"))
        {
            currentButton = 'B';
        }


    }

    void Combo()
    {



        for (int i = 0; i < combo_arr.Length; i++)
        {
            DetectComboButtons();

            if (currentButton == combo_arr[i])
            {

                Debug.Log("Correct: " + currentButton);
            }
            else
            {
                i = 0;
                Debug.Log("Incorrect");
            }
        }






    }




}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共阶级制度:单一行为
{
公共字符按钮;
public char[]combo_arr={'A','A','B','B','X'};
PlayerLockOnSystem plos;
私有void Start()
{
plos=GetComponent();
}
私有void更新()
{
国际单项体育联合会(plos.lockedOn)
{
Combo();
}
}
void DetectComboButtons()
{
if(Input.GetButton(“操纵杆A”))
{
currentButton='A';
}
if(Input.GetButton(“操纵杆X”))
{
currentButton='X';
}
if(Input.GetButton(“操纵杆B”))
{
currentButton='B';
}
}
无效组合()
{
for(int i=0;i

当触发Combo()方法时,Unity崩溃,我必须强制关闭编辑器。

您需要在不同的更新调用中检查帧之间的输入。试试这个:

private const char CHAR_THAT_MEANS_THAT_THE_PLAYER_DIDNT_BREAK_THE_CHAIN = '0';//doesnt matter

private int _currentPlaceInTheComboChain = 0;

private void Update()
{
    if (plos.lockedOn)
    {
        Combo();
    }
}

char DetectComboButtons()
{
    if (Input.GetButtonDown("Joystick A"))
    {
        return 'A';
    }

    if (Input.GetButtonDown("Joystick X"))
    {
        return 'X';
    }

    if (Input.GetButtonDown("Joystick B"))
    {
        return 'B';
    }
 return CHAR_THAT_MEANS_THAT_THE_PLAYER_DIDNT_BREAK_THE_CHAIN;


}

void Combo()
{
    char currentButton = DetectComboButtons();
    if (currentButton == CHAR_THAT_MEANS_THAT_THE_PLAYER_DIDNT_BREAK_THE_CHAIN) 
    {
       return; //the player didn't continue the combo but didn't break it (yet)
    } 

    if (currentButton == combo_arr[_currentPlaceInTheComboChain])
    {
       _currentPlaceInTheComboChain++;//wait for the next button, will only be checked the next time update is called
       Debug.Log("Correct: " + currentButton);
       if (_currentPlaceInTheComboChain == combo_arr.Length) { //this was the last button in the combo
           _currentPlaceInTheComboChain = 0; //for a new combo
           Debug.Log("Combo completed");
       }
    } else {
      _currentPlaceInTheComboChain = 0; //player broke the chain
      Debug.Log("Incorrect " + currentButton);
    }

}

您需要向
detectcombobutions
添加while循环和延迟。代码不等待输入,它只返回按下的键,如果没有按下任何键,则不返回任何内容。您需要等待一个输入。您正在循环中设置i=0,这将导致无限循环。你必须跟踪玩家当前的按键和他们所在组合的当前索引。。如果“下一步”按钮不正确,请重置当前组合索引。感谢您的快速响应,我仍然有点不确定您的意思,很抱歉,如果这是一个非常基本的事情,我只是不完全了解,请参阅是否有任何帮助。好的,我来看看!感谢好消息,它不再运行无限循环了,代码可以正常工作了。当我按下“正确”按钮时,控制台输出正确和不正确,并且似乎不再前进。您的意思是它只识别第一个按钮,但没有继续,没有达到“组合完成”,或者它确实按照您的要求一次继续一个按钮?它似乎没有作为“组合完成”前进不会出现在我的日志中。这是我在按下正确的组合键后得到的。哦,你需要使用GetButtonDown而不是getButton,否则按下时间过长将被视为2次或更多次按下。代码可能会简化一点(可能使用协同程序),但答案没有错。Tom,您可能还需要编辑答案以使用
GetButtonDown