Old 05-30-2009, 01:27 PM   #1 (permalink)
Jr. Member
 
xCyberFang's Avatar
 
Join Date: Apr 2008
Posts: 449
xCyberFang will become famous soon enough
Send a message via AIM to xCyberFang Send a message via MSN to xCyberFang Send a message via Yahoo to xCyberFang
Default Extend GameStateManagement: added scrolling menu!

Easily add some scrolling to your GameStateManagement, all you need to do is replace the MenuScreen.cs in the "Screens" folder with this code (I will soon create a walkthrough tutorial on what all is happening and whatnot later, for now i just wanted to share this feature):

MenuScreen.cs


Add these to the top in the fields section
Code:
        #region Fields

        List<MenuEntry> menuEntries = new List<MenuEntry>();
        int selectedEntry = 0;
        int varY = 0;
        int varY2 = 40;
        int lineheight = 0;
        int topnumber = 0;
        int bottomnumber = 0;
        string menuTitle;

        #endregion

Replace the handle input part with this, to show what to do once a user presses something.

Code:
        #region Handle Input
        /// <summary>
        /// Responds to user input, changing the selected entry and accepting
        /// or cancelling the menu.
        /// </summary>
        public override void HandleInput(InputState input)
        {

            // Move to the previous menu entry?
            if (input.MenuUp)
            {
                selectedEntry--;
                if (selectedEntry < 0)
                {
                    selectedEntry = menuEntries.Count - 1;
                    varY2 -= (lineheight * (selectedEntry - 8));
                }
                if (selectedEntry > 8)
                    varY2 += lineheight;


            }

            // Move to the next menu entry?
            if (input.MenuDown)
            {
                selectedEntry++;

                if (selectedEntry >= menuEntries.Count)
                    selectedEntry = 0;

                if (selectedEntry > 8)
                    varY2 -= lineheight;
            }

            // Accept or cancel the menu?
            if (input.MenuSelect)
            {
                OnSelectEntry(selectedEntry);
            }

            if (selectedEntry < 10)
            {
                varY2 = 40;
                bottomnumber = 13;
            }
            topnumber = selectedEntry - 10;
            if (selectedEntry > 8)
            bottomnumber = selectedEntry + 4;
        }

replace the draw part with this:
Code:
        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            SpriteFont font = ScreenManager.Font;

            Vector2 position = new Vector2(10, varY2);

            // Make the menu slide into place during transitions, using a
            // power curve to make things look more interesting (this makes
            // the movement slow down as it nears the end).
            float transitionOffset = (float)Math.Pow(TransitionPosition, 2);

            if (ScreenState == ScreenState.TransitionOn)
                position.X -= transitionOffset * 256;
            else
                position.X += transitionOffset * 512;

            spriteBatch.Begin();

            // Draw each menu entry in turn.
            for (int i = 0; i < menuEntries.Count; i++)
            {
                MenuEntry menuEntry = menuEntries[i];

                bool isSelected = IsActive && (i == selectedEntry);

                varY = (i * (menuEntry.GetHeight(this)));
                lineheight = (menuEntry.GetHeight(this));
                if (i > topnumber && i < bottomnumber)
                menuEntry.Draw(this, position, isSelected, gameTime, selectedEntry);

                position.Y += (menuEntry.GetHeight(this));
            }

            // Draw the menu title.
            Vector2 titlePosition = new Vector2(10, 10);
            Vector2 titleOrigin = font.MeasureString(menuTitle) / 2;
            Color titleColor = new Color(192, 192, 192, TransitionAlpha);
            float titleScale = 1.25f;

            titlePosition.Y -= transitionOffset * 100;

            spriteBatch.DrawString(font, menuTitle, titlePosition, titleColor, 0,
                                   Vector2.Zero, titleScale, SpriteEffects.None, 0);

            spriteBatch.End();
        }
        #endregion
    }


Now your done with MenuScreen.cs file

Edit the MenuEntry.cs
change the Draw Code to this.

Code:
        /// <summary>
        /// Draws the menu entry. This can be overridden to customize the appearance.
        /// </summary>
        public virtual void Draw(MenuScreen screen, Vector2 position,
                                 bool isSelected, Color hovercolor, Color menucolor, Color barcolor, GameTime gameTime, int select, Texture2D hoverbartext, int menunumber, bool userSettings, bool randomcolors)
        {


            Color color = isSelected ? Yellow : White;

            // Pulsate the size of the selected menu entry.
            double time = gameTime.TotalGameTime.TotalSeconds;

            float pulsate = (float)Math.Sin(time * 6) + 1;
            int recty = ((int)(position.Y));
            int rectymainmenu = ((int)(position.Y) + 10);

            Rectangle position2 = new Rectangle(0, recty+2, 240, 18);
            Rectangle position2mainmenu = new Rectangle(0, rectymainmenu, 240, 35);
            Rectangle position2loader = new Rectangle(0, rectymainmenu-10, 240, 40);

            float scale = 1 + pulsate * 0.05f * selectionFade;

            // Modify the alpha to fade text out during transitions.
            color = new Color(color.R, color.G, color.B, screen.TransitionAlpha);

            // Draw text, centered on the middle of each line.
            ScreenManager screenManager = screen.ScreenManager;
            SpriteBatch spriteBatch = screenManager.SpriteBatch;
            SpriteFont font = screenManager.Segoe12;

Vector2 origin = new Vector2(0, (font.LineSpacing / 2) + select);


                    spriteBatch.DrawString(font, text, position, color);



        }


i have changed it to a small tutorial on what all to change and whatnot, somebody please try this and tell me if anything goes wrong, i mostly worked off of my ZuneTools coding.
__________________


Last edited by xCyberFang; 07-14-2009 at 10:27 PM.





xCyberFang is offline   Reply With Quote

Advertisement [Remove Advertisement]
Old 06-28-2009, 03:56 PM   #2 (permalink)
Squirt
 
Join Date: May 2009
Posts: 19
Extron is on a distinguished road
Default

Very useful, especially for the people like me who are just starting to look at creating games with side scrolling. thanks, and get that tutorial up soon.




Extron is offline   Reply With Quote
Old 07-14-2009, 08:39 AM   #3 (permalink)
Zuner
 
badmoodguy88's Avatar
 
Join Date: Jun 2008
Posts: 72
badmoodguy88 is on a distinguished road
Default

I wish this had been out sooner. Although I still don't understand it enough to use it to be honest

I have one scrolling menu in the game I am working on now but it took me forever to make and I suppose I should switch it to this system now.

I am sure the game would be better with it.



badmoodguy88 is offline   Reply With Quote
Old 07-14-2009, 02:55 PM   #4 (permalink)
Experienced Member
 
Join Date: May 2008
Posts: 860
Berty is on a distinguished road
Default

There r many different ways to do this and many other things, all it takes is some time learning the basics. For those wanting to learn the basics i recomend clicking in my sig! XNABeginnersGuide

however this is very usefull if anyone is confused!
__________________
--new--zGuitarTuner2.1--new--
Now Works With All Zune Models!
Quote:
We talked about this. Bypass the word filter again and you'll be infracted.
-Seb






Berty is offline   Reply With Quote
Old 07-14-2009, 10:15 PM   #5 (permalink)
Jr. Member
 
xCyberFang's Avatar
 
Join Date: Apr 2008
Posts: 449
xCyberFang will become famous soon enough
Send a message via AIM to xCyberFang Send a message via MSN to xCyberFang Send a message via Yahoo to xCyberFang
Default

no i would not reccomend you switching the thing to this version just yet, i plan on doing a quick switchup and mark which parts to change soon, im just very busy at the moment.

this thing right now has a problem when going from the top to the way bottom, where it wont show the options if theres alot because it will be offscreen.


*EDIT*

i threw a quick pasteup of the work from my zunetools, somebody please tell me if this works.
__________________


Last edited by xCyberFang; 07-14-2009 at 10:35 PM.





xCyberFang is offline   Reply With Quote
Reply

Bookmarks

Thread Tools