When I try to cast an Item as an ImageItem in fails to convert and returns null, hence skipping the code I want to use to extract the texture data. I have already down cast my Item to an ImageItem, and set my TextureItem to public, so I am struggling to see the solution.
Any advice would be greatly appreciated.
Game 1 (Main Class)
ImageItem Downcast
public class ImageItem : Item
{
public Texture2D Texture;
public List<Color[]> itemTextureDataList = new List<Color[]>();
public List<Rectangle> itemRectangleList = new List<Rectangle>();
}
Assign level textures color data to array
foreach (Layer layer in level.Layers)
{
foreach (Item item in layer.Items)
{
ImageItem imageItem = item as ImageItem;
if (imageItem != null)
{
Texture2D texture = imageItem.Texture;
imageItemList[iImageItemNum].itemTextureDataList[0] = new Color[imageItem.Texture.Width * imageItem.Texture.Height];
imageItem.Texture.GetData(imageItemList[iImageItemNum].itemTextureDataList[0]);
iImageItemNum++;
}
}
}
Create a rectangle for every level texture
foreach (Layer layer in level.Layers)
{
foreach (Item item in layer.Items)
{
ImageItem imageItem = item as ImageItem;
if (imageItem != null)
{
imageItemList[iImageRectNum].itemRectangleList[0] = new Rectangle((int)imageItem.Position.X, (int)imageItem.Position.Y, imageItem.Texture.Width, imageItem.Texture.Height);
iImageRectNum++;
}
}
}
Level Class
public partial class TextureItem : Item
{
/// <summary>
/// The item's rotation in radians.
/// </summary>
public float Rotation;
/// <summary>
/// The item's scale vector.
/// </summary>
public Vector2 Scale;
/// <summary>
/// The color to tint the item's texture with (use white for no tint).
/// </summary>
public Color TintColor;
/// <summary>
/// If true, the texture is flipped horizontally when drawn.
/// </summary>
public bool FlipHorizontally;
/// <summary>
/// If true, the texture is flipped vertically when drawn.
/// </summary>
public bool FlipVertically;
/// <summary>
/// The path to the texture's filename (including the extension) relative to ContentRootFolder.
/// </summary>
public String texture_filename;
/// <summary>
/// The texture_filename without extension. For using in Content.Load<Texture2D>().
/// </summary>
public String asset_name;
/// <summary>
/// The XNA texture to be drawn. Can be loaded either from file (using "texture_filename")
/// or via the Content Pipeline (using "asset_name") - then you must ensure that the texture
/// exists as an asset in your project.
/// Loading is done in the Item's load() method.
/// </summary>
public Texture2D texture;
/// <summary>
/// The item's origin relative to the upper left corner of the texture. Usually the middle of the texture.
/// Used for placing and rotating the texture when drawn.
/// </summary>
public Vector2 Origin;
public TextureItem()
{
}
/// <summary>
/// Called by Level.FromFile(filename) on each Item after the deserialization process.
/// Loads all assets needed by the TextureItem, especially the Texture2D.
/// You must provide your own implementation. However, you can rely on all public fields being
/// filled by the level deserialization process.
/// </summary>
public override void load(ContentManager cm)
{
//throw new NotImplementedException();
//TODO: provide your own implementation of how a TextureItem loads its assets
//for example:
//this.texture = Texture2D.FromFile(<GraphicsDevice>, texture_filename);
//or by using the Content Pipeline:
this.texture = cm.Load<Texture2D>(asset_name);
}
item as ImageItem
returns null becauseitem
is not anImageItem
. What leads you to believe otherwise? When you hover over the variable in the debugger, what type does it claim to be? \$\endgroup\$Base
into a variable of typeDerived
, but only if the object referenced by the variable is actually of typeDerived
. \$\endgroup\$