Jump to content
  • Advertisement
Sign in to follow this  
zaff

[.net] XNA Dynamic Array Problem

This topic is 3905 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Well ill give you some background, I am making a Breakout Clone and it is pretty much finished theres just one snag.... Theres one level. Like my usual self I didnt sit back and plan it out first just started hacking code together. Now the problem I have the following : - int rows = 3; //Number of rows depending on level bool[,] blocksBool = new bool[8,rows]; //To Make the brick go away Vector2[,] blockPos = new Vector2[8,rows]; // position of the bricks BoundingBox[,] boxBounce = new BoundingBox[8,rows]; //For the bounding box Now the error message says i cant reference a nonstatic field, method or property into the arrays. (I know I cant but I need them to change depending on the level :S) I have code accessing the arrays in several different functions however so I cannot just up and move the declarations or I start to get errors all over the place (as I would expect haha). Help please :) the problem might be staring me in the face but ive had hardly any sleep as im trying to get my sleeping patterns back in order for when i go back to Uni on monday. (oh deep joy)

Share this post


Link to post
Share on other sites
Advertisement
Instead of having arrays declared globally and accessible from anywhere, encapsulate them inside a class. Pass the parameters you want to the class through it's constructor, where you create the arrays based on those parameters.

When you want to create a new round, create a new instance of that class. Either store it globally (which isn't good practice but is easy and will work) or pass it around to wherever it's needed.

Share this post


Link to post
Share on other sites
What about something like:


public class Brick
{
private BoundingBox _box;
private Vector2 _location;
private bool _alive;

public Brick(Vector2 location)
{
_location = location;
_box = new BoundingBox(new Vector3(location.X, location.Y, 0), new Vector3(location.X * Level.BrickWidth, location.Y * Level.BrickHeight, 0));
_alive = true;
}

public Vector2 Location
{
get { return _location; }
}

public bool Alive
{
get { return _alive; }
}

public BoundingBox Box
{
get { return _box; }
}

public void Kill()
{
_alive = false;
}
}

public class Level
{
public const int BrickWidth = 32;
public const int BrickHeight = 8;

private List<Brick> _bricks;

public Level(int rows, int cols)
{
_bricks = new List<Brick>();

Brick brick;

for (int x = 0; x<cols;x++)
{
for (int y = 0;y<rows;y++)
{
brick = new Brick(new Vector2(x * BrickWidth, y * BrickHeight));
_bricks.Add(brick);
}
}
}

public void LevelUp(int rows, int cols)
{
_bricks.Clear();

Brick brick;

for (int x = 0; x < cols; x++)
{
for (int y = 0; y < rows; y++)
{
brick = new Brick(new Vector2(x * BrickWidth, y * BrickHeight));
_bricks.Add(brick);
}
}
}

public bool CheckForCollision(BoundingBox ball)
{
BoundingBox brick;
Vector2 location;
bool ret = false;

for (int i = 0; i < _bricks.Count; i++)
{
if (_bricks.Alive)
{
ret = ball.Intersects(_bricks.Box);

if (ret)
{
_bricks.Kill();
break;
}
}
}

return ret;
}

public void Render()
{
for (int i = 0; i < _bricks.Count; i++)
{
if (_bricks.Alive)
{
//render
}
}

}
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!