john Mc kenna

john Mc kenna

  • NA
  • 11
  • 14.2k

Using the same instance of an object across diff classes.

Feb 22 2012 10:11 AM
Hi Guys..So ive used a tutorial to allow clickable objects to be dragged and dropped around the screen (XNA game)...
The level is initally loaded which then loads each object seperatly.  Each draggable object has a class of its own.

The problem being im using a mouse class which allows the objects to be dragged and dropped however im loading a new mouse class from inside each object being dragged around the screen.  When I should be loading it once from the level.cs(which works), but then how do I share it and call it from each tile?

So if you take my draggable class atm(with each instance of a new mouse being loaded)...which includes the performMouseInteractions(gameTime); and PerformNormalUpdate(gameTime); methods.

    class LetterE
    {

        public int PointValue;

        Mouse mouse;
        ClickablePlayer TileE;
        List<ClickablePlayer> clickableObjects;

        //Reference XNA framework
        Game gameReference;

        //Textures for images
        private Texture2D TileE1;

        public int x;
        public int y;

        public static bool addLetterScore = true;
        public static bool allowEmove = false;
        public static bool ePlaced = false;
        MouseState previousState, currentState;

        //pos of tile on board
        public Rectangle RectangleOfBoard
        {
            get;
            set;
        }

        //Call Content       
        public ContentManager Content
        {
            get { return content; }
        }
        ContentManager content;

        public LetterE(Game theGame)
        {
            content = theGame.Content;
            this.gameReference = theGame;
            LoadContent();

            PointValue = 2;

        }

        public void LoadContent()
        {
            mouse = new Mouse(content);

            TileE1 = Content.Load<Texture2D>("Letters/e");
            TileE = new ClickablePlayer(TileE1);
            TileE.Position = new Vector2(1065, 475);

            clickableObjects = new List<ClickablePlayer>(10);
            clickableObjects.Add(TileE);
        }

        public void checkPosition()
        {
            RectangleOfBoard = new Rectangle(601, 401, 40, 40);

            x = currentState.X;
            y = currentState.Y;


            if (RectangleOfBoard.Contains(x, y) && Mouse.mytestbool == false)   // PositionC is within RectangleOfBoard, so act accordingly.
            {
                allowEmove = false;
                ePlaced = true;
            }
        }

        public void Update(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Level.whosTurnIsIt();
            mouse.Update();
            if (allowEmove)
            {
                PerformMouseInteractions(gameTime);
                PerformNormalUpdate(gameTime);
            }

            currentState = Microsoft.Xna.Framework.Input.Mouse.GetState();

            checkPosition();
        }


        public void PerformNormalUpdate(GameTime gameTime)
        {
            foreach (ClickableGameplayObject cgo in clickableObjects)
            {
                if (cgo != mouse.ClickedObject)
                {
                    cgo.Update(gameTime);
                }
            }
        }

        public void PerformMouseInteractions(GameTime gameTime)
        {
            foreach (ClickableGameplayObject cgo in clickableObjects)
            {
                if (mouse.ClickedObject == null)
                {
                    cgo.Update(gameTime, mouse);
                    if (cgo.ActiveMouse != null)
                    {

                        return;
                    }
                }
                else if (mouse.ClickedObject != null)
                {
                    mouse.ClickedObject.Update(gameTime, mouse);

                    return;
                }
            }
        }


        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            TileE.Draw(gameTime, spriteBatch);

        }
    }
}



So im transfering everything related to the perform mouse updates over to the level.
but then how do i call it from the moveable object class?

Iv tried using a static and calling level.mouse.update,
but then i had to load the level again from the object?

Im making this very complicated for myself could someone please advise?

Thanks



Answers (1)