【问题标题】:How to move my sprite in MonoGame using C#如何使用 C# 在 MonoGame 中移动我的精灵
【发布时间】:2016-07-23 18:47:40
【问题描述】:

首先,我对编程相当陌生,大约 2 个月后,我涉足了控制台应用程序、WinForms,并且仍然使用丑陋的控制台来更好地处理算法。但现在,我想开始深入研究游戏编程,因为这就是我想学习编程的原因。我偶然发现了 MonoGame,虽然它比 Unity 更难,但在仅使用代码创建某些东西后,我获得了巨大的成就感。我已经制作了 Space Invaders 和 Pong 但与精灵动画、使用精灵表和移动玩家无关。所以 2 天前,我开始了一个平台游戏,分割了我的精灵表,做了一些动画,现在是时候移动我的播放器了,我完全迷失了。我尝试阅读一些关于向量的教程,但对我来说没有帮助。也许你可以对这件事有所了解。

那么,废话不多说,代码如下:

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();
    }
}
}

这是我的代码,帮我找个答案!谢谢你是我的意思:)

【问题讨论】:

    标签: c# monogame


    【解决方案1】:

    您只需要更改“位置”,以便精灵向左/向右/向上/向下移动。我还建议将此代码从 JohnnyPlayer 移至另一个“控制器”类。

    这里: http://www.gamefromscratch.com/post/2015/06/15/MonoGame-Tutorial-Creating-an-Application.aspx

    他们制作了一个精灵并将其从左向右移动。在您的情况下,精灵上的纹理会随时间变化(动画),但运动仍然相同。

    【讨论】:

    • 抱歉回复晚了,我自己弄明白了,有点忘了!我很难将运动和动画联系起来,但最终还是让它发挥了作用。谢谢:)
    • 自己解决问题总是好的。感觉很棒)
    猜你喜欢
    • 1970-01-01
    • 2017-06-06
    • 1970-01-01
    • 2021-11-12
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多