如何从精灵表中将精灵更改为另一个精灵?

时间:2015-03-18 21:42:05

标签: c# xna

我正在开发一款游戏,当玩家按下Tab键时,我想让精灵从精灵表转换到另一艘宇宙飞船。所以就像在太空飞船之间切换一样。

我一直在尝试使用类中的GetSourceRectangle并设置一个,并在游戏中更新它,但它不起作用。

这是Spaceship类的代码:

    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因为我没有足够的声誉(严重堆叠?)但我希望你们明白我的意思。

1 个答案:

答案 0 :(得分:0)

根据你所给予的,我能想到的是:

  • Vector2类上有2个Rectangle(或Draw,您在Spaceship方法中使用的任何对象)对象,其中一个用于标签时的太空船坐标未按下,另一个用于在瓷砖表上按下按键时。
  • 然后,您可以在bool类上设置Game值,以指定要使用的纹理。
  • Update方法上,更新bool值。
  • Draw方法上,根据bool值绘制纹理。

所以基本上在Spaceship类:

Texture2D spaceship;
Vector2 spaceship1, spaceship2;
bool tabPressed;

Update类的Game方法:

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

现在,您可以将bool值传递给Spaceship Draw方法并相应地绘制,或者访问类的属性/方法以表示所需绘图行为的变化。