unity3d 函数有时会随机调用

llew8vvj  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(142)

我正在做一个棋盘/纸牌游戏卡尔卡松。我有预制的卡片,每张卡片都有四个变量s,v,j,z(我的语言中的世界指南针)。我有一个函数,它可以找到值为“R”的边(代表道路)并找到旁边的牌例如,如果s ==“R”,则调用第二个函数,该函数在卡片的顶部找到瓦片,并将变量lastSide设置为“j”,这样当再次调用第一个函数时,它就不会返回。道路总是只在两侧,所以有“nicovani”。我希望这不难理解,问题是有时当我铺一张牌时,函数被调用一次从铺好的牌,一次从之前铺好的牌,然后再一次从刚铺好的牌。我不知道为什么,但这是我需要解决的最后一件事来完成这个。如果已经读到这里我已经很感激了。下面是重要的代码:

public string s;
    public string v;
    public string j;
    public string z;
    private int cross = 0;
    public bool Layed;
    public bool IsRoadEnding;
    private string lastSide;
    private int nicovani = 0;
    private bool isScored = false;

    public void OnMouseDown()
    {
        if(Layed == false)
        {
            if(r == 1)
            {
                r = 2;
                IsHere = false;
                StartCoroutine(Follow());
            }
            else 
            {
                if(IsHere == true)
                {
                    TheWholeThing();
                }
                
                else
                {
                    r = 1;
                    transform.position = spawner.transform.position;
                }
            }
        }
    }

    void TheWholeThing()
    {
        setPos = new Vector2 (Mathf.RoundToInt(transform.position.x), Mathf.RoundToInt(transform.position.y));
        r = 1;
        transform.position = setPos;
        FindTile();
        CheckTile(asociatedTile);

        if(r == 1)
        {
            drawer.SpawnCard();

            SetTile(asociatedTile);
            
            gmg.GenerateGrid(transform.position.x, transform.position.y+1, "j" , s);
            gmg.GenerateGrid(transform.position.x, transform.position.y-1, "s", j);
            gmg.GenerateGrid(transform.position.x +1, transform.position.y, "z", v);
            gmg.GenerateGrid(transform.position.x -1, transform.position.y, "v", z);
        
            Layed = true;

            startingTile = gameObject.transform;
            if(nicovani < 2)
            {
                FindScoringRoad(transform.position.x, transform.position.y, "", s, v, j, z);
                return;
            }
        }
        else
        {
            return;
        }

        IsHere = false;
    }

    void FindScoringRoad(float x, float y, string side, string s, string v, string j, string z)
    {
        if(isScored == false)
        {
            lastSide = side;

            if(lastSide != "s")
            {
                if(s == "R")
                {
                    cross = 0;
                    Debug.Log("s");
                    FindNextCard("j", x, y + 1);
                }
            }

            if(lastSide != "v")
            {
                if(v == "R")
                {
                    cross = 0;
                    Debug.Log("v");
                    FindNextCard("z", x + 1, y);
                }
            }

            if(lastSide != "j")
            {
                if(j == "R")
                {
                    if(nicovani == 2)
                    {
                        nicovani = 0;
                        return;
                    }
                    else
                    {
                        cross = 0;
                        Debug.Log("j");
                        FindNextCard("s", x , y - 1);
                    }
                }
            }
          
            if(lastSide != "z")
            {
                if(z == "R")
                {
                    if(nicovani == 2)
                    {
                        nicovani = 0;
                        return;
                    }
                    else
                    {
                        cross = 0;
                        Debug.Log("z");
                        Debug.Log(x + " " + y);
                        FindNextCard("v", x - 1, y);
                    }
                }
            }
            cross = 0;
            return;
        }
    }

    void FindNextCard(string side, float x, float y)
    {
        if(x == startingTile.position.x & y == startingTile.position.y)
        {
            Debug.Log("Road Closed");
            isScored = true;
            return;
        }

        foreach(GameObject card in drawer.spawnedCards)
        {
            if(card.transform.position.x == x & card.transform.position.y == y)
            {
                var cardS = card.GetComponent<Card>();
                if(cross < 1)
                {
                    FindScoringRoad(card.transform.position.x, card.transform.position.y, side, cardS.s, cardS.v, cardS.j, cardS.z);
                    cross++;
                }          
                return;
            }         
        }

        Debug.Log("Ends here");
        cross = 0;
        nicovani++;
        return;
    }

代码描述了我到目前为止所尝试的。感谢任何帮助,它对我意义重大!

xriantvc

xriantvc1#

问题是有时当我放一张牌时,函数从放的牌调用一次,从之前放的牌调用一次,然后再从刚放的牌调用一次。
所以问题是FindScordingRoad被调用了三次,而你只期望两次?我想这就是你的问题。

void FindScoringRoad(float x, float y, string side, string s, string v, string j, string z)
{
    // we enter this function so this method has been called once
    // assuming s and v equals "R" but not j and z
    if (isScored == false)
    {
        lastSide = side;
        // lastSide == "" so we enter if below.
        if (lastSide != "s")
        {
            // as per assumption this is true and we enter if
            if (s == "R")
            {
                ...
                // This will call FindScoringRoad a second time
                FindNextCard("j", x, y + 1);
            }
        }

        // lastSide == "" still so we enter if below.
        if (lastSide != "v")
        {
            // as per assumption this is true and we enter if
            if (v == "R")
            {
                ...
                // This will call FindScoringRoad a third time
                FindNextCard("z", x + 1, y);
            }
        }
        ...
    }
}

您可以通过设置cross=0并在FindNextCard之后立即返回来解决此问题

if (lastSide != "s")
{
    if (s == "R")
    {
        cross = 0;
        Debug.Log("s");
        FindNextCard("j", x, y + 1);
        cross = 0;
        return;
    }
}

相关问题