Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.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# 在unity 3d中绘制卡片会产生不同的结果_C#_Unity3d - Fatal编程技术网

C# 在unity 3d中绘制卡片会产生不同的结果

C# 在unity 3d中绘制卡片会产生不同的结果,c#,unity3d,C#,Unity3d,我有我的剧本,是关于移动的东西,从甲板名单到提请一堆手到丢弃一堆 我的问题是,当我绘制一张卡片时(使用一个按钮调用DrawtoHand),它会工作,但有时绘制的卡片少于DrawCount变量设置的数量(在本例中为5)。从我测试的情况来看,事情似乎没有正确地进入Discardlist/成为Discardpile的孩子,它们有时会,但并不总是 我已经在这上面停留了足够长的时间,代码在我的脑海中真的在一起运行,所以任何帮助都将不胜感激 public class Draw : MonoBehaviour

我有我的剧本,是关于移动的东西,从甲板名单到提请一堆手到丢弃一堆

我的问题是,当我绘制一张卡片时(使用一个按钮调用DrawtoHand),它会工作,但有时绘制的卡片少于DrawCount变量设置的数量(在本例中为5)。从我测试的情况来看,事情似乎没有正确地进入Discardlist/成为Discardpile的孩子,它们有时会,但并不总是

我已经在这上面停留了足够长的时间,代码在我的脑海中真的在一起运行,所以任何帮助都将不胜感激

public class Draw : MonoBehaviour
{
    public GameObject Card1;
    public GameObject Card2;
 
    public int DrawCount;
    public CharacterController charactercontroller;
 
 
    public GameObject Deckpile;
    public GameObject Handpile;
    public GameObject Discardpile;
 
 
    List<GameObject> Drawlist = new List<GameObject>();
    List<GameObject> Discardlist = new List<GameObject>();
    List<GameObject> Handlist = new List<GameObject>();
    List<GameObject> Decklist = new List<GameObject>();
 
 
    // Start is called before the first frame update
    void Start()
    {
        for (int i = 0; i < 5;)
        {
            Decklist.Add(Card2);
            Decklist.Add(Card1);
            i++;
        }
 
        Deckpile = GameObject.Find("DeckPile");
        Handpile = GameObject.Find("Yours");
        Discardpile = GameObject.Find("DiscardPile");
 
 
        Shuffle(Decklist);
        Makedraw();
        charactercontroller.Energyreset();
        DrawtoHand();
 
    }
    public void Makedraw()
    {
        int a = 0;
        foreach (GameObject x in Decklist)
        {
            GameObject deckCard = Instantiate(Decklist[0 + a], new Vector3(0, 0, 0), Quaternion.identity);
            deckCard.transform.SetParent(Deckpile.transform, false);
            Drawlist.Add(deckCard);
            a++;
 
        }
    }
 
    public void DrawtoHand()
    {
        HandtoDiscard();
        charactercontroller.Drawing();
        charactercontroller.Energyreset();
        for (var i = 0; i < DrawCount;)
        {
            if (Drawlist.Count >= DrawCount)
            {
                Drawlist[0].transform.SetParent(Handpile.transform, false);
                Handlist.Add(Drawlist[0]);
                Drawlist.RemoveAt(0);
                i++;
            }
            else
            {
                Shuffle(Discardlist);
                DiscardtoDraw();
            }
        }
 
 
    }
 
    public void HandtoDiscard()
    {
 
        for (var i = 0; i < Handlist.Count; i++)
        {
            Handlist[i].transform.SetParent(Discardpile.transform, false);
            Discardlist.Add(Handlist[i]);
 
 
        }
        for (var i = 0; i < Handlist.Count; i++)
        {
            Handlist.RemoveAt(0);
        }
 
    }
 
    void Shuffle<T>(List<T> list)
    {
        System.Random random = new System.Random();
        int n = list.Count;
        while (n > 1)
        {
            int k = random.Next(n);
            n--;
            T temp = list[k];
            list[k] = list[n];
            list[n] = temp;
        }
    }
 
