简体   繁体   中英

Mouse clicks C# XNA

This has been bothering me for quite some time so here we go... I'm making a 2D game using XNA framework and part of the game needs the usage of mouse click funtions in UI. By using these textures as buttons it makes another window to pop up with more functions. Now the problem is when you create a button and use it, it will open another window with more buttons in it. Because these 2 buttons on different windows are at the same location, that one mouse click operates both buttons and makes the action of the last button instantly rather than having to click twice.

The real question is how do I use sprites as buttons rather than checking the position and checking if that possition is clicked?

Thanks in advance for any comments.

You could use a state manager in which the proper UI live.

In your case, the first state would have your first buttons, lets call it FirstGameState . When you click on them, the state machine gets you to a another state, lets call it MenuState . You can implement your state manager in a way that allows you to decide who handle events and who doesn't. The only state that handles your click events would be the one flagged to do so. The rest would only draw and update the visual content but handle no user events.

Here is an example for you:

namespace States
{
    class StateManager
    {
        List<GameState> _states = new List<GameState>();
        List<GameState> _statesCopy = new List<GameState>();

        public Game Game                { get; private set; }
        public SpriteBatch SpriteBatch  { get; private set; }

        public StateManager(Game game, SpriteBatch sb)
        {
            Game = game;
            SpriteBatch = sb;
        }

        public void AddState(GameState state)
        {
            _states.Add(state);
        }

        public void RemoveState(GameState state)
        {
            _states.Remove(state);
        }

        public void Update(GameTime gameTime)
        {
            _statesCopy.Clear();
            foreach (GameState state in _states)
                if (state.RequireUpdate)
                    _statesCopy.Add(state);

            foreach (GameState state in _statesCopy)
                if (state.RequireUpdate)
                    state.Update(gameTime);
        }

        public void Draw()
        {
            foreach (GameState state in _states)
                state.Draw();
        }
    }
}

I created a basic UI framework that has Windows with Controls on them. Windows are stacked by zOrder as are the controls on each window. There is a ScreenManager class that updates every tick that tracks the mouse. When it sees a button click it walks the window stack from top to bottom looking for the first window that contains the click coordinates. It then calls the OnClick sub of that window instance, which does the same thing for the controls on the window. Each method stops walking the control stack as soon as it finds the first control that contains the click coords.

Each unique window in my game is a subclass of that basic window class that creates child control instances and lays them out in the specific way for it's layout. Controls like buttons raise a clicked event that the window subclass handles to do the logic needed for that button.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM