简体   繁体   English

如何在C#的listDictionary中存储Texture2D?

[英]How to store Texture2D in a listDictionary in C#?

I am having problems with retrieving a texture2D from a listDictionary. 我在从listDictionary检索texture2D时遇到问题。

This is my LoadGraphics Class: 这是我的LoadGraphics类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Media;

namespace Space_Game
{
    public static class LoadGraphics
    {
        //global variable
        public static ListDictionary _blue_turret_hull;

        public static void LoadContent(ContentManager contentManager)
        {
            //loads all the graphics/sprites
            _blue_turret_hull = new ListDictionary();
            _blue_turret_hull.Add("graphic", contentManager.Load<Texture2D>("Graphics/Team Blue/Turret hull spritesheet"));
            _blue_turret_hull.Add("rows", 1);
            _blue_turret_hull.Add("columns", 11);
        }
    }
}

This is the Turret class the should retrieve the Texture2D: 这是Turret类,应该检索Texture2D:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Space_Game
{
    class Turret_hull:GameObject
    {
        public Turret_hull(Game game, String team)
            : base(game)
        {
            if(team == "blue") { _texture = LoadGraphics._blue_turret_hull["graphic"];      }
        }
    }
}

Only here it gives the following error: 仅在这里,它会出现以下错误:

Cannot implicitly convert type 'object' to 'Microsoft.Xna.Framework.Graphics.Texture2D'. 无法将类型“对象”隐式转换为“ Microsoft.Xna.Framework.Graphics.Texture2D”。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)

I know there is a problem regarding the fact that I stored it in a listDictionary. 我知道将其存储在listDictionary中存在一个问题。 I did that, because that way I could retrieve all necessary info at once. 我这样做是因为这样可以立即检索所有必要的信息。 How else should I do this? 我还应该怎么做?

Thanks in Advance, 提前致谢,

Mark Dijkema 马克·迪凯玛

ListDictionary is not generic, so the items are stored as Objects. ListDictionary不是通用的,因此项目存储为对象。 You have to cast them back to the type you want: 您必须将它们转换回所需的类型:

if(team == "blue") { _texture = (Texture2D)LoadGraphics._blue_turret_hull["graphic"];      }

Probably a better way to achieve what you want is to define a new class with 3 properties and use this to retrieve the info you need to pass around: 实现所需目标的更好方法是定义一个具有3个属性的新类,并使用该类检索需要传递的信息:

public class TurretHullInfo
{
    Texture2D Graphic { get; set; }
    int Rows { get; set; }
    int Columns { get; set; }
}

public static class LoadGraphics
{
    //global variable
    public static TurretHullInfo _blue_turret_hull;

    public static void LoadContent(ContentManager contentManager)
    {
        //loads all the graphics/sprites
        _blue_turret_hull = new TurretHull();
        _blue_turret_hull.Graphic = contentManager.Load<Texture2D>("Graphics/Team Blue/Turret hull spritesheet");
        _blue_turret_hull.Rows = 1;
        _blue_turret_hull.Columns = 11;
    }
}

class Turret_hull : GameObject
{
    public Turret_hull(Game game, String team)
        : base(game)
    {
        if(team == "blue")
            _texture = LoadGraphics._blue_turret_hull.Graphic;     
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM