Jump to content
  • Advertisement
Sign in to follow this  
jadendecar

foreach list always uses last value

This topic is 2021 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

So, I'm using a foreach to update all the Button objects I place on the screen at once. They are all placed in a list and are executed as so:

 

foreach(Button button in Buttons)
    button.update(activeMouse);

 

 

the method checks if the button is being clicked and if so changes it's text. For some reason, when I click on any button, it always changes the text of the last Button object in the list.

Share this post


Link to post
Share on other sites
Advertisement

Try this:

 

foreach(Button button in Buttons)
{
    Button b = button; // add this to "copy" the current button
    b.update(activeMouse);
}

Share this post


Link to post
Share on other sites

Show the code that adds the buttons to the list. I suspect that you aren't allocating a new instance for each button, but are reusing a single button instance.

 

@tharealjohn - that code would make no difference at all.

Share this post


Link to post
Share on other sites

@tharealjohn I had already tried that as well as a regular for loop to no avail.

 

@Dave Hunt

 

Edit: Notcied the Button constructor was missing the arguments.

 

This is how I add a button (inside of a method);

Button button = new Button(this, rectangle, title);
Buttons.Add(button);
Edited by Kurenis

Share this post


Link to post
Share on other sites

The Button constructor:

        public Button(Object obj, Rectangle relativeRectangle, string title)
            : base()
        {
            Rectangle = obj.Rectangle;
            RelativeRectangle = relativeRectangle;
            Visible = true;
            Title = title;
        }

 

and the update method:

        public void Update(Object obj, Mouse activeMouse)
        {
            Rectangle = obj.Rectangle;
            base.Update(activeMouse);
        }

Share this post


Link to post
Share on other sites

Deeper and deeper...

 

The code that actually uses the mouse information and updates the button's text appears to be somewhere in a base class. That's the code we need to see.

 

Also, the code you show for calling Button.Update doesn't match the signature of the Button.Update method. Your Button.Update method takes an Object and a Mouse, but your calling function only shows passing a Mouse...

Share this post


Link to post
Share on other sites

Fixed the update call in the foreach (no change). The base update code is as follows:

public void Update(Mouse activeMouse)
        {
            bool intersect = activeMouse.Rectangle.Intersects(Rectangle);

            if (intersect || (ActiveMouse != null && (LeftState != ObjectClickedState.Normal || RightState != ObjectClickedState.Normal)))
            {
                ActiveMouse = activeMouse;
                ActiveMouse.ClickedObject = this;
                if (intersect)
                    OnHover();
                if (ActiveMouse.LeftPress && (LeftState == ObjectClickedState.Normal || LeftState == ObjectClickedState.Released))
                {
                    LeftState = ObjectClickedState.Clicked;
                    OnLeftClick();
                }
                else if (ActiveMouse.LeftPress && LeftState == ObjectClickedState.Clicked)
                    OnLeftPress();
                else if (ActiveMouse.LeftRelease && LeftState == ObjectClickedState.Clicked)
                {
                    LeftState = ObjectClickedState.Released;
                    OnLeftRelease();
                }
                else if (!ActiveMouse.LeftPress && LeftState == ObjectClickedState.Released)
                {
                    LeftState = ObjectClickedState.Normal;
                    OnLeftNormal();
                }

                if (ActiveMouse.RightPress && (RightState == ObjectClickedState.Normal || RightState == ObjectClickedState.Released))
                {
                    RightState = ObjectClickedState.Clicked;
                    OnRightClick();
                }
                else if (ActiveMouse.RightPress && RightState == ObjectClickedState.Clicked)
                    OnRightPress();
                else if (ActiveMouse.RightRelease && RightState == ObjectClickedState.Clicked)
                {
                    RightState = ObjectClickedState.Released;
                    OnRightRelease();
                }
                else if (!ActiveMouse.RightPress && RightState == ObjectClickedState.Released)
                {
                    RightState = ObjectClickedState.Normal;
                    OnRightNormal();
                }
            }
            else
            {
                if (ActiveMouse != null)
                {
                    ActiveMouse.ClickedObject = null;
                    ActiveMouse = null;
                    OnLeave();
                }
            }
        }

Share this post


Link to post
Share on other sites

Is Mouse a struct or a class? If it's a class, then all the changes you make to ActiveMouse are actually changing the values in activeMouse that gets passed on to the Update methods for all the rest of the buttons. That might explain why subsequent buttons think they've been clicked.

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!