2

だから私は数ヶ月前にC#コードを学び始めたばかりで、完全な歩行サイクルでスプライトを取得したいと思っています。

PNG画像で歩行サイクルがあります。これは明らかに、歩行サイクルのさまざまな段階を示す一連の個別の画像です。これで、プロジェクト/ソリューションに組み込まれました。これは、VisualStudio2010でWindowsゲーム4.0として実行しています。

そして、私はかなり遠いので、srcRectとdestRectのセットアップを試み始めました。しかし、私が画面に表示できたのは、画像を左から右に非常に速くフリックしてから、無限に右に移動することだけです。これは、srcRect.X +=srcRect.width行を使用して実現しました。

しかし、私がやりたいのは、この動きをはるかに遅くすることです。サムスティックを右に押したときに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);
        }
    }
}

4

1 に答える 1

0

必要なことを実行する簡単な方法は、おそらくカウンターを実装することです。

コードの先頭にpublicdoubleanimCounter=0のようなものを追加します。

次に、srcRect.Xの周り+ = srcRect.Width; 行は、次の効果に何かを追加します。

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

繰り返しますが、これはあなたがやりたいことをする簡単な方法ですが、必ずしも最高ではありません。1つは、+ = srcRect.Widthがスプライトシートの幅をすぐに超えるため、それを確認して修正する必要があります。itsme86が示唆しているように、いくつかのチュートリアルをGoogleで検索する必要があります。

これは悪いスタートではありません:http: //msdn.microsoft.com/en-us/library/bb203866.aspx

于 2013-01-25T17:21:14.687 に答える