1
\$\begingroup\$

I'm currently making a shoot em up in monogame.

I want to add just one animation to my object. No commands or transitions between animations for different actions: just an object swapping between different images all the time.

For example, my player will have 4 different images for it and I'd want it to always swap trough these images.

Animation guides I've reviewed seem way too complicated for my needs. Unlike the examples they cover, I don't want different animations for different commands.

How can I make this type of simple, always-looping frame animation?

Edit

I managed to make a constantly looping animation to a sprite and but I don't know how to insert into myPlayer.

Game1:
        Texture2D myPlayerAnim;
        Rectangle myDestRect;
        Rectangle mySourceRect;
        Texture2D myEnemyAnim;
        Rectangle myEnemyDestRect;
        Rectangle myEnemySourceRect;
        float myElapsed;
        float myDelay = 100f;
        int myFrames = 0;

Initialize:
            myDestRect = new Rectangle(0, 0, 512, 512);
            myEnemyDestRect = new Rectangle(0, 0, 808, 608);

LoadContent: 

            myPlayerAnim = Content.Load<Texture2D>("SpriteSheetPlayerAnim");
            myEnemyAnim = Content.Load<Texture2D>("SpriteSheetEnemyAnim");

Update:    
            myElapsed += (float)aGameTime.ElapsedGameTime.TotalMilliseconds;

            if (myElapsed >= myDelay)
            {
                if (myFrames >= 3)
                {
                    myFrames = 0;
                }
                else
                {
                    myFrames++;
                }
                myElapsed = 0;
            }

            mySourceRect = new Rectangle(512 * myFrames, 0, 512, 512);
            myEnemySourceRect = new Rectangle(808 * myFrames, 0, 808, 608);

Draw: 

            mySpriteBatch.Draw(myPlayerAnim, myDestRect , mySourceRect, 
            Color.White);
            mySpriteBatch.Draw(myEnemyAnim, myEnemyDestRect, myEnemySourceRect, 
            Color.White);´

Draw just draws the animations out but I don't know how to insert it into myPlayer. For myPlayer I currently have this but I couldn't figure out how to do it with my animations.

Draw: 
myPlayer = new Player(TextureLibrary.GetTexture("player"), myPlayerPos, 200, new Vector2(.3f, .3f), 0, Color.White, 1000, 1);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ For beginners, I suggest that you get through the tutorial below first. 2D XNA Tutorials - RB Whitaker's Wiki Also your problem (which we call texture atlases) is lying in this link: Texture Atlases - Part 1 Just follow the tutorial from the beginning. Hope this helps! \$\endgroup\$
    – Silver
    Commented May 29, 2019 at 16:22

1 Answer 1

0
\$\begingroup\$

Based on your code you have given. I think you just need to change some things.

First, use the SpriteSheetPlayerAnim as the texture of your player directly instead of using player texture.

Then move the code of Update and Draw of player's animation to Update and Draw of Player class.

They will be like this:

public class Player
{
    //Default constructor
    public Player()
    {
        //default values for your player//for example only
        //this.hp = 100;
        //this.mp = 20;
    }

    //Constructor
    public Player(GameTime myelapsedGameTime, Texture2D texPlayer, Vector2 playerPosition, int something, Vector2 vector2ForSomething, int intFST, Color playerColor, ...)
    {
        //initialise player's attributes

    }

    public override Update(GameTime gameTime)
    {
        if (myElapsed >= myDelay)
        {
            if (myFrames >= 3)
            {
                myFrames = 0;
            }
            else
            {
                myFrames++;
            }
            myElapsed = 0;
        }
    }

    public override Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(myPlayerAnim, myDestRect , mySourceRect, 
            Color.White);//for example
    }
}

Then in your Game1 class:

public class Game1 : Game
{
    Player player;
    GameTime myElapsed;

    public Game1()
    {
        //
    }

    protected override void LoadContent()
    {
        //Load your animated sprite here
        myPlayerAnim = Content.Load<Texture2D>("SpriteSheetPlayerAnim");

        player = new Player(myElapsed, myPlayerAnim, ...);//constructor with attributes here or whatever you want
    }

    protected override void Update(GameTime gameTime)
    {
        myElapsed += (float)aGameTime.ElapsedGameTime.TotalMilliseconds;
        player.Update(gameTime);
    }

    protected override void Draw(SpriteBatch spriteBatch)
    {
        player.Draw(spriteBatch);
    }
}

Hope this helps!

For beginners, I suggest that you get through the tutorial below first.

2D XNA Tutorials - RB Whitaker's Wiki

Also your problem (which we call texture atlases) is lying in this link:

Texture Atlases - Part 1

Just follow the tutorial from the beginning. Hope this helps!

\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .