I have a game. It has a Level class for drawing the level, managing blocks, etc. I also have a Tile class for each individual tile (type, x, y), and a block class for the type of block.
I get the error:
Member cannot be accessed with an instance reference; qualify it with a type name instead
In
Texture2D texture = tiles[x, y].blockType.Dirt_Block.texture;
Here is my Tile.cs
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Game.Tiles;
namespace Game
{
public struct Tile
{
public BlockType blockType;
public int X;
public int Y;
/// <summary>
/// Creates a new tile for one position
/// </summary>
/// <param name="blocktype">Type of block</param>
/// <param name="x">X Axis position</param>
/// <param name="y">Y Axis position</param>
public Tile(BlockType blocktype,int x,int y)
{
blockType = blocktype;
X = x;
Y = y;
}
}
}
And block.cs…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Game.Tiles;
namespace Game.Tiles
{
/// <summary>
/// Controls the collision detection and response behavior of a tile.
/// </summary>
enum BlockCollision
{
/// <summary>
/// A passable tile is one which does not hinder player motion at all.
/// </summary>
Passable = 0,
/// <summary>
/// An impassable tile is one which does not allow the player to move through
/// it at all. It is completely solid.
/// </summary>
Impassable = 1,
/// <summary>
/// A platform tile is one which behaves like a passable tile except when the
/// player is above it. A player can jump up through a platform as well as move
/// past it to the left and right, but can not fall down through the top of it.
/// </summary>
Platform = 2,
}
public enum BlockType
{
Dirt_Block = new Block("Dirt Block",,BlockCollision.Impassable,250,0,0)
}
/// <summary>
/// Stores the appearance and collision behavior of a tile.
/// </summary>
public struct Block
{
public static string Name;
public static Texture2D Texture;
public static BlockCollision Collision;
public static int Width = 16;
public static int Height = 16;
public static int maxAmount;
public static int Worth;
public static int Strength;
public static Vector2 Size = new Vector2(Width, Height);
/// <summary>
/// Contructs a new block
/// </summary>
/// <param name="name">Name of the block</param>
/// <param name="texture">Texture to use as image</param>
/// <param name="collision">Collision type</param>
/// <param name="maxamount">Maximum amount of these blocks you can have</param>
/// <param name="worth">How much this block is worth</param>
/// <param name="strength">How hard the block is (how much you will have to hit to mine it)</param>
public Block(string name, Texture2D texture,BlockCollision collision, int maxamount, int worth, int strength)
{
Name = name;
Texture = texture;
Collision = collision;
Width = 16;
Height = 16;
maxAmount = maxamount;
Worth = worth;
Strength = strength;
}
}
}
I’m not even sure if its an efficient way to make a new tile(blockType,x,y) for every block on the grid. Any help would be appreciated. Mainly; How can I get the value of blockType.Dirt_Block.Texture? But I get the error about something not being static or whatever.
EDIT: Looking back at this I feel really stupid, but I will leave it here since obviously 10k views means it has helped someone
This code is not legal, because an enum member can only have an integer value.
Dirt_Blockis a static member of theBlockTypetype, so you can’t access theDirt_Blockmember on an instance ofBlockType.And I think you misunderstand what enums are; enum members can’t have enums of their own.