简体   繁体   中英

How to store Texture2D in a listDictionary in C#?

I am having problems with retrieving a texture2D from a listDictionary.

This is my LoadGraphics Class:

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:

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:

I know there is a problem regarding the fact that I stored it in a 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. 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:

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;     
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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