【问题标题】:'Unexpected symbol 'void' expecting ; { => or where' Unity2D Tower Defense game'意外的符号'void'期待; { => or where' Unity2D 塔防游戏
【发布时间】:2018-01-08 11:18:42
【问题描述】:

我一直在尝试创建一个Unity2D塔防游戏,'Unexpected symbol'void'expecting似乎有问题; { => or where' 在 'string[] LoadLevelText (int i)' 之后(第 71 行)。我正在学习一个教程,我做了所有的事情,但是这个错误似乎无法修复,因为它只会引发更多问题。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class LvlManagerScr : MonoBehaviour {


    public int fieldWidth, fieldHeight;

    public GameObject cellPref;
    public Transform cellParent;

    public Sprite[] tileSpr = new Sprite[2];

    public List<GameObject> wayPoints = new List<GameObject>();
    GameObject[,] allCells = new GameObject[10, 22];

    int currWayX, currWayY;
    GameObject firstCell;


    void Start () {
        CreateLevel();
        LoadWaypoints();
    }

    void CreateLevel()
    {
        Vector3 worldVec = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height, 0));

        for (int i = 0; i < fieldHeight; i++)
            for(int k = 0; k < fieldWidth; k++)
            {
                int sprIndex = int.Parse(LoadLevelText(1)[i].ToCharArray()[k].ToString());
                Sprite spr = tileSpr[sprIndex];

                bool isGround = spr == tileSpr[1] ? true : false;

                CreateCell(isGround, spr, k, i, worldVec);
            }
    }

    void CreateCell(bool isGround, Sprite spr, int x, int y, Vector3 wV)
    {
        GameObject tmpCell = Instantiate(cellPref);
        tmpCell.transform.SetParent(cellParent, false);

        tmpCell.GetComponent<SpriteRenderer>().sprite = spr;

        float sprSizeX = tmpCell.GetComponent<SpriteRenderer>().bounds.size.x;
        float sprSizeY = tmpCell.GetComponent<SpriteRenderer>().bounds.size.y;

        tmpCell.transform.position = new Vector3 (wV.x + (sprSizeX * x), wV.y + (sprSizeY * -y));

        if (isGround)

        {
            tmpCell.GetComponent<CellScr>().isGround = true;

            if (firstCell == null)
            {
                firstCell = tmpCell;
                currWayX = x;
                currWayY = y;
            }
        }

        allCells[y, x] = tmpCell;

    }


    string[] LoadLevelText (int i)

    void LoadWaypoints()
    {
        GameObject currWay60;
        wayPoints.Add(firstCell);

        while (true) 
        {
            currWay60 = null;

            if (currwayX > 0 && allCells [currWayY, currWayX - 1].GetComponent<CellScr> ().isGround &&
               !wayPoints.Exists (x => x == allCells [currWayY, currWayX - 1])) 
            {
                currWay60 = allCells [currWayY, currWayX - 1];
                currWayX--;
                Debug.Log ("Next Cell is Left");
            } 
            else if (currwayX < 0 (fieldWidth - 1) && allCells [currWayY, currWayX + 1].GetComponent<CellScr> ().isGround &&
                    !wayPoints.Exists (x => x == allCells [currWayY, currWayX - 1])) 
            {
                currWay60 = allCells [currWayY, currWayX + 1];
                currWayX++;
                Debug.Log ("Next Cell is Right");
            } 
            else if (currWayY > 0 && allCells [currwayY - 1, currWayX].GetComponent<CellScr> ().isGround &&
                     !wayPoints.Exists (x => x == allCells [currWayY - 1, currWayX])) 
            {
                currWay60 = allCells [currWayY - 1, currWayX];
                currWayY--;
                Debug.Log ("Next Cell is Up");
            } 
            else if (currWayY < 0 (fieldHeight - 1) && allCells [currwayY + 1, currWayX].GetComponent<CellScr> ().isGround &&
                     !wayPoints.Exists (x => x == allCells [currWayY - 1, currWayX])) 
            {
                currWay60 = allCells [currWayY + 1, currWayX];
                currWayY++;
                Debug.Log ("Next Cell is Down");
            } 
            else
                break;

            wayPoints.Add (currWay60);

        }

    }               
}

在教程中似乎有一个带有省略号的框:

【问题讨论】:

  • 复制并发布该脚本中的所有内容。这将帮助人们为您发现错误。
  • LoadLevelText 方法缺少正文。
  • (……你的代码块中的大多数行,尤其是最后两行,都缺少另外四个缩进空格。)(欢迎来到 SO!(及其有趣的标记))

标签: c# unity3d unity5


【解决方案1】:

线

string[] LoadLevelText (int i)

看起来不完整!

尝试将其替换为以下内容,看看它现在是否有效:

string[] LoadLevelText(int i)
{
   return null;
}

您可能需要弄清楚 LoadLevelText() 应该做什么才能完成您的代码。

【讨论】:

    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-10
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多