Hey guys pls help me i have watched this tutorial: https://www.youtube.com/watch?v=OLsiWxgONeM
i watched the whole video did the exact same thing but i have a 126 errors pls someone help me!
btw this is a video about how to make a 2d sprite animating and walking in directions
ScreenShots:
https://gyazo.com/638092e4f4e5f8fdc6054d2abbee8a72
Scripts etc:
Game1.cs:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Survival.Models;
using Survival.Sprites;
using System.Collections.Generic;
namespace Survival
{
///
/// This is the main type for your game.
///
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
private List<Sprite> _sprites;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
var animations = new Dictionary<string, Animation>()
{
{ "WalkUp", new Animation(Content.Load<Texture2D>("Player/WalkingUp"), 3) },
{ "WalkDown", new Animation(Content.Load<Texture2D>("Player/WalkingDown"), 3) },
{ "WalkLeft", new Animation(Content.Load<Texture2D>("Player/WalkingLeft"), 3) },
{ "WalkRight", new Animation(Content.Load<Texture2D>("Player/WalkingRight"), 3) },
};
_sprites = new List<Sprite>()
{
new Sprite(animations)
{
Position = new Vector2(150, 100),
Input = new Input()
{
Up = Keys.W,
Down = Keys.S,
Left = Keys.A,
Right = Keys.D,
},
},
new Sprite(new Dictionary<string, Animation>()
{
{ "WalkUp", new Animation(Content.Load<Texture2D>("Player/WalkingUp"), 3) },
{ "WalkDown", new Animation(Content.Load<Texture2D>("Player/WalkingDown"), 3) },
{ "WalkLeft", new Animation(Content.Load<Texture2D>("Player/WalkingLeft"), 3) },
{ "WalkRight", new Animation(Content.Load<Texture2D>("Player/WalkingRight"), 3) },
}
Position = new Vector2(100, 100),
Input = new Input()
{
Up = Keys.Up,
Down = Keys.Down,
Left = Keys.Left,
Right = Keys.Right,
},
},
};
// TODO: use this.Content to load your game content here
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// game-specific content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
foreach (var sprite in _sprites)
sprite.Update(gameTime, _sprites);
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (var sprite in _sprites)
sprite.Draw(spriteBatch);
spriteBatch.End();
// TODO: Add your drawing code here
base.Draw(gameTime);
}
}
}
Sprite.cs:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Survival.Managers;
using Survival.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Survival.Sprites
{
public class Sprite
{
#region Fields
protected AnimationManager _animationManager;
protected Dictionary<string, Animation> _animations;
protected Vector2 _position;
protected Texture2D _texture;
#endregion
#region Properties
public Input Input;
public Vector2 Position;
{
get { return _position; }
set
{
_position = Value;
if(_animationManager != null)
_animationManager.Position = _position;
}
}
public float Speed = 1f;
public Vector2 Velocity;
#endregion
#region Method
public virtual void Draw(SpriteBatch spriteBatch)
{
if (_testure != null)
spriteBatch.Draw(_texture, Position, Color.White);
else if (_animationManager != null)
_animationManager.Draw(spriteBatch);
else throw new Exception("This ain't right..!");
}
protected virtual void Move()
{
if (Keyboard.GetState().IsKeyDown(Input.Up))
Velocity.Y = -Speed;
else if (Keyboard.GetState().IsKeyDown(Input.Down))
Velocity.Y = Speed;
else if (Keyboard.GetState().IsKeyDown(Input.Left))
Velocity.X = -Speed;
else if (Keyboard.GetState().IsKeyDown(Input.Down))
Velocity.X = Speed;
}
protected virtual void SetAnimations()
{
if (Velocity.X > 0)
_animationManager.Play(_animations["WalkRight"]);
else if (Velocity.X < 0)
_animationManager.Play(_animations["WalkLeft"]);
else if (Velocity.Y > 0)
_animationManager.Play(_animations["WalkDown"]);
else if (Velocity.Y < 0)
_animationManager.Play(_animations["WalkUp"]);
}
public Sprite(Dictionary<string, Animation> animations)
{
_animations = animations;
_animationManager = new AnimationManager(_animations.First().Value);
}
public Sprite(Texture2D texture)
{
_texture = texture;
}
public virtual void Update(GameTime gametime, List<Sprite> sprites)
{
Move();
SetAnimations();
_animationManager.Update(GameTime);
Position += Velocity;
Velocity = Vector2.Zero;
}
#endregion
}
}
Input.cs:
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Survival.Models
{
public class Input
{
public Keys Down { get; set; }
public Keys Left { get; set; }
public Keys Right { get; set; }
public Keys Up { get; set; }
}
}
Animation.cs:
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Survival.Models
{
public class Animation
{
public int CurrentFrame { get; set; }
public int FrameCount { get; private set; }
public int FrameHeight { get; { return Texture.Height; } }
public float FrameSpeed { get; set; }
public int FrameWidth { get { return Texture.Width / FrameCount; } }
public bool IsLooping { get; set; }
public Texture2D Texture { get; private set; }
public Animation(Texture2D texture, int frameCount)
{
Texture = texture;
FrameCount = frameCount;
IsLooping = true;
FrameSpeed = 0.2f;
}
}
}
AnimationManager.cs: using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Survival.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Survival.Managers
{
class AnimationManager
{
private AnimationManager _animation;
private float _timer;
public Vector2 Position { get; set; }
public AnimationManager(Animation animation)
{
_animation = animation;
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_animation.Texture,
Position,
new Rectangle(_animation.CurrentFrame * _animation.FrameWidth,
0,
_animation.FrameWidth,
_animation.FrameHeight),
Color.White);
}
public void Play(Animation animation)
{
if (_animation == animation)
return;
_animation = animation;
_animation.CurrentFrame = 0;
_timer = 0;
}
public void Stop()
{
_timer = 0f;
_animation.CurrentFrame = 0;
}
public void Update(GameTime gameTime)
{
_timer += (float)gameTime.ElapsedGameTime.TotalSeconds;
if(_timer > _animation.FrameSpeed)
{
_timer = 0f;
_animation.CurrentFrame++;
if (_animation.CurrentFrame >= _animation.FrameCount)
_animation.CurrentFrame = 0;
}
}
}
}