2
\$\begingroup\$

I have created a spritesheet to create animation. Everything is working fine untill I come to a specific frame in one animation.

I have currently 3 animations loops

  1. Idle Animation
  2. Moving Animation
  3. Shooting Animation

Nr 1,2 is working fine and behave like they should but when I am shooting my sprite shifts a little to the left. And I don't why it does.

Here is a gif for the animations enter image description here

Here is the spritesheet I am using for the animations enter image description here

And here is the code I am using for looping and shifting different animations

public void DrawSpriteSheetFrame(CustomTexture2D ct, Vector2 position, StateOfObject soo, GameTime gt, SpriteFont sf)
    {
        if (!_currentstate.Equals(soo))
        {
            _currentframe = 0;
        }
        _currentstate = soo;

        List<SpriteFrameInfo> currentLoopsheet = ct.SubTexture.FindAll(x => x.name.Contains(soo.ToString()));
        _timeSinceLastFrame += gt.ElapsedGameTime.Milliseconds;
        if (_timeSinceLastFrame > _frameUpdateSpeed)
        {
            _timeSinceLastFrame -= _frameUpdateSpeed;
            if (_currentframe <= currentLoopsheet.Count - 1)
            {
                _currentframe++;
            }
            else _currentframe = 0; 
        }
        var t = currentLoopsheet.Where(x => x.name.Contains(_currentframe.ToString())).FirstOrDefault();
        Vector2 origin = new Vector2(t.width, t.height);
        var rec = new Rectangle(0,0, 400,200);
        _spriteBatch.Begin();
        _spriteBatch.DrawString(sf, "Currentsheet: " + t.name, new Vector2(100), Color.White);
        _spriteBatch.DrawString(sf, "Position X: " + position.X, new Vector2(100,150), Color.White);
        _spriteBatch.DrawString(sf, "Position Y: " + position.Y, new Vector2(100,200), Color.White);
        _spriteBatch.DrawString(sf, "SheetCord X: " + t.x, new Vector2(100, 250), Color.White);
        _spriteBatch.DrawString(sf, "SheetCord Y: " + t.y, new Vector2(100, 300), Color.White);
        _spriteBatch.Draw(ct.Texture, position, new Rectangle(new Point(t.x, t.y), new Point(t.width, t.height)), Color.White, 0, origin, 1, SpriteEffects.None, 1);
        //DrawBorder(new Rectangle(new Point(t.x, t.y), new Point(t.width, t.height)), 1, Color.Black);
        //_spriteBatch.Draw(ct.Texture, position, new Rectangle(new Point(t.x, t.y), new Point(t.width, t.height)), Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);
        //_spriteBatch.Draw(ct.Texture, position, rec, Color.White, 0, Vector2.Zero, 1, SpriteEffects.None, 1);

        _spriteBatch.End();
    }
\$\endgroup\$
1
\$\begingroup\$

I think one frame of the shooting animation has a different width than the rest of the frames, which seems noticeable when looking at the sprite sheet.

EDIT: Looking at it again, I think this has to do with the sheetCord, which translates to me to "Spritesheet Coördination". So the coder who made this uses a spritesheet as the whole image, and uses specific positions for every frame of the spritesheet to show the right image out of it.

It appears that one position is misarranged, likely edited by someone else. so if you're able to find the location where all spritesheet positions are located, you can also change the value of the position of the misplaced frame to correct it again.

Hope this helps.

\$\endgroup\$
  • \$\begingroup\$ Well many of the frames have different width. But why are just SOME of them changing the position of my sprite and not all of them? \$\endgroup\$ – tirri Apr 4 at 9:28
  • \$\begingroup\$ I've updated my answer with a more likely solution. it looks like the sheetCord value for one of the sprites is incorrect. \$\endgroup\$ – Steven Apr 4 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.