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'm having some issues in XNA. I have a Player class and I would like to call a foreach function from a list which has been declared in the Game1 main class, which is below:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    // Player
    Player player;
    // Landscape
    public List<Tile> tiles = new List<Tile>();

The following is the code I'm using and within it is the foreach:

if (isJumping == true)
        {
            foreach (Tile tile in tiles)
            {
                if (recTopBot.Intersects(tile.rectangle))
                {
                    position.Y += velocity.Y * curTime;
                    velocity.Y += gravity * curTime;
                }

                else
                {
                    position.Y = 320 - texture.Height;
                    velocity.Y = 0;
                    isJumping = false;
                }
            }
        }

It says that the name "tiles" does not exist in the current context. Now, I understand why it doesn't work, but what's the best way to fix it without modifying the classes and ruining my algorithms?

That chunk is used for detecting bottom collision for the tiles that are added in my world to make jumping + gravity work as intended.

Thank you very much!

share|improve this question
    
I understand why it doesn't work, why don't you tell us your understanding, maybe then we'll see your question. –  Joshua Drake May 2 '12 at 14:31

1 Answer 1

up vote 1 down vote accepted

You can make it static:

 public static List<Tile> tiles = new List<Tile>();

In your player:

 if (isJumping == true)
    {
        foreach (Tile tile in Game1.tiles)
        {

Although is not an elegant solution... I'd prefer to store the Game1 instance in your Player class

public class Player()
{ 
   public Game1 Game; 
   ....

   if (isJumping == true)
   {
      foreach (Tile tile in Game.tiles)
      {
       ....
}

Player player = new Player() { Game = this; }  // From the game class

And you should at the end use a Environment or World class:

publica class Environment
{
    public static readonly Environment Instance = new Environment();

    public Game Game;
    public LevelCollection Levels;
    public PlayerCollection Players;
    public Level CurrentLevel;
    ....

}
share|improve this answer
    
yeah... this is probably the answer @Cosmin wants, but I would recommend treating Game1 as an instance and pass it around: player = new Player(this); then foreach (Tile tile in theGameInstance.tiles). Thanks for saying so in the edit, :) +1 –  John McDonald May 2 '12 at 14:37
    
Wow, I wasn't expecting this :) Thanks a lot! –  Cosmin Negoita May 2 '12 at 14:37

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.