当前位置:  开发笔记 > 编程语言 > 正文

3D无限亚军游戏平台生成给出了例外

如何解决《3D无限亚军游戏平台生成给出了例外》经验,为你挑选了1个好方法。

情况:我指的是项目的教程,代码中有一个对象池概念.

问题:在我编写生成平台(PlatformManager.cs)脚本之后,我得到一个例外,

InvalidOperationException:由于对象的当前状态,操作无效System.Collections.Generic.Queue`1 [UnityEngine.Transform] .Peek()PlatformManager.Update()(在Assets/Platform/PlatformManager.cs:35)

PlatformManager.cs脚本:

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

public class PlatformManager  : MonoBehaviour {
    public Transform prefab;
    public int numberOfObjects;
    public float recycleOffset;
    public Vector3 startPosition;
    public Vector3 minSize, maxSize,minGap, maxGap;
    public float minY, maxY;
    private Vector3 nextPosition;
    private Queue objectQueue;

    void Start () {
        objectQueue = new Queue(numberOfObjects);
        for (int i = 0; i < numberOfObjects; i++) {
            objectQueue.Enqueue((Transform)Instantiate(prefab));
        }
        nextPosition = startPosition;
        for (int i = 0; i < numberOfObjects; i++) {
            Transform o = (Transform)Instantiate(prefab);
            o.localPosition = nextPosition;
            nextPosition.x += o.localScale.x;
            objectQueue.Enqueue(o);
        }
    }

    void Update () {
        if (objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled)//THIS IS THE 35TH LINE WHICH CAUSE THE ERROR
     {
            Transform o = objectQueue.Dequeue();
            o.localPosition = nextPosition;
            nextPosition.x += o.localScale.x;
            objectQueue.Enqueue(o);
            Recycle();
        }
    }

    private void Recycle () {
    Vector3 scale = new Vector3(
            Random.Range(minSize.x, maxSize.x),
            Random.Range(minSize.y, maxSize.y),
            Random.Range(minSize.z, maxSize.z));

        Vector3 position = nextPosition;
        position.x += scale.x * 0.5f;
        position.y += scale.y * 0.5f;

        Transform o = objectQueue.Dequeue();
        o.localScale = scale;
        o.localPosition = position;
        nextPosition.x += scale.x;
        objectQueue.Enqueue(o);

        nextPosition += new Vector3(
            Random.Range(minGap.x, maxGap.x) + scale.x,
            Random.Range(minGap.y, maxGap.y),
            Random.Range(minGap.z, maxGap.z));

        if(nextPosition.y < minY){
            nextPosition.y = minY + maxGap.y;
        }
        else if(nextPosition.y > maxY){
            nextPosition.y = maxY - maxGap.y;
        }
    }
}

提前致谢.



1> 小智..:

确保队列不为空.

if(objectQueue.Count != 0) 
{ 
     if (objectQueue.Peek().localPosition.x + recycleOffset < Runner.distanceTraveled)//THIS IS THE 35TH LINE WHICH CAUSE THE ERROR
     {
            Transform o = objectQueue.Dequeue();
            o.localPosition = nextPosition;
            nextPosition.x += o.localScale.x;
            objectQueue.Enqueue(o);
            Recycle();
        }
}

推荐阅读
N个小灰流_701
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有