2015-05-04 69 views
-3

我的繼承人整個代碼的操作數,也許我的變量是正確的,我不是肯定:操作「>」不能應用於類型「詮釋」和「法團」

using System; 
using System.Collections.Generic; 
using System.Linq; 
using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Audio; 
using Microsoft.Xna.Framework.Content; 
using Microsoft.Xna.Framework.GamerServices; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 
using Microsoft.Xna.Framework.Media; 
using System.IO; 
using System.Text; 

namespace PickUpTheCrewGame 
{ 
    /// <summary> 
    /// This is the main type for your game 
    /// </summary> 
    public class PickUpTheCrewGame : Microsoft.Xna.Framework.Game 
    { 
     public enum State 
     { 
      Menu, 
      Playing, 
      Gameover, 
     } 

     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     SpriteFont messageFont; 
     SpriteFont playerScoreFont; 
     Texture2D backgroundTexture; 
     Rectangle backgroundRectangle; 
     Texture2D menuImageTexture; 
     Rectangle menuImageRectange; 
     Texture2D gameoverImageTexture; 
     Rectangle gameoverImageRectangle; 
     Sprite BlueBall; 
     Sprite GreenBall; 
     Sprite OrangeBall; 
     Sprite PinkBall; 
     Sprite RedBall; 
     Sprite c; 
     Sprite YellowBall; 
     public Texture2D menuImage; 
     public Texture2D gameoverImage; 

     //---player scoresyer 
     int playerScore = 0; 
     int highScore = 0; 

     //Lists 
     List<sharks> sharks = new List<sharks>(); 
     List<Sprite> crew = new List<Sprite>(); 
     List<int> highscoreList = new List<int>(); 

     //highScoreList.Add(score); 


     //Set First State 
     State gameState = State.Menu; 

     HUD hud = new HUD(); 

     public PickUpTheCrewGame() 
     { 
      graphics = new GraphicsDeviceManager(this); 

      Content.RootDirectory = "Content"; 

      //sreen size 
      graphics.PreferredBackBufferWidth = 1280; 
      graphics.PreferredBackBufferHeight = 720; 
      menuImage = null; 
      gameoverImage = null; 


     } 

     /// <summary> 
     /// Allows the game to perform any initialization it needs to before starting to run. 
     /// This is where it can query for any required services and load any non-graphic 
     /// related content. Calling base.Initialize will enumerate through any components 
     /// and initialize them as well. 
     /// </summary> 
     protected override void Initialize() 
     { 
      // TODO: Add your initialization logic here 
      //enable the mousepointer 
      //IsMouseVisible = true; 
      base.Initialize(); 
     } 

