2015-03-18 213 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方法和借鉴相应地,或者访问该类的属性/方法来发信号通知所需绘图行为的改变。

相关问题