0
\$\begingroup\$

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!

\$\endgroup\$
  • \$\begingroup\$ I understand why it doesn't work, why don't you tell us your understanding, maybe then we'll see your question. \$\endgroup\$ – Joshua Drake May 2 '12 at 14:31
1
\$\begingroup\$

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;
    ....

}
\$\endgroup\$
  • \$\begingroup\$ 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 \$\endgroup\$ – John McDonald May 2 '12 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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