简体   繁体   English

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

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

First off, i'm fairly new to programming, about 2 months in, i dabbled a bit in Console Application, WinForms, and still using the ugly console to get better at algorithms in general. 首先,我对编程很新,大约2个月,我在控制台应用程序,WinForms中使用了一些,并且仍然使用丑陋的控制台来改善算法。 But now, I wanna start digging into game programming, because that's the reason i wanted to learn programming. 但现在,我想开始深入研究游戏编程,因为这就是我想学习编程的原因。 I stumbled upon MonoGame, and while it's harder than say Unity, I got an immense sense of achievement after creating something by just using code. 我偶然发现了MonoGame,虽然它比Unity更难,但在通过使用代码创建一些东西后,我获得了巨大的成就感。 I already made a Space Invaders and Pong but nothing related to sprite animation, using spritesheets and moving a player. 我已经制作了太空入侵者和Pong,但没有任何与精灵动画相关的东西,使用精灵表格和移动玩家。 So 2 days ago, I started a platformer, divided my spritesheet, got some animation down, and now that it's time to move my player, I'm completely lost. 因此,2天前,我开始制作一个平台游戏,分割我的精灵片,得到一些动画,现在是时候移动我的播放器了,我完全迷失了。 I tried reading some tutorials on vectors, but it doesn't help in my case. 我尝试阅读一些关于向量的教程,但它对我的情况没有帮助。 Maybe you can shed some light on the matter. 也许你可以对这个问题有所了解。

So, without further ado, here's the code: 所以,不用多说了,这是代码:

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

So here's my code, find me an answer minions! 所以这是我的代码,找到我的答案minions! Thank you is what I mean :) 谢谢你是我的意思:)

You just need to change "location" so the sprite moves left/right/up/down. 您只需要更改“位置”,以便精灵向左/向右/向上/向下移动。 Also I recommend moving this code from JohnnyPlayer to another "controller" class. 另外,我建议将此代码从JohnnyPlayer移动到另一个“控制器”类。

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

They make a sprite and move it from left to right. 他们制作精灵并从左向右移动。 In your case the texture on sprite changes in time (animation) but the movement is still the same. 在你的情况下,精灵上的纹理随时间变化(动画)但运动仍然是相同的。

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

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