c# - 我根据教程制作了一个记忆游戏
问题描述
我正在尝试在您可以在此处找到的教程中制作记忆游戏基础
在我得到这个例外之前,一切都很好:
IndexOutOfRangeException:数组索引超出范围。GameController.addgamepuzzles () (在 Assets/memorygame/scripts/GameController.cs:43)
这是行
gamepuzzles.Add(puzzles[index]);
这是我的实际代码:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour {
[SerializeField]
private Sprite bgimage;
public Sprite[] puzzles;
public List<Sprite> gamepuzzles = new List<Sprite>();
public List<Button> btns = new List<Button>();
void Start () {
GetButttons();
AddListeners();
addgamepuzzles();
puzzles = Resources.LoadAll<Sprite> ("Sprites/Candy");
}
void GetButttons()
{
GameObject[] objects = GameObject.FindGameObjectsWithTag("PuzzleButton");
for(int i = 0; i < objects.Length; i++){
btns.Add(objects[i].GetComponent<Button>());
btns[i].image.sprite = bgimage;
}
}
void addgamepuzzles(){
int looper = btns.Count;
int index = 0;
for (int i = 0; i < looper; i++){
if (index == looper / 2){
index = 0;
}
gamepuzzles.Add(puzzles[index]);
index++;
}
}
void AddListeners(){
foreach (Button btn in btns){
btn.onClick.AddListener(() => PickPuzzle());
}
}
public void PickPuzzle(){
string name = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name;
Debug.Log("you clicked a button with name: " + name);
}
}
这是我的添加按钮脚本
using UnityEngine;
using System.Collections;
public class AddButtons : MonoBehaviour {
[SerializeField]
public Transform PuzzleFiled;
[SerializeField]
private GameObject btn;
void Awake () {
for(int i = 0; i < 8; i++){
GameObject button = Instantiate(btn);
button.name ="" +i;
button.transform.SetParent(PuzzleFiled, false);
}
}
}
这两个脚本附加到一个空的gameObject
呼叫游戏管理器。我还有一个名为 Puzzle Field 的面板,上面附有网格布局元素。我还有一个带有按钮和图像脚本的预制件。
解决方案
好吧事实上,在我关注所有代码之后很容易找到错误,错误是因为我在填充列表之前完成了游戏拼图,我只需要反转行 addgamepuzzles(); 并将其放在拼图 = Resources.LoadAll ("Sprites/Candy"); 像例子一样
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class GameController : MonoBehaviour {
[SerializeField]
private Sprite bgimage;
public Sprite[] puzzles;
public List<Sprite> gamepuzzles = new List<Sprite>();
public List<Button> btns = new List<Button>();
void Start () {
GetButttons();
AddListeners();
puzzles = Resources.LoadAll<Sprite> ("Sprites/Candy");
addgamepuzzles();
}
void GetButttons()
{
GameObject[] objects = GameObject.FindGameObjectsWithTag("PuzzleButton");
for(int i = 0; i < objects.Length; i++){
btns.Add(objects[i].GetComponent<Button>());
btns[i].image.sprite = bgimage;
}
}
void addgamepuzzles(){
int looper = btns.Count;
int index = 0;
for (int i = 0; i < looper; i++){
if (index == looper / 2){
index = 0;
}
gamepuzzles.Add(puzzles[index]);
index++;
}
}
void AddListeners(){
foreach (Button btn in btns){
btn.onClick.AddListener(() => PickPuzzle());
}
}
public void PickPuzzle(){
string name = UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name;
Debug.Log("you clicked a button with name: " + name);
}
}
推荐阅读
- android - 如何在回收站视图中添加 onclciklistner 及其位置
- javascript - 尝试使用 VueJS 过滤数组并仅显示结果
- powershell - 在 PS 脚本中处理 PSSession
- c - "%13s" 或 "%13d" 在 C 中代表什么
- javascript - 是否可以在不了解移动应用程序前端的情况下构建 REST API
- c# - 使用“TAP”设计模式执行许多长时间运行的任务
- sql - 在 WHERE 子句中使用短路会提高速度吗
- spring - 如何在spring boot的主项目(有两个依赖项目)中使用message.properties文件?
- sql - 一行代码中断否则工作查询
- python - 在python中按多个键对字典进行分组