c# - 返回 float 组时出现错误

标签 c# game-engine

当我尝试返回 float 组时,出现此错误:

Cannot implicitly convert type 'float[]' to 'float'

我正在使用 OpenTK 用 C# 制作 2d 游戏引擎 我正在研究顶点类,但我不断收到错误并且陷入困境。

我已经尝试过这个:

return data[vertices.Length * Size];

代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenTK;

namespace RacerEngine
{
    public class Vertex
    {
        private const int Size = 2;

        private Vector2 position;
        public Vector2 Position
        {
            get
            {
                return position;
            }
            set
            {
                position = value;
            }
        }

        public Vertex(Vector2 position)
        {
            this.position = position;
        }

        public static float Process(Vertex[] vertices)
        {
            float[] data = new float[vertices.Length * Size];

            for(int i = 0; i < vertices.Length; i++)
            {
                data[i] = vertices[i].position.X;
                data[i + 1] = vertices[i].position.Y;
            }

            return data;
        }
    }
}

最佳答案

您的方法Process被定义为返回一个float,但您的return语句从中返回一个float []。根据您的需要,将方法签名更改为

public static float[] Process(Vertex[] vertices)

或者从方法中返回一个float

关于c# - 返回 float 组时出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429498/

相关文章:

c# - 数组中抽象类的派生类

android - 如何仅针对 Android 和 iOS 设置 libGDX 项目?

c++ - C 语言还在游戏引擎中被广泛使用吗?

c# - 如何在 Windows 中通过 C# 执行 msg.exe?

c# - Microsoft C# 字符串文档 : Am I misinterpreting what I read, 还是文档有误?

c# - 如何在 C# 中使用 XML 序列化包含元素(不是根)的属性

audio - 不能包含XACT3 header

haskell - 如何在 Haskell 中最好地同步游戏引擎和网络服务器?

Java自定义像素渲染引擎bug

c# - 为什么 .NET 不解析某些带有嵌入式 IPv4 值的 IPv6?