如何使用C#在MonoGame中移动我的精灵

3
首先,我对编程还是比较新的,大约只学习了两个月时间,涉猎了一点控制台应用程序和WinForms,但仍在使用难看的控制台来提高算法水平。但现在,我想开始深入游戏开发,因为这是我学习编程的原因。我偶然发现了MonoGame,虽然比Unity难一些,但是仅凭代码就能创造出一个东西让我感到巨大的成就感。我已经制作了太空侵略者和乒乓球,但是没有涉及精灵动画、使用精灵表和移动角色等内容。所以两天前,我开始制作一个平台游戏,分割了我的精灵表,并创建了一些动画,但现在移动我的角色时完全迷失了。我尝试阅读一些有关向量的教程,但在我的情况下并没有帮助。也许你可以给我提供一些指导。

那么,不再多说,以下是代码:

Game.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MafiaJohnny
{

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    private JohnnyPlayer johnnyPlayer;

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

    protected override void Initialize()
    {
        base.Initialize();

    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        Texture2D texture = Content.Load<Texture2D>("JohnnyDone");
        johnnyPlayer = new JohnnyPlayer(texture, 2, 4);
    }

    protected override void UnloadContent()
    {

    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        johnnyPlayer.Update(gameTime);

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.White);

        johnnyPlayer.Draw(spriteBatch, new Vector2(200, 200));

        base.Draw(gameTime);
    }
}
}

JohnnyPlayer.cs

using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace MafiaJohnny
{
class JohnnyPlayer
{
    public Texture2D Texture { get; set; }
    public int Rows { get; set; }
    public int Columns { get; set; }
    private int currentFrame;
    private int totalFrames;

    //Slow down frame animation
    private int timeSinceLastFrame = 0;
    private int millisecondsPerFrame = 400;

    public JohnnyPlayer(Texture2D texture, int rows, int columns)
    {
        Texture = texture;
        Rows = rows;
        Columns = columns;
        currentFrame = 0;
        totalFrames = Rows * Columns;
    }

    public void Update (GameTime gameTime)
    {
        timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
        if (timeSinceLastFrame > millisecondsPerFrame)
        {
            timeSinceLastFrame -= millisecondsPerFrame;

            KeyboardState keystate = Keyboard.GetState();

            //Idle animation
            if (keystate.GetPressedKeys().Length == 0)
            currentFrame++;
            timeSinceLastFrame = 0;
            if (currentFrame == 2)
                currentFrame = 0;

            //Walking Animation
            if (keystate.IsKeyDown(Keys.Left))
            {

            }
        }
    }

    public void Draw (SpriteBatch spriteBatch, Vector2 location)
    {
        int width = Texture.Width/Columns;
        int height = Texture.Height / Rows;
        int row = (int) ((float) currentFrame/Columns);
        int column = currentFrame % Columns;

        Rectangle sourceRectangle = new Rectangle(width * column, height * row, width, height);
        Rectangle destinationRectangle = new Rectangle((int)location.X, (int)location.Y, width, height);

        spriteBatch.Begin();
        spriteBatch.Draw(Texture, destinationRectangle, sourceRectangle, Color.White);
        spriteBatch.End();
    }
}
}

所以这是我的代码,请帮我找到答案,谢谢您的帮助 :)
1个回答

2

2
抱歉回复晚了,我有点忘了,因为我自己已经解决了!我在连接运动和动画方面遇到了一些困难,但最终成功了。谢谢 :) - Maxwell
自己解决问题总是很好的感觉。感觉棒极了! - Sergey.quixoticaxis.Ivanov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接