Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I am trying to use an accessor, as it seems to me that that is the only way to accomplish what I want to do. Here is my code:

Game1.cs

public class GroundTexture
{
    private Texture2D dirt;
    public Texture2D Dirt
    {
        get
        {
            return dirt;
        }

        set
        {
            dirt = value;
        }
    }
}

public class Main : Game
{
    public static Texture2D texture = tile.Texture;
    GroundTexture groundTexture = new GroundTexture();
    public static Texture2D dirt;

    protected override void LoadContent()
    {
        Tile tile = (Tile)currentLevel.GetTile(20, 20);

        dirt = Content.Load<Texture2D>("Dirt");
        groundTexture.Dirt = dirt;

        Texture2D texture = tile.Texture;
    }

    protected override void Update(GameTime gameTime)
    {
        if (texture == groundTexture.Dirt)
        {
            player.TileCollision(groundBounds);
        }

        base.Update(gameTime);
    }

}

I removed irrelevant information from the LoadContent and Update functions.

On the following line:

if (texture == groundTexture.Dirt)

I am getting the error

Operator '==' cannot be applied to operands of type 'Microsoft.Xna.Framework.Graphics.Texture2D' and 'Game1.GroundTexture'

Am I using the accessor correctly? And why do I get this error? "Dirt" is Texture2D, so they should be comparable.

This using a few functions from a program called Realm Factory, which is a tile editor. The numbers "20, 20" are just a sample of the level I made below:

enter image description here

tile.Texture returns the sprite, which here is the content item Dirt.png

share|improve this question
    
All I can say so far, is that I tried this and I could compare Texture2D references just fine. I really can't see why groundTexture.Dirt would be treated as a GroundTexture in your example.. –  David Gouveia Dec 16 '12 at 23:19
    
Have you tried this in an empty project in order to isolate where the problem might be? –  David Gouveia Dec 16 '12 at 23:21
    
If groundTexture.Dirt is indeed a Texture2D, why not cast it to Texture2D and then comparing them? –  Panda Pajama Jan 16 '13 at 6:31
add comment

2 Answers

From what I'm understanding you want to check what Tile you're trying to walk over. You shouldn't check textures. You should compare tile ID. In the end the Texture is just a visual presentation of the tile context ( in this case "dirt" ).

Like so:

//Somewhere you see fit
enum TileType{None, Wall, Water, Dirt, Grass};

//When comparing it:
if(tile.ID == TileType.Dirt) // Do something

ID in this case is an enum-type of TileType. So when you load you're level you simply assign the type to the tile:

Tile myTile = new Tile();
mytile.ID = TileType.Dirt;   

But have you tried it like

Texture2D dirt = groundTexture.Dirt;
if (texture == dirt)

But again if you want to know the context of your tiles I recommand using Enums or something similar.

It's also easier if you reference to one texture file at all time instead of creating new Dirt textures. Comparing 2 different references will result as false. I'm not sure how the Contentloader from XNA handles this but i'm just sharing just in case.

share|improve this answer
1  
I would like to add to this answer that it is a good idea to separate the physical properties of a tile from the visual properties, because when you expand your game, you will surely have a lot of tiles which use different textures but behave identically. When you keep implementing logic based on the visual appearance, adding a new textures which doesn't do anything new will require changes all over your sourcecode, and you will have constructs like if (texture == Dirt1 || texture == Dirt2 || texture = Wall || texture = StoneWall ...) instead of just a simple if (physics == SOLID). –  Philipp Jan 16 '13 at 9:02
    
@Philipp, +1. Exactly what I was going for. –  Sidar Jan 16 '13 at 9:11
add comment

Have you tried Cleaning and Rebuilding? Cause Like David said, I can't see any problems either.

If that doesn't help I would also see if the type does in fact return a Texture2D by print it out.

hope that helps

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.