2016-07-23 2 views
2

Zunächst einmal bin ich ziemlich neu in der Programmierung, etwa 2 Monate in, habe ich ein wenig in Console Application, WinForms, und immer noch die hässliche Konsole, um besser bei Algorithmen in Allgemeines. Aber jetzt möchte ich mit der Programmierung von Spielen anfangen, denn das ist der Grund, warum ich programmieren lernen wollte. Ich stolperte über MonoGame, und obwohl es schwieriger ist als Unity zu sagen, bekam ich ein immenses Erfolgserlebnis, nachdem ich etwas nur mit Code erstellt hatte. Ich habe bereits Space Invaders und Pong erstellt, aber nichts mit Sprite-Animation, Spritesheets und Bewegen eines Spielers. Also habe ich vor 2 Tagen einen Platformer gestartet, mein Spritesheet geteilt, etwas Animation heruntergefahren, und jetzt, da es Zeit ist, meinen Player zu bewegen, bin ich komplett verloren. Ich habe versucht, ein paar Tutorials zu Vektoren zu lesen, aber in meinem Fall hilft das nicht. Vielleicht können Sie etwas Licht in die Sache bringen.Wie verschiebe ich mein Sprite in MonoGame mit C#

Also, ohne weitere Umschweife, hier ist der Code:

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

meinen Code Also hier ist, finden Sie mir eine Antwort Schergen! Danke, was ich meine :)

Antwort

1

Sie müssen nur "Standort" ändern, so dass das Sprite nach links/rechts/oben/unten bewegt. Außerdem empfehle ich, diesen Code von JohnnyPlayer in eine andere "Controller" -Klasse zu verschieben.

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

Sie machen einen Sprit und verschieben Sie sich von links nach rechts. In deinem Fall ändert sich die Textur auf Sprite mit der Zeit (Animation), aber die Bewegung ist immer noch die gleiche.

+1

Sorry für die späte Antwort, irgendwie vergessen, seit ich es selbst herausgefunden habe! Es fiel mir schwer, Bewegung und Animation miteinander zu verbinden, aber ich habe es schließlich zum Laufen gebracht. Danke :) – Maxwell

+0

Es ist immer gut, selbst etwas herauszufinden. Fühlt sich toll an) –