spritedrawxna-4.0

Wrapping drawn image in XNA 4.0


As you see in the picture above, billy, our little orange dressed man, is going off the screen to the left and appearing on the right side.

This is what I want to happen, however, I can't seem to replicate this effect (I got the picture with some modifications in Paint.net).

Basically, if the players x position is negative, it should wrap to the other side. If the x is larger than the width, it should wrap over to the left side.


Solution

  • Draw it two times when you need it.

     void Draw(SpriteBacth batch)
     {
          batch.Draw(Player.texture, Player.Position, player.Source, player.Color);
    
    
          if (Player.X <0)
          {
               bacth.Draw(Player.texture, Player.Position + ScreenHorizontalSize, player.Source, Player.Color);
          }
          else if (Player.X + Player.Size.Width> ScreenHorizontalSize.Width)
          {
               bacth.Draw(Player.texture, Player.Position - ScreenHorizontalSize, player.Source, Player.Color);
          }
      }
    
      void Update()
      {
          if (Player.X < -Player.Size.Width) Player.X += ScreenHorizontalSize.Width;
          if (Player.X > ScreenHorizontalSize.Width) Player.X -= ScreenHorizontalSize.Width;
      }
    

    Of course you have to take in mind this when you check for colliding with player, you will have to check in the two positions too.