XNA Creators Club Online
Page 1 of 1 (2 items)
Sort Posts: Previous Next

Trouble Displaying Image

Last post 10/9/2008 1:59 PM by Zotos. 1 replies.
  • 10/8/2008 9:29 PM
    • (42)
    • premium membership
    • Posts 2

    Trouble Displaying Image

    I'm writing  basic 2D program. Problem is I put some buttons on a page and only one will display. If I remove the one that does display the other will show.

     I had some mouse over methods that changed the image color but i have commented them out thinking that was the issue but I still get the same end result.

    Here is my code.

    MenuScreen.cs

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using Microsoft.Xna.Framework; 
    using Microsoft.Xna.Framework.Graphics; 
    using System.Collections; 
     
    namespace BugColony 
        public enum Buttons 
        { 
            New, 
            Exit 
        } 
        class MenuScreen : BaseScreen 
        { 
            public ArrayList MyButtons = new ArrayList(); 
            ExitButton exit = new ExitButton(new Vector2((800 / 2) - (Images.ExitButton.Width / 2), 500)); 
            NewGameButton newgame = new NewGameButton(new Vector2((800 / 2) - (Images.ExitButton.Width / 2), 370)); 
     
            public ArrayList Bugs = new ArrayList(); 
            public void CreateBugs() 
            { 
                for (int i = 0; i < 10; i++) 
                { 
                    MenuBug bug = new MenuBug(); 
                    Bugs.Add(bug); 
                } 
            } 
            public void CreateButtons() 
            { 
                /*ExitButton exit = new ExitButton(new Vector2((800 / 2) - (Images.ExitButton.Width / 2), 500));
                MyButtons.Add(exit);
                NewGameButton newgame = new NewGameButton(new Vector2((800 / 2) - (Images.ExitButton.Width / 2), 370));
                MyButtons.Add(newgame);*/ 
            } 
            public MenuScreen() : base() 
            { 
                CreateBugs(); 
     
                //CreateButtons(); 
            } 
     
            public override void Draw(Game1 game) 
            { 
                //draw background 
                game.spriteBatch.Draw(Images.MenuBG, new Vector2(0, 0), Color.White); 
     
                //draw bugs 
                foreach (MenuBug bug in Bugs) 
                { 
                    bug.Draw(game); 
                } 
                //draw buttons 
                /*string rects = "";
                for (int i = 0; i < MyButtons.Count;i++ )
                {
                    BaseButton button = MyButtons[i] as BaseButton;
                    button.ShowImage(game);
                    rects = "   Rect: " + button.Area.ToString();
                }*/ 
                exit.ShowImage(game); 
                //newgame.ShowImage(game); 
     
                //Debug *** 
                game.Window.Title = exit.Area.ToString(); 
     
                base.Draw(game); 
            } 
            public override void OnClick(Game1 game) 
            { 
                base.OnClick(game); 
            } 
        } 
     

     

    BaseButton.cs

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
     
    using System.Windows.Forms; 
     
    using Microsoft.Xna; 
    using Microsoft.Xna.Framework; 
    using Microsoft.Xna.Framework.Graphics; 
     
    namespace BugColony 
        /// <summary> 
        /// Anything that can be manipulated with the mouse 
        /// </summary> 
        public class BaseButton 
        { 
            #region Properties 
            public bool MouseHover; 
            public Rectangle Area = GetBounds(); 
     
            private static Rectangle GetBounds() 
            { 
                Rectangle r = new Rectangle(); 
                r.X = (int)Position.X; 
                r.Y = (int)Position.Y; 
     
                if (PrimaryImage != null
                { 
                    r.Width = PrimaryImage.Width; 
                    r.Height = PrimaryImage.Height; 
                } 
                return r; 
            } 
            /// <summary> 
            /// Where the button is located 
            /// </summary> 
            public static Vector2 Position; 
            /// <summary> 
            /// Base image for normal display 
            /// </summary> 
            public static Texture2D PrimaryImage; 
            /// <summary> 
            /// Image for mouse over. 
            /// </summary> 
            public static Texture2D SecondaryImage; 
            /// <summary> 
            /// Images for when a click image is needed.  
            /// Sounds funny yea i made that *** up. 
            /// </summary> 
            public static Texture2D ThirdaryImage; 
            #endregion 
            #region Assemblies 
            /// <summary> 
            /// Button has no images WTF? 
            /// </summary> 
            public BaseButton() 
            { 
            } 
            /// <summary> 
            /// Button with only one image (no mouse over function) 
            /// </summary> 
            /// <param name="tex1">Base Image for button</param> 
            public BaseButton(Texture2D tex1) 
            { 
                PrimaryImage = tex1; 
            } 
            /// <summary> 
            /// Mouse with 2 images supporting mouse over feature. 
            /// </summary> 
            /// <param name="tex1">Base image for button</param> 
            /// <param name="tex2">Mouse over image</param> 
            public BaseButton(Texture2D tex1, Texture2D tex2) 
            { 
                PrimaryImage = tex1; 
                SecondaryImage = tex2; 
            } 
            /// <summary> 
            /// Mouse supporting mouse over and click imaging. 
            /// </summary> 
            /// <param name="tex1">Base image for button</param> 
            /// <param name="tex2">Mouse ove image</param> 
            /// <param name="tex3">Click image.</param> 
            public BaseButton(Texture2D tex1, Texture2D tex2, Texture2D tex3) 
            { 
                PrimaryImage = tex1; 
                SecondaryImage = tex2; 
                ThirdaryImage = tex3; 
            } 
            public bool MouseOver() 
            { 
                if (MyMouse.m_Position.X >= Position.X && MyMouse.m_Position.X <= Position.X + Area.Width && MyMouse.m_Position.Y >= Position.Y && MyMouse.m_Position.Y <= Position.Y + Area.Height) 
                    return true
     
                return false
            } 
            #endregion 
     
            public virtual void ShowImage(Game1 game) 
            { 
                Area = GetBounds(); 
                bool mouse = MouseOver(); 
                //if (PrimaryImage != null) 
                    //game.spriteBatch.Draw(PrimaryImage, Position, mouse ? Color.Red : Color.White); 
     
                game.spriteBatch.Draw(PrimaryImage, Position, Color.White); 
            } 
     
            public virtual void OnClick(Game1 game) 
            { 
            } 
        } 
     

     

    ExitButton.cs

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using Microsoft.Xna.Framework; 
     
    namespace BugColony 
        class ExitButton : BaseButton 
        { 
            public ExitButton(Vector2 position) : base() 
            { 
                Position = new Vector2(position.X,position.Y); 
                PrimaryImage = Images.ExitButton; 
            } 
     
            public override void OnClick(Game1 game) 
            { 
                base.OnClick(game); 
            } 
        } 

     

    NewGameButton.cs

    using System; 
    using System.Collections.Generic; 
    using System.Text; 
    using Microsoft.Xna.Framework; 
     
    namespace BugColony 
        class NewGameButton : BaseButton 
        { 
            public NewGameButton(Vector2 position) : base() 
            { 
                Position = new Vector2( position.X,position.Y); 
                PrimaryImage = Images.NewGameButton; 
            } 
     
            public override void OnClick(Game1 game) 
            { 
                base.OnClick(game); 
            } 
        } 

  • 10/9/2008 1:59 PM In reply to
    • (42)
    • premium membership
    • Posts 2

    Re: Trouble Displaying Image

    Problem solved. For some reason I set the Primary Image to static and that was causing the issue.
Page 1 of 1 (2 items) Previous Next