繁体   English   中英

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

[英]How to move my sprite in MonoGame using C#

首先,我对编程很新,大约2个月,我在控制台应用程序,WinForms中使用了一些,并且仍然使用丑陋的控制台来改善算法。 但现在,我想开始深入研究游戏编程,因为这就是我想学习编程的原因。 我偶然发现了MonoGame,虽然它比Unity更难,但在通过使用代码创建一些东西后,我获得了巨大的成就感。 我已经制作了太空入侵者和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();
    }
}
}

所以这是我的代码,找到我的答案minions! 谢谢你是我的意思:)

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM