无法使用实例引用访问成员;用类型名称来限定它

时间:2012-03-03 20:19:20

标签: c# static xna

我有一个游戏。它有一个Level类用于绘制关卡,管理块等。我还为每个单独的tile(类型,x,y)都有一个Tile类,以及一个块类型的块类

我收到错误:

Member cannot be accessed with an instance reference; qualify it with a type name instead

Texture2D texture = tiles[x, y].blockType.Dirt_Block.texture;

这是我的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;
    }
}
}

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

我甚至不确定它是否是为网格上的每个块制作new tile(blockType,x,y)的有效方法。任何帮助,将不胜感激。主要表现;如何获得blockType.Dirt_Block.Texture的值?但我得到的错误是关于某些不是静态的东西。

http://pastebin.com/s50AB6kz

编辑:回头看这个我真的很蠢,但我会把它留在这里,因为很明显10k的观点意味着它帮助了某人

2 个答案:

答案 0 :(得分:4)

static个变量对于该类型的所有实例都是相同的。把所有static从Block上取下来你应该没问题。

同时分别制作BlockTypeBlock class es而不是enumstruct,因为它们既不是enum也不是struct秒。

答案 1 :(得分:1)

此代码不合法​​,因为枚举成员只能有一个整数值。

public enum BlockType
{
     Dirt_Block = new Block("Dirt Block",,BlockCollision.Impassable,250,0,0)
}

Dirt_BlockBlockType类型的静态成员,因此您无法访问Dirt_Block实例上的BlockType成员。

我认为你误解了枚举是什么;枚举成员不能拥有自己的枚举。