2015-03-18 250 views
0

我正在製作一款遊戲,當玩家按下Tab鍵時,我希望從精靈表變爲另一個飛船。所以它喜歡在飛船之間切換。如何讓精靈從精靈表變成另一個精靈?

我已經從類試圖與GetSourceRectangle並設置一個,和更新在遊戲中,但它不工作。

這裏是來回的飛船類的代碼:

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace SpaceShooterTest1 
{ 
    class Spaceship 
    { 
     public int xPos = 0; 
     public int yPos = 0; 
     private int width = 128; 
     private int height = 128; 
     private Texture2D texture; 

     public Rectangle currentSourceRect { get; set; } //determines which part of sprite sheet to show 

     public Spaceship(Texture2D tex) 
     { 
      texture = tex; 
      currentSourceRect = new Rectangle(0, 0, 128, 128); 
     }//end Spaceship 

     // // // 
     public void MoveToPosition(int x, int y) 
     { 
      xPos = x; 
      yPos = y; 
     }//end MoveToPosition 

     public void Update(GameTime gametime) 
     { 
      currentSourceRect = GetSourceRectangle(2, 0); // this could be getting the fiery weapons 
      //currentSourceRect = SetSourceRectangle(2, 0); 
     } 

     public void Draw(GameTime gameTime, SpriteBatch spriteBatch) 
     { 
      spriteBatch.Draw(texture, new Rectangle(xPos, yPos, width, height), currentSourceRect, Color.White); 

     }//end Draw 

     public Rectangle GetSourceRectangle(int row, int col) 
     { 
      Rectangle r; 

      //TODO: Make custom based on row and col 

      r = new Rectangle(0, 128, width, height); 

      return r; 
     }//end GetSourseRectangle 


     //public Rectangle SetSourceRectangle(int row, int col) 
     //{ 
     // Rectangle r; 

     // //TODO: Make custom based on row and col 

     // r = new Rectangle(0, 128, width, height); 

     // return r; 
     //}//end GetSourseRectangle 

    } 
} 

這裏是遊戲的代碼:

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; 

namespace SpaceShooterTest1 
{ 
    /// <summary> 
    /// This is the main type for your game 
    /// </summary> 
    public class Game1 : Microsoft.Xna.Framework.Game 
    { 
     //Game State Enum 
     enum GameState { GScreen, Playing, Won, Lost }; 

     GraphicsDeviceManager graphics; 
     SpriteBatch spriteBatch; 
     Random rand; 

     int playerScore = 0; 

     //Textures 
     Texture2D galaxyScreen; 
     Texture2D texShip; 

     GameState currentState = GameState.Playing; 

     //GameState currentState = GameState.GScreen; /// use after 

     //ship 
     Spaceship spaceShip; 

     public Game1() 
     { 
      graphics = new GraphicsDeviceManager(this); 
      Content.RootDirectory = "Content"; 
     } 

     /// <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 

      base.Initialize(); 

      rand = new Random(); 
     } 

     /// <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); 

      texShip = Content.Load<Texture2D>(@"Images\ships_sm"); 
      spaceShip = new Spaceship(texShip); 

      spaceShip.xPos = 0; 
      spaceShip.yPos = Window.ClientBounds.Height - 128; 

      //galaxyScreen = Content.Load<Texture2D>(@"Images\galaxy"); 

      // 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) 
     { 
      // Allows the game to exit 
      if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
       this.Exit(); 

      KeyboardState keyboardState = Keyboard.GetState(); 

      if (Keyboard.GetState().IsKeyDown(Keys.Tab)) 
      { 
       spaceShip.GetSourceRectangle(2, 0); 

      } 

      if (Keyboard.GetState().IsKeyDown(Keys.D)) 
      { 
       spaceShip.xPos += 5; 
      } 
      else if (Keyboard.GetState().IsKeyDown(Keys.A)) 
      { 
       spaceShip.xPos -= 5; 
      } 

      if (spaceShip.xPos < 0) 
       spaceShip.xPos = 0; 

      if (spaceShip.xPos + 128 > Window.ClientBounds.Width) 
      { 
       spaceShip.xPos = Window.ClientBounds.Width - 128; 
      } 



      /*if (currentState == GameState.GScreen && keyboardState.IsKeyDown(Keys.Space)) 
      { 
       currentState = GameState.Playing; 
      } 

      spaceShip.Update(gameTime); 
      */ 

      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); 

      //draw ship 
      spriteBatch.Begin(); 

      if (currentState == GameState.Playing) 
      { 
       spaceShip.Draw(gameTime, spriteBatch); 
      } 

      //800 wide x 400 high 
      else if (currentState == GameState.GScreen) 
      { 
       spriteBatch.Draw(galaxyScreen, new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height), Color.White); 

      } 

      spriteBatch.End(); 
      base.Draw(gameTime); 
     } 


    } 
} 

顯然我不能張貼spritesheet,因爲我沒有足夠的聲譽(嚴重)?但我希望你們明白我的意思。

+0

我已刪除從你的問題的標題標籤(一個或多個)基於_ [應的問題包括「標籤」,在他們的頭銜?(http://meta.stackexchange.com/questions/19190/should-questions-include-標籤在他們的標題)_其中共識是**否** _他們不應該。 – MickyD 2015-04-02 00:41:44

回答

0

有了你給什麼,我能想到的是:

  • 有2 Vector2(或Rectangle,取其對象,你在你的Draw方法使用)在Spaceship類的對象,一個用於飛船的座標當Tab未被按下時,另一個用於在瓷磚上按鍵的時間。
  • 然後你就可以對Gamebool值,指定要使用的紋理。
  • Update方法中,更新bool值。
  • Draw方法,畫出根據bool值質地。

所以基本上在Spaceship類:

Texture2D spaceship; 
Vector2 spaceship1, spaceship2; 
bool tabPressed; 

Game類的Update方法:

tabPressed = Keyboard.GetState().IsKeyDown(Keys.Tab); 

現在,你可以在布爾值傳遞給你的Spaceship Draw方法和借鑑相應地,或者訪問該類的屬性/方法來發信號通知所需繪圖行爲的改變。

相關問題