Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

[.net] XNA Dynamic Array Problem

Started by zaff Jan 10, 2008 at 11:07 AM 1 replies 1k views
Original Post
zaff
zaff
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)
gharen2
gharen2
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.
Machaira
Machaira
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                }            }        }    }
Former Microsoft XNA and Xbox MVP | Check out my blog for random ramblings on game development

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.