Advertisement
DanTonyBrown

C# Static Issue

Mar 31st, 2012
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.47 KB | None | 0 0
  1. public class MenuButton : SpriteObject
  2.     {
  3.         //Menu buttons have a windows phone theme background, which is iteself a SpriteObject
  4.         private static SpriteObject background;
  5.  
  6.         //Different Icons, static for loading
  7.         private static Texture2D playButtonIcon;
  8.         private static Texture2D highscoreButtonIcon;
  9.         private static Texture2D settingButtonIcon;
  10.         private static Texture2D aboutButtonIcon;
  11.         private static Texture2D reviewButtonIcon;
  12.         private static Texture2D brownsoftAppsIcon;
  13.         private static Texture2D websiteIcon;
  14.  
  15.         //The tap countdown stops users from "pressing" a button constantly by holding their finger down in one place
  16.         public static int TapCountdown = 0;
  17.  
  18.         //Constructor which forces size to be chosen and a button type
  19.         public MenuButton(int requestedX, int requestedY, int requestedWidth, int requestedHeight, MenuButtonType requestedMenuButtonType)
  20.             : base(requestedX, requestedY, requestedWidth, requestedHeight)
  21.         {
  22.             background = new SpriteObject(requestedX, requestedY, requestedWidth, requestedHeight);
  23.             switch (requestedMenuButtonType)
  24.             {
  25.                 case MenuButtonType.play:
  26.                     spriteTexture = playButtonIcon;
  27.                     break;
  28.  
  29.                 case MenuButtonType.highscore:
  30.                     spriteTexture = highscoreButtonIcon;
  31.                     break;
  32.  
  33.                 case MenuButtonType.settings:
  34.                     spriteTexture = settingButtonIcon;
  35.                     break;
  36.  
  37.                 case MenuButtonType.about:
  38.                     spriteTexture = aboutButtonIcon;
  39.                     break;
  40.  
  41.                 case MenuButtonType.review:
  42.                     spriteTexture = reviewButtonIcon;
  43.                     break;
  44.  
  45.                 case MenuButtonType.brownsoftApps:
  46.                     spriteTexture = brownsoftAppsIcon;
  47.                     break;
  48.  
  49.                 case MenuButtonType.website:
  50.                     spriteTexture = websiteIcon;
  51.                     break;
  52.             }
  53.         }
  54.  
  55.         //Method used to load all the private texture icons and transparent background
  56.         public static void LoadContent(ContentManager Content)
  57.         {
  58.             //Windows Phone Theme Background
  59.             background.spriteTexture = Content.Load<Texture2D>("Menu Items/Menu Buttons/WhiteBlank");
  60.  
  61.             //Main Menu Icons
  62.             playButtonIcon = Content.Load<Texture2D>("Menu Items/Menu Buttons/PlayButtonIcon");
  63.             highscoreButtonIcon = Content.Load<Texture2D>("Menu Items/Menu Buttons/HighscoreButtonIcon");
  64.             settingButtonIcon = Content.Load<Texture2D>("Menu Items/Menu Buttons/SettingsButtonIcon");
  65.             aboutButtonIcon = Content.Load<Texture2D>("Menu Items/Menu Buttons/AboutButtonIcon");
  66.  
  67.             //About Menu Icons
  68.             brownsoftAppsIcon = Content.Load<Texture2D>("Menu Items/Menu Buttons/BrownsoftAppsButtonIcon");
  69.             reviewButtonIcon = Content.Load<Texture2D>("Menu Items/Menu Buttons/ReviewButtonIcon");
  70.             websiteIcon = Content.Load<Texture2D>("Menu Items/Menu Buttons/WebsiteButtonIcon");
  71.         }
  72.  
  73.         //Method to detect if the button is currently being tapped
  74.         public bool IsTapped(TouchCollection tc)
  75.         {
  76.             if (tc.Count == 0)
  77.             {
  78.                 //If there are no fingers on the screen the button cannot be being touched, return false
  79.                 return false;
  80.             }
  81.             else
  82.             {
  83.                 //There are fingers on the screen, check if any are touching the button
  84.  
  85.                 //
  86.                 Rectangle fingerPosition;
  87.                 for (int i = 0; i < tc.Count; i++)
  88.                 {
  89.                     fingerPosition = new Rectangle((int)tc[i].Position.X, (int)tc[i].Position.Y, 1, 1);
  90.                    
  91.                     if(fingerPosition.Intersects(this.spriteRectagle) && TapCountdown == 0)
  92.                     {
  93.                         TapCountdown = 5;
  94.                         return true;
  95.                     }
  96.                 }
  97.                 return false;
  98.             }
  99.         }
  100.  
  101.         //Override the Draw method here because we need to draw both the background and the foreground Icon
  102.         public override void Draw(SpriteBatch sb)
  103.         {
  104.             background.Draw(sb, WindowsPhoneTheme.getTileColour());
  105.             base.Draw(sb);
  106.         }
  107.     }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement