Guest User

Dave XNA N00b

a guest
Mar 28th, 2012
66
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // I've included 3 of my classes in here. Gameplay Screen, Bat, and Input. I'm having difficulty getting my Gameplay Screen to recognize the inputs from my bats (paddles). I am receiving this error:
  2.  
  3. 'MyXnaGame.Bat' does not contain a definition for 'Position' and no extension method 'Position' accepting a first argument of type 'MyXnaGame.Bat' could be found (are you missing a using directive or an assembly reference?)
  4.  
  5. Should I be calling the input from my BAT class, or my Input class?
  6.  
  7.  
  8. #region File Description
  9. //-----------------------------------------------------------------------------
  10. // GameplayScreen.cs
  11. //
  12. // Microsoft XNA Community Game Platform
  13. // Copyright (C) Microsoft Corporation. All rights reserved.
  14. //-----------------------------------------------------------------------------
  15. #endregion
  16.  
  17. #region Using Statements
  18. using System;
  19. using System.Threading;
  20. using Microsoft.Xna.Framework;
  21. using Microsoft.Xna.Framework.Content;
  22. using Microsoft.Xna.Framework.Graphics;
  23. using Microsoft.Xna.Framework.Input;
  24. using Microsoft.Xna.Framework.Media;
  25. using Drawing;
  26. #endregion
  27.  
  28. namespace MyXnaGame
  29. {
  30.     /// <summary>
  31.     /// This screen implements the actual game logic. It is just a
  32.     /// placeholder to get the idea across: you'll probably want to
  33.     /// put some more interesting gameplay in here!
  34.     /// </summary>
  35.     class GameplayScreen : GameScreen
  36.     {
  37.         #region Fields
  38. //        Sprite mySprite;
  39.        
  40.         private Ball myBall;
  41.         private Bat rightBat;
  42.         private Bat leftBat;
  43.  
  44.         Input myInputClass;
  45.         private Input batInput;
  46.  
  47.         HudContainer hudContainer = new HudContainer();
  48.        
  49.         SafeArea safeArea;
  50.         Vector2 safeTopLeft = Vector2.Zero; // (2.0f * (safeArea.dx), 1.5f * (safeArea.dy));
  51.         ContentManager content;
  52.         SpriteFont gameFont;
  53.         SpriteFont hudFont;
  54.         private Texture2D backgroundTexture;
  55.         public Song PlayingSong { get; private set; }
  56.  
  57.         Vector2 enemyPosition = new Vector2(100, 100);
  58.  
  59.         Random random = new Random();
  60.  
  61.         float pauseAlpha;
  62.  
  63.         #endregion
  64.  
  65.         #region Initialization
  66.  
  67.  
  68.         /// <summary>
  69.         /// Constructor.
  70.         /// </summary>
  71.         public GameplayScreen()
  72.         {
  73.             //
  74.             safeArea = new SafeArea();
  75. //            mySprite = new Sprite();
  76.             TransitionOnTime = TimeSpan.FromSeconds(1.5);
  77.             TransitionOffTime = TimeSpan.FromSeconds(0.5);
  78.         }
  79.  
  80.  
  81.  
  82.         public override void LoadContent()
  83.         {
  84.             if (content == null)
  85.                 content = new ContentManager(ScreenManager.Game.Services, "Content");
  86.             DrawingHelper.Initialize(ScreenManager.GraphicsDevice);
  87.             myInputClass = new Input();
  88.             //
  89.             PlayingSong = content.Load<Song>(@"sfx/boomer");
  90.             MediaPlayer.Play(PlayingSong);
  91.             //
  92.             backgroundTexture = content.Load<Texture2D>(@"gfx/mm7Background");
  93.             //
  94.             gameFont = content.Load<SpriteFont>("gamefont");
  95.             hudFont = content.Load<SpriteFont>("menufont");
  96.             //
  97.             safeArea.LoadGraphicsContent(ScreenManager.GraphicsDevice);
  98.             //
  99.             hudContainer.LoadGraphicsContent(ScreenManager.GraphicsDevice, hudFont, safeArea);
  100.             var hudScore = new HudTextComponent("SCORE: 100", HudComponent.PresetPosition.TopLeft);
  101.             var hudHealth = new HudTextComponent("HEALTH: 300", HudComponent.PresetPosition.TopRight);
  102.             var hudLives = new HudTextComponent("LIVES: 3", HudComponent.PresetPosition.MiddleLeft);
  103.             var hudAmmo = new HudTextComponent("Ammo: 447", HudComponent.PresetPosition.MiddleRight);
  104.             var hudName = new HudTextComponent("Angry Zombie...", HudComponent.PresetPosition.BottomLeft);
  105.             var hudInfo = new HudTextComponent("...Ninja Cats!", HudComponent.PresetPosition.BottomRight);
  106.  
  107.            
  108. /*          I've commented out the hud variables -DV
  109.             hudContainer.Add(hudScore);
  110.             hudContainer.Add(hudHealth);
  111.             hudContainer.Add(hudLives);
  112.             hudContainer.Add(hudAmmo); ;
  113.             hudContainer.Add(hudName);
  114.             hudContainer.Add(hudInfo);
  115.  */          //
  116.            
  117. //            mySprite.LoadContent(content);
  118.  
  119.              myBall = new Ball(content, new Vector2 (ScreenManager.GraphicsDevice.Viewport.Width,
  120.                 ScreenManager.GraphicsDevice.Viewport.Height));        
  121.             rightBat = new Bat(content, new Vector2 (ScreenManager.GraphicsDevice.Viewport.Width,
  122.                 ScreenManager.GraphicsDevice.Viewport.Height), true);
  123.             leftBat = new Bat(content, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width,
  124.                 ScreenManager.GraphicsDevice.Viewport.Height), false);
  125.          
  126.            
  127.  
  128.             // A real game would probably have more content than this sample, so
  129.             // it would take longer to load. We simulate that by delaying for a
  130.             // while, giving you a chance to admire the beautiful loading screen.
  131.             Thread.Sleep(1000);
  132.  
  133.             // once the load has finished, we use ResetElapsedTime to tell the game's
  134.             // timing mechanism that we have just finished a very long frame, and that
  135.             // it should not try to catch up.
  136.             ScreenManager.Game.ResetElapsedTime();
  137.         }
  138.  
  139.  
  140.         public override void UnloadContent()
  141.         {
  142.             content.Unload();
  143.         }
  144.  
  145.  
  146.         /// <summary>
  147.         /// Updates the state of the game. This method checks the GameScreen.IsActive
  148.         /// property, so the game will stop updating when the pause menu is active,
  149.         /// or if you tab away to a different application.
  150.         /// </summary>
  151.         public override void Update(GameTime gameTime, bool otherScreenHasFocus,
  152.                                                        bool coveredByOtherScreen)
  153.         {
  154.             base.Update(gameTime, otherScreenHasFocus, false);
  155.  
  156.             // Gradually fade in or out depending on whether we are covered by the pause screen.
  157.             if (coveredByOtherScreen)
  158.                 pauseAlpha = Math.Min(pauseAlpha + 1f / 32, 1);
  159.             else
  160.                 pauseAlpha = Math.Max(pauseAlpha - 1f / 32, 0);
  161.  
  162.             if (IsActive)
  163.             {
  164.                 // Apply some random jitter to make the enemy move around.
  165.                 const float randomization = 10;
  166.  
  167.                 enemyPosition.X += (float)(random.NextDouble() - 0.5) * randomization;
  168.                 enemyPosition.Y += (float)(random.NextDouble() - 0.5) * randomization;
  169.  
  170.                 // Apply a stabilizing force to stop the enemy moving off the screen.
  171.                 Vector2 targetPosition = new Vector2(
  172.                     ScreenManager.GraphicsDevice.Viewport.Width / 2 - gameFont.MeasureString("XNA Basic Starter Kit!").X / 2,
  173.                     200);
  174.  
  175.                 enemyPosition = Vector2.Lerp(enemyPosition, targetPosition, 0.05f);
  176.  
  177. //                mySprite.Update(gameTime);
  178.                 myBall.UpdatePosition();
  179.                
  180.       //          rightBat.Update();
  181.                 myInputClass.Update();
  182.  
  183.             }
  184.         }
  185.  
  186.  
  187.         /// <summary>
  188.         /// Lets the game respond to player input. Unlike the Update method,
  189.         /// this will only be called when the gameplay screen is active.
  190.         /// </summary>
  191.         public override void HandleInput(InputState input) //, GameTime gameTime)
  192.         {
  193.             if (input == null)
  194.                 throw new ArgumentNullException("input");
  195.  
  196.             // Look up inputs for the active player profile.
  197.             int playerIndex = (int)ControllingPlayer.Value;
  198.  
  199.             KeyboardState keyboardState = input.CurrentKeyboardStates[playerIndex];
  200.             GamePadState gamePadState = input.CurrentGamePadStates[playerIndex];
  201.  
  202.             // The game pauses either if the user presses the pause button, or if
  203.             // they unplug the active gamepad. This requires us to keep track of
  204.             // whether a gamepad was ever plugged in, because we don't want to pause
  205.             // on PC if they are playing with a keyboard and have no gamepad at all!
  206.             bool gamePadDisconnected = !gamePadState.IsConnected &&
  207.                                        input.GamePadWasConnected[playerIndex];
  208.  
  209.             if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
  210.             {
  211.                 ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
  212.             }
  213.          if (input.IsPauseGame(ControllingPlayer) || gamePadDisconnected)
  214. {
  215.     ScreenManager.AddScreen(new PauseMenuScreen(), ControllingPlayer);
  216. }
  217. else
  218. {
  219.     //If/ElseIf is used so we don't move up AND down (by holding both buttons). It's either up OR down, not both
  220.     if (myInputClass.LeftUp)
  221.     {
  222.         leftBat.Position.Y--; //Decrease the Y position by 1 every frame. (Move towards top of screen)
  223.     }
  224.     else if (myInputClass.LeftDown)
  225.     {
  226.         leftBat.Position.Y++; //Increase the Y position by 1 every frame. (Move towards bottom of screen)
  227.     }
  228.  
  229.     //If/ElseIf is used so we don't move up AND down (by holding both buttons). It's either up OR down, not both
  230.     if (myInputClass.RightUp)
  231.     {
  232.         rightBat.Position.Y--; //Decrease the Y position by 1 every frame. (Move towards top of screen)
  233.     }
  234.     else if (myInputClass.RightDown)
  235.     {
  236.         rightBat.Position.Y++; //Increase the Y position by 1 every frame. (Move towards bottom of screen)
  237.     }
  238. }
  239.  
  240.  
  241.         public override void Draw(GameTime gameTime)
  242.         {
  243.  
  244.  
  245.             // Our player and enemy are both actually just text strings.
  246.             SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
  247.  
  248.             spriteBatch.Begin();
  249.  
  250.             spriteBatch.Draw(backgroundTexture, new Rectangle(0, 0, 800, 600), Color.White);
  251.  
  252.             // draw hud
  253.             hudContainer.Draw(spriteBatch);
  254.  
  255.             spriteBatch.DrawString(gameFont, "XNA Basic Starter Kit!",
  256.                                    enemyPosition, Color.Black);
  257.  
  258. #if DEBUG     // I've temporarily commented out the safeArea debug -DV  
  259. //            safeArea.Draw(spriteBatch);
  260. #endif
  261.  
  262.             // debug info
  263.             var safeAreaInfo = "dx=" + safeArea.dx + ",dy=" + safeArea.dy
  264.                 + ",w=" + safeArea.width + ",h=" + safeArea.height;
  265.                 //+ ", H = " + playerHorizDirection
  266.                 //+ ", V = " + playerVertDirection;
  267.             spriteBatch.DrawString(gameFont, safeAreaInfo, safeTopLeft, Color.Green);
  268.  
  269.             //
  270. //            mySprite.Draw(spriteBatch);
  271.             //
  272.             myBall.Draw(spriteBatch);
  273.            
  274.             rightBat.Draw(spriteBatch);
  275.             leftBat.Draw(spriteBatch);
  276.  
  277.            
  278.             spriteBatch.End();
  279.  
  280.            // If the game is transitioning on or off, fade it out to black.
  281.             if (TransitionPosition > 0 || pauseAlpha > 0)
  282.             {
  283.                 float alpha = MathHelper.Lerp(1f - TransitionAlpha, 1f, pauseAlpha / 2);
  284.  
  285.                 ScreenManager.FadeBackBufferToBlack(alpha);
  286.             }
  287.         }
  288.  
  289.  
  290.         #endregion
  291.     }
  292. }
  293.  
  294. --------------------------------------------------------------------------------------
  295.  
  296.  
  297.  
  298. namespace MyXnaGame
  299. {
  300.     using System;
  301.     using System.Collections.Generic;
  302.     using System.Linq;
  303.     using System.Text;
  304.     using Microsoft.Xna.Framework;
  305.     using Microsoft.Xna.Framework.Content;
  306.     using Microsoft.Xna.Framework.Graphics;
  307.  
  308.     class Bat
  309.     {
  310.         private Vector2 position;
  311.         private int moveSpeed;
  312.         private Rectangle size;
  313.         private int points;
  314.         private int yHeight;
  315.         private Texture2D texture;
  316.  
  317.         public Bat(ContentManager content, Vector2 screenSize, bool side)
  318.         {
  319.             moveSpeed = 6;
  320.             points = 0;
  321.             texture = content.Load<Texture2D>(@"gfx/bat");
  322.             size = new Rectangle(0, 0, texture.Width, texture.Height);
  323.             if (side) position = new Vector2(30, screenSize.Y / 2 - size.Height / 2);
  324.             else position = new Vector2(screenSize.X - 30 - size.Width, screenSize.Y / 2 - size.Height / 2);
  325.             yHeight = (int)screenSize.Y;
  326.         }
  327.  
  328.         public void IncrementPoints()
  329.         {
  330.             points++;
  331.         }
  332.  
  333.         public int GetPoints()
  334.         {
  335.             return points;
  336.         }
  337.  
  338.         public Rectangle GetSize()
  339.         {
  340.             return size;
  341.         }
  342.  
  343.         public Vector2 GetPosition()
  344.         {
  345.             return position;
  346.         }
  347.  
  348.         public virtual void UpdatePosition(Ball ball)
  349.         {
  350.             size.X = (int)position.X;
  351.             size.Y = (int)position.Y;
  352.         }
  353.  
  354.  
  355.         private void SetPosition(Vector2 position)
  356.         {
  357.             if (position.Y < 0)
  358.             {
  359.                 position.Y = 0;
  360.             }
  361.             if (position.Y > yHeight - size.Height)
  362.             {
  363.                 position.Y = yHeight - size.Height;
  364.             }
  365.             this.position = position;
  366.         }
  367.  
  368.  
  369.         public void MoveUp()
  370.         {
  371.             SetPosition(position + new Vector2(0, -moveSpeed));
  372.         }
  373.  
  374.         public void MoveDown()
  375.         {
  376.             SetPosition(position + new Vector2(0, moveSpeed));
  377.         }
  378.  
  379.         public void Draw(SpriteBatch batch)
  380.         {
  381.             batch.Draw(texture, position, Color.White);
  382.         }
  383.  
  384.     }
  385. }
  386.  
  387.  
  388. -------------------------------------------------------------------------------
  389.  
  390.  
  391. using System;
  392. using System.Collections.Generic;
  393. using System.Linq;
  394. using System.Text;
  395. using Microsoft.Xna.Framework.Input;  
  396.  
  397. namespace MyXnaGame
  398. {
  399.     class Input
  400.     {
  401.         private KeyboardState keyboardState;
  402.         private KeyboardState lastState;
  403.  
  404.         public Input()
  405.         {
  406.             keyboardState = Keyboard.GetState();
  407.             lastState = keyboardState;
  408.         }
  409.  
  410.         public void Update()
  411.         {
  412.             lastState = keyboardState;
  413.             keyboardState = Keyboard.GetState();
  414.         }
  415.  
  416.         public bool RightUp
  417.         {
  418.             get
  419.             {
  420.                 return keyboardState.IsKeyDown(Keys.Up);
  421.             }
  422.         }
  423.  
  424.         public bool RightDown
  425.         {
  426.             get
  427.             {
  428.                 return keyboardState.IsKeyDown(Keys.Down);
  429.             }
  430.         }
  431.  
  432.         public bool LeftUp
  433.         {
  434.             get
  435.             {
  436.                 return keyboardState.IsKeyDown(Keys.W);
  437.             }
  438.         }
  439.  
  440.         public bool LeftDown
  441.         {
  442.             get
  443.             {
  444.                 return keyboardState.IsKeyDown(Keys.S);
  445.             }
  446.         }
  447.     }
  448. }
RAW Paste Data