【问题标题】:Function sometimes randomly calling函数有时会随机调用
【发布时间】:2022-11-12 13:59:39
【问题描述】:

我正在制作棋盘/纸牌游戏 Carcassonne。我有预制卡,每张卡都有四个变量 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;
    }

该代码描述了我到目前为止所尝试的内容。感谢您的帮助,这对我来说意义重大!

【问题讨论】:

    标签: c# unity3d game-development


    【解决方案1】:

    问题是有时当我放一张卡片时,该函数被调用一次 从打出的卡片开始,一次从之前打出的卡片,然后一次 再次从刚刚铺设的卡中。

    所以问题是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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-08-02
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多