c# - 步行周期所需的代码? XNA Visual Studio C#

标签 c# visual-studio-2010 xna

所以我几个月前才开始学习 C# 代码,我想在一个完整的步行周期中获得一个 Sprite 。

我在 PNG 图像上有我的步行周期。这显然是一系列单独的图像,显​​示了步行周期的不同阶段。现在,我已将其纳入我的项目/解决方案,我在 visual studio 2010 中将其作为 Windows 游戏 4.0 进行处理。

而且我还差得远,所以我开始尝试设置 srcRect 和 destRect。但我设法在屏幕上看到的是图像从左向右快速闪烁,然后无限向右移动。我通过使用 srcRect.X += srcRect.width 线实现了这一点。

但我想做的是让这个 Action 慢得多。当向右插入拇指杆时,我希望 destRect 在屏幕上向右移动,并且当发生这种情况时,我希望它在我的步行周期中向右轻弹。

我越来越糊涂了,我是一个初学者,所以任何帮助都将不胜感激!到目前为止,下面是我的代码!


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 fra_walk
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        GamePadState pad1, oldpad1;

        Rectangle srcRect;

        struct Rect
        {
        public Rectangle destRect;
        public Rectangle frame;
        public Point velocity;

        }
            Rect destRect;

        struct Wright
        {
            public Texture2D txr;
            public Rectangle frame;
        }

        Wright walkright;

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


            graphics.PreferredBackBufferWidth = 800;
            graphics.PreferredBackBufferHeight = 600;

        }

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

            destRect.velocity = new Point(0, 0);

            destRect.frame = new Rectangle(400, 400, 83, 170);

            srcRect = new Rectangle(0, 0, 64, 170);

            base.Initialize();
        }

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



            walkright.txr = Content.Load<Texture2D>("walk right");

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


            GamePadState pad1 = GamePad.GetState(PlayerIndex.One);


            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            srcRect.X += srcRect.Width;

        //  if ((pad1.ThumbSticks.Left.X == 1.0f)
         //     && (oldpad1.ThumbSticks.Left.X == 1.0f))
       //   {
                // srcRect.X += destRect.frame.Width;
        //  }

            oldpad1 = pad1;
            // TODO: Add your update logic here

            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();
            spriteBatch.Draw(walkright.txr, destRect.frame, srcRect, Color.White);
            spriteBatch.End();

            // TODO: Add your drawing code here

            base.Draw(gameTime);
        }
    }
}

最佳答案

执行您想要的操作的一种简单方法可能是实现一个计数器。

在代码顶部添加类似 public double animCounter = 0 的内容。

然后绕srcRect.X += srcRect.Width;行添加一些东西的效果:

        if (animCounter > 0)
        {
            animCounter -= gameTime.ElapsedGameTime.TotalSeconds;
        }
        else
        {
            srcRect.X += srcRect.Width;
            animCounter = 0.5f;
        }

同样,这是做您想做的事情的简单方法,但不一定是最好的方法。其一,+= srcRect.Width 将很快超过您的 sprite 表的宽度,因此您需要检查并更正它。正如 itsme86 所建议的,您应该在 Google 上搜索一些教程。

这不是一个糟糕的开始: http://msdn.microsoft.com/en-us/library/bb203866.aspx

关于c# - 步行周期所需的代码? XNA Visual Studio C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14490721/

相关文章:

c# - Visual Studio 2010 : How refer to a C# . 具有第三方依赖项的 Net 类库项目

C#/XNA/HLSL - 深度图搞砸了

c# - 发送矩阵的方向...转换为度数?弧度?没有小数点的东西?

c# - 我正在制作我的第一个加密程序,有什么建议吗?

c# - 设计一个跨平台的通信接口(interface)

c++ - 在特定条件下迭代文件夹的文件

visual-studio-2010 - 你如何将 'System::String ^' 转换为 'TCHAR' ?

c# - XNA 窗口缩放性能

asp.net - 在 asp.net 中使用 c# 预编译的网站中缺少 .cs 文件

c# - 如何使用 IHasManyConvention Fluent NHibernate 约定在 HasMany 映射上设置 PropertyRef?