    public void DiscardtoDraw()
    {
        for (var i = 0; i < Discardlist.Count; i++)
        {
            Discardlist[i].transform.SetParent(Deckpile.transform, false);
            Drawlist.Add(Discardlist[i]);
 
        }
        for (var i = 0; i < Discardlist.Count; i++)
        {
            Discardlist.RemoveAt(i);
        }
    }
 
}
公共类绘图:单一行为
{
公共游戏对象卡1;
公共游戏对象卡2;
公共提款;
公共字符控制器字符控制器;
公共游戏对象;
公共游戏对象处理堆;
公共游戏对象;
List Drawlist=新列表();
列表丢弃列表=新列表();
List Handlist=新列表();
List Decklist=新列表();
//在第一帧更新之前调用Start
void Start()
{
对于(int i=0;i<5;)
{
甲板列表。添加(卡片2);
甲板列表。添加(Card1);
i++;
}
Deckpile=GameObject.Find(“Deckpile”);
Handpile=GameObject.Find(“您的”);
Discardpile=GameObject.Find(“Discardpile”);
洗牌(甲板名单);
Makedraw();
charactercontroller.Energyreset();
DrawtoHand();
}
公开作废Makedraw()
{
int a=0;
foreach(Decklist中的游戏对象x)
{
游戏对象deckCard=实例化(Decklist[0+a],新向量3(0,0,0),四元数.identity);
deckCard.transform.SetParent(Deckpile.transform,false);
提款单。添加(deckCard);
a++;
}
}
公开作废
{
HandtoDiscard();
charactercontroller.Drawing();
charactercontroller.Energyreset();
对于(变量i=0;i=DrawCount)
{
图纸列表[0].transform.SetParent(Handpile.transform,false);
手工列表。添加(提取列表[0]);
提取列表。删除(0);
i++;
}
其他的
{
洗牌(丢弃列表);
丢弃todraw();
}
}
}
公开作废手册()
{
对于(变量i=0;i1)
{
int k=随机。下一个(n);
n--;
T温度=列表[k];
列表[k]=列表[n];
列表[n]=临时;
}
}
public-void-draw()
{
对于(var i=0;i
我在DrawtoHand()中看到的第一件事是在循环中一次绘制一张卡,但在循环的每次迭代中,您都会检查Drawlist中剩余的卡是否至少是您需要绘制的卡的总数

假设你正试图从4张牌中抽出3张牌。你画第一个,留下一堆3。然后画第二个,留下一堆2。那么就不画第三个了,因为Drawlist.Count 但就其本身而言,这可能不会阻止你画出正确数量的牌,只要你的手与牌的总数相比很小。这只会让你提前调整。(不过请注意,如果你在重新洗牌后仍然没有足够的牌来抽牌,你会陷入一个无限循环,反复洗牌空的弃牌堆,你的游戏就会冻结。为了安全起见,你应该为此添加一个特例。)

在HandtoDiscard()和DiscardtoDraw()中,您正在错误地清除列表。移除(0)后,列表中的所有其他元素将向下复制(1变为新的#0,#2变为新的#1,以此类推)。您的循环每次递增i,因此您要调用RemoveAt(0),然后调用RemoveAt(1),然后调用RemoveAt(2),等等。但是,当您调用RemoveAt(1)时,原始的#1已经移动到插槽#0以填充您删除的第一个内容,因此您永远不会删除它。实际上,您只删除了列表中的一半元素,而不是全部元素

如果要从列表中删除所有内容,只需调用yourList.Clear()

(调用RemoveAt(0)也没有效率,因为它会迫使计算机将列表中的所有内容复制到一个插槽中,以填充空白。但在您有更多经验之前,可能不值得担心。)

这是一个棘手的问题:由于您将一个列表中的所有卡片添加到新列表中,但只从旧列表中删除了一半,因此其中一些卡片现在同时在两个列表中。在屏幕上直观地显示卡片的游戏对象只在一个地方,但是你的代码没有检查它在对象层次结构中的位置,它依赖于你的列表,而现在这些列表是相互矛盾的

因此,你重新排列你的弃牌堆,将所有弃牌添加到抽牌堆中,但也将其中的一半保留在弃牌堆中。你抽这些牌,你玩它们,你扔掉它们,它们又回到了弃牌堆中——但是它们已经在弃牌堆中了,所以现在它们是我