【发布时间】:2023-04-05 07:51:01
【问题描述】:
我是编码新手,答案可能很简单。 我正在统一制作一个配对记忆游戏,我希望每次玩家加载游戏时卡片随机产生。我有一个卡片的游戏对象列表和一个我希望卡片生成的生成位置的变换列表。 到目前为止,我已经设法将卡片随机洗牌。但要做到这一点,我已经将纸牌游戏对象列表转换为整数。
public List<GameObject> cardsToSpawn;
public List<Transform> cardPositions = new List<Transform>();
public int cardCount;
public int someNum;
public List<GameObject> cards;
public GameObject empty;
void Awake()
{
// list of the card gameobjects.
List<int> cardsToSpawn = new List<int>();
cardsToSpawn.Add(1);
cardsToSpawn.Add(2);
cardsToSpawn.Add(3);
cardsToSpawn.Add(4);
//Randomises the order. A, B, C, D = C, D, B, A for example.
//cardsToSpawn is assigned a random number (1-18(length of cardsToSpawn list)).
for (int i = 0; i < cardsToSpawn.Count; i++)
{
int temp = cardsToSpawn[i];
int randomIndex = Random.Range(i, cardsToSpawn.Count);
cardsToSpawn[i] = cardsToSpawn[randomIndex];
cardsToSpawn[randomIndex] = temp;
//spawn card i=1 in cardPositions[1], card i=2 in cardPositions[2]...
temp = cards
Instantiate(cardsToSpawn[i], cardPositions[i].position, transform.rotation);
// but this refers back to the original list declared at the beginning of the script rather than the new randomIndex int.
}
// randomises the order. A, B, C, D = C, D, B, A for example.
//tell me the order (i.e. make sure it is randomised.)
cardsToSpawn.ForEach(i => Debug.Log("{0}\t"+ i));
}
【问题讨论】: