Mitglied kann nicht zugegriffen werden, mit einer Instanz-Referenz; es qualifizieren mit einem Typ-Namen statt
Habe ich ein Spiel. Es hat eine Level
Klasse für Zeichnung an der Ebene, die Verwaltung von Blöcken, etc. Ich habe auch ein Tile
Klasse für jede einzelne Fliese (Typ, x, y), und eine block-Klasse für den Typ des Blocks.
Bekomme ich die Fehlermeldung:
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;
Hier ist meine Fliese.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;
}
}
}
Und 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;
}
}
}
Ich bin mir auch nicht sicher, ob es eine effiziente Möglichkeit, um eine new tile(blockType,x,y)
für jeden block in der Startaufstellung. Jede Hilfe würde geschätzt werden. Vor allem; Wie kann ich den Wert von blockType.Dirt_Block.Texture
? Aber ich bekomme den Fehler über etwas, das nicht statisch, oder was auch immer.
EDIT: im Rückblick auf diese, ich fühle mich wirklich dumm, aber ich will es hierbei belassen, denn offensichtlich 10k views bedeutet, dass es jemandem geholfen hat
Offensichtlich meine Art nicht funktionieren würde.. wo zum nächsten?
InformationsquelleAutor Cyral | 2012-03-03
Du musst angemeldet sein, um einen Kommentar abzugeben.
Dieser code ist nicht legal, da ein enum-Mitglied kann nur ein ganzzahliger Wert.
Dirt_Block
ist ein statischer member derBlockType
geben, so können Sie nicht auf dieDirt_Block
Mitglied auf eine Instanz vonBlockType
.Und ich denke, Sie missverstehen, was Enumerationen sind; enum-Mitglieder können nicht Enumerationen, die von Ihrer eigenen.
Machen BlockType ein Klasse-als vielmehr um eine enum, und deklarieren Sie eine Textur-Eigenschaft in es
Ah, warum nicht, denke ich!
Also vielleicht so etwas?
public class BlockType { public BlockType Dirt_Block = new Block("Dirt Block",,BlockCollision.Impassable,250,0,0);
Wie kann ich die Variablen aus der Blocktype? }Sehen, was Ich geändert in pastebin.com/erDeg7Dn (zu groß zum hier posten)
InformationsquelleAutor Thomas Levesque
static
Variablen sind die gleichen für ALLE Instanzen dieses Typs. Nehmen Sie allestatic
s aus Block, und Sie sollten in Ordnung sein.Machen auch
BlockType
undBlock
class
es statt einerenum
undstruct
, beziehungsweise, da Sie wederenum
s nochstruct
s.InformationsquelleAutor D Stanley