     public static bool _highScore (int playerScore) 
     { 
      if(playerScore >_highScore) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

     public void Save(string filename) 
     { 
      System.IO.TextWriter textOut = null; 
      try 
      { 
       textOut = new System.IO.StreamWriter(filename); 
       Save(textOut); 
      } 
      catch (Exception e) 
      { 
       throw e; 
      } 
      finally 
      { 
       if (textOut != null) textOut.Close(); 
      } 
     } 

     private void Save(TextWriter textOut) 
     { 
      try 
      { 
       foreach (Sprite crew1 in crew) 
       { 
        textOut.WriteLine(crew1.location.X); 
        textOut.WriteLine(crew1.location.Y); 
       } 

       foreach (sharks enemySprite in sharks) 
       { 
        textOut.WriteLine("Shark"); 
        textOut.WriteLine(enemySprite.location.X); 
        textOut.WriteLine(enemySprite.location.Y); 
       } 
      } 
      catch 
      { 

      } 
     } 

     public void Load(string filename) 
     { 
      System.IO.TextReader textIn = null; 
      //try 
      //{ 
      textIn = new System.IO.StreamReader(filename); 
      Load(textIn); 
      //} 
      //catch (Exception e) 
      //{ 
      // throw e; 
      //} 
      //finally 
      //{ 
      if (textIn != null) textIn.Close(); 
      //} 
     } 

     private void Load(TextReader textIn) 
     { 
      foreach (Sprite crew1 in crew) 
      { 
       crew1.location.X = int.Parse(textIn.ReadLine()); 
       crew1.location.Y = int.Parse(textIn.ReadLine()); 
      } 
      foreach (sharks enemySprite in sharks) 
      { 
       enemySprite.location.X = int.Parse(textIn.ReadLine()); 
       enemySprite.location.Y = int.Parse(textIn.ReadLine()); 
      } 
      throw new NotImplementedException(); 
     } 

     /// <summary> 
     /// LoadContent will be called once per game and is the place to load 
     /// all of your content. 
     /// </summary> 
     protected override void LoadContent() 
     { 
      // Create a new SpriteBatch, which can be used to draw textures. 
      spriteBatch = new SpriteBatch(GraphicsDevice); 

      hud.LoadContent(Content); 
      //-----LOAD THE MENU BACKGROUND----- 
      menuImageTexture = Content.Load<Texture2D>("menuImage"); 
      menuImageRectange = new Rectangle(0, 0, 
       Window.ClientBounds.Width, 
       Window.ClientBounds.Height); 

      //------LOAD INGAME BACKGROUND----- 
      backgroundTexture = Content.Load<Texture2D>("Background"); 
      backgroundRectangle = new Rectangle(
      0, 0, // top left hand corner 
      Window.ClientBounds.Width, 
      Window.ClientBounds.Height); // size of screen display 

      //GAMEOVER IMAGE BACKGROUND 
      gameoverImageTexture = Content.Load<Texture2D>("gameoverImage"); 
      gameoverImageRectangle = new Rectangle(
       0, 0, 
       Window.ClientBounds.Width, 
       Window.ClientBounds.Height); 


      //-------Captains crew------- 

      c = new Sprite(new Vector2(0, 0), new Vector2(0, 0), 
        Content.Load<Texture2D>("WhiteBall"), Color.White); 

      BlueBall = new Sprite(new Vector2(640, 450), 
       Content.Load<Texture2D>("BlueBall")); 
      crew.Add(BlueBall); 

      GreenBall = new Sprite(new Vector2(250, 600), 
       Content.Load<Texture2D>("GreenBall")); 
      crew.Add(GreenBall); 

      OrangeBall = new Sprite(new Vector2(115, 400), 
       Content.Load<Texture2D>("OrangeBall")); 
      crew.Add(OrangeBall); 

      RedBall = new Sprite(new Vector2(500, 600), 
       Content.Load<Texture2D>("RedBall")); 
      crew.Add(RedBall); 

      YellowBall = new Sprite(new Vector2(800, 400), 
       Content.Load<Texture2D>("YellowBall")); 
      crew.Add(YellowBall); 

      PinkBall = new Sprite(new Vector2(25, 175), 
       Content.Load<Texture2D>("PinkBall")); 
      crew.Add(PinkBall); 

      //--------Sharks------ 
      sharks s = new sharks(new Vector2(1000, 200), 
       Content.Load<Texture2D>("BlackBall")); 
      sharks.Add(s); 
      s = new sharks(new Vector2(900, 200), 
       Content.Load<Texture2D>("BlackBall")); 
      sharks.Add(s); 
      s = new sharks(new Vector2(800, 200), 
       Content.Load<Texture2D>("BlackBall")); 
      sharks.Add(s); 

      messageFont = Content.Load<SpriteFont>("messageFont"); 

      // TODO: use this.Content to load your game content here 
     } 

     /// <summary> 
     /// UnloadContent will be called once per game and is the place to unload 
     /// all content. 
     /// </summary> 
     protected override void UnloadContent() 
     { 
      // TODO: Unload any non ContentManager content here 
     } 

     /// <summary> 
     /// Allows the game to run logic such as updating the world, 
     /// checking for collisions, gathering input, and playing audio. 
     /// </summary> 
     /// <param name="gameTime">Provides a snapshot of timing values.</param> 
     protected override void Update(GameTime gameTime) 
     { 
      //----------This gets the time value--------- 
      float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds; 

      //--------------keyboard input--------------- 
      //Exit 
      if (Keyboard.GetState().IsKeyDown(Keys.Escape)) 
       this.Exit(); 
      //Save 
      if (Keyboard.GetState().IsKeyDown(Keys.S)) 
       Save("test.txt"); 
      //Load 
      if (Keyboard.GetState().IsKeyDown(Keys.L)) 
       Load("test.txt"); 

      //Directional Movement 
      if (Keyboard.GetState().IsKeyDown(Keys.Left)) 
       c.velocity.X = -350; 

      if (Keyboard.GetState().IsKeyDown(Keys.Right)) 
       c.velocity.X = 350; 

      if (Keyboard.GetState().IsKeyDown(Keys.Down)) 
       c.velocity.Y = 350; 

      if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
       c.velocity.Y = -350; 


      //UPDATING PLAYING STATE 
      switch (gameState) 
      { 
       case State.Playing: 
        c.Update(elapsed); 
        foreach (Sprite cr in crew) 
        { 
         cr.Update(elapsed); 
        } 
        c.col = Color.White; 

        //----sharks intersects with whiteball---- 
        foreach (sharks s in sharks) 
        { 
         if (c.bounds.Intersects(s.bounds)) 
         { 
          gameState = State.Gameover; 
          break; 
         } 
        } 
        foreach (sharks s in sharks) 
        { 
         s.Update(elapsed, c.location); 
        } 



        //hud.Update(gameTime); 
        //----sprites intersect with whiteball---- 
        foreach (Sprite crew1 in crew) 
        { 
         if (c.bounds.Intersects(crew1.bounds)) 
         { 
          //gameState = State.Gameover; 
          playerScore += 1; 
          crew1.bounds.X = 10000; 
          crew1.bounds.Y = 10000; 
          crew1.location.Y = 10000; 
          crew1.location.X = 10000; 
          break; 
         } 
        } break; 


       //UPDATING MENU STATE 
       case State.Menu: 
        { 
         //Get keyboard state 
         KeyboardState keyState = Keyboard.GetState(); 
         if (keyState.IsKeyDown(Keys.Enter)) 
         { 
          gameState = State.Playing; 
         } 
         break; 
        } 

       //UPDATING GAMEOVER STATE 
       case State.Gameover: 
        { 
         //Get keyboard state 
         KeyboardState keyState = Keyboard.GetState(); 
         if (keyState.IsKeyDown(Keys.Back)) 
         { 
          if (playerScore > highScore) 
          { 
           highScore = playerScore; 
           SavehighScore(); 
          } 
          playerScore = 0; 
          sharks.Clear(); 
          crew.Clear(); 
          gameState = State.Menu; 
         } 
         break; 
        } 
      } 
      base.Update(gameTime); 
     } 

     /// <summary> 
     /// This is called when the game should draw itself. 
     /// </summary> 
     /// <param name="gameTime">Provides a snapshot of timing values.</param> 
     protected override void Draw(GameTime gameTime) 
     { 
      GraphicsDevice.Clear(Color.CornflowerBlue); 

      spriteBatch.Begin(); 

      switch (gameState) 
      { 
       //DRAWING PLAYING STATE 
       case State.Playing: 
        { 
         spriteBatch.Draw(backgroundTexture, backgroundRectangle, 
      Color.White); 
         c.Draw(spriteBatch); 
         //FOR EACH CREW DRAW 
         foreach (Sprite cr in crew) 
         { 
          cr.Draw(spriteBatch); 
         } 
         //FOREACH SHARK DRAW 
         foreach (sharks s in sharks) 
         { 
          s.Draw(spriteBatch); 
         } 

         hud.Draw(spriteBatch); 

         spriteBatch.DrawString(messageFont, playerScore.ToString(), 
       new Vector2(200, 0), 
       Color.White); 

         spriteBatch.DrawString(messageFont, " Player Scores - ", 
          new Vector2(0, 0), Color.White); 
         break; 


        } 
       //DRAWING MENU STATE 
       case State.Menu: 
        { 
         spriteBatch.Draw(menuImageTexture, menuImageRectange, Color.White); 
         break; 
        } 
       //DRAWING GAMEOVER STATE 
       case State.Gameover: 
        { 
         spriteBatch.Draw(gameoverImageTexture, gameoverImageRectangle, Color.White); 
         spriteBatch.DrawString(messageFont, "Your Final Score was - " + playerScore.ToString(),new Vector2 (0,0), Color.White); 
         break; 
        } 
      } 


      spriteBatch.End(); 

      base.Draw(gameTime); 
     } 
    } 
} 

這裏拳頭錯誤:

Error 1 Operator '>' cannot be applied to operands of type 'int' and 'method group'

我做的高分在XNA和已經得到了一條錯誤消息:

public static bool _highScore (int playerScore) 
     { 
      if(playerScore >_highScore) 
      { 
       return true; 
      } 
      else 
      { 
       return false; 
      } 
     } 

我也是在恩得到一個錯誤messeage d說法:

'The name 'SavehighScore' does not exist in current context'

代碼:

case State.Gameover: 
        { 
         //Get keyboard state 
         KeyboardState keyState = Keyboard.GetState(); 
         if (keyState.IsKeyDown(Keys.Back)) 
         { 
          if (playerScore > highScore) 
          { 
           highScore = playerScore; 
           SavehighScore(); 
          } 
          playerScore = 0; 
          sharks.Clear(); 
          crew.Clear(); 
          gameState = State.Menu; 
         } 
         break; 
        } 

這是我當玩家死亡,其中全GAMEOVER狀態,其意在顯示有高分。

,如果你能告知什麼最好的方式保存高分的方法中的方法,這將是真正的幫助,以及請:

我是相當新的XNA,還在學習。如果任何人可以得到任何幫助,將真的很感激它。謝謝如果(playerScore> this.highScore)

+6

如果您確實需要幫助,請發佈一個小而全的問題重現。不要將您的整個代碼庫複製到SO。這沒有幫助。 –

+0

道歉,我已經編輯了該部分的文章 – shaneo

+0

這是* from * *最小*工作示例。順便說一句。這可能不會奏效:'public static bool _highScore(int playerScore){if(playerScore> _highScore)...'這可能是拋出異常的地方。 (看看異常的堆棧跟蹤) – Corak

回答

0

只是使用也被錯誤地稱爲)。

你想在這裏完成什麼?如果你只是想要一個方便的方法來檢查高分:

重構爲:

public bool IsHighScore() 
{ 
     return playerScore > highScore; 
} 

然後,在你的遊戲狀態:

case State.Gameover: 
        { 
         //Get keyboard state 
         KeyboardState keyState = Keyboard.GetState(); 
         if (keyState.IsKeyDown(Keys.Back)) 
         { 
          if (IsHighScore()) 
          { 
           highScore = playerScore; 
           SavehighScore(); 
          } 
          playerScore = 0; 

正如其他人說,你寫您的SavehighScore方法。

1
public static bool _highScore (int playerScore) 
    { 
     if(playerScore >_highScore) 
     { 
      return true; 
     } 
     else 
     { 
      return false; 
     } 
    } 

"Here the fist error: Error 1 Operator '>' cannot be applied to operands of type 'int' and 'method group'"

爲什麼你得到這個錯誤的原因是因爲你比較playerScore(一個int)到_highScore方法(這是你

相關問題