Home > Mobile >  C# cannot convert from 'float' to 'Microsoft.Xna.Framework.Point'
C# cannot convert from 'float' to 'Microsoft.Xna.Framework.Point'

Time:11-15

I'm Trying to make a rougelike in C# using Monogame and RogueSharp.

here is some of the code I have in Draw()

protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

            int sizeOfSprites = 64;
            float scale = .25f;
            foreach (Cell cell in _map.GetAllCells())
            {
            // error in the line below    
            Microsoft.Xna.Framework.Rectangle position = new Microsoft.Xna.Framework.Rectangle(cell.X * sizeOfSprites * scale, cell.Y * sizeOfSprites * scale);
                if (cell.IsWalkable)
                {
                    _spriteBatch.Draw(floorSprite, position, null, Color.White, 0f, new Vector2(scale, scale), 0.0f, 0f);
                }
                else
                {
                    _spriteBatch.Draw(wallSprite, position, null, Color.White, 0f, new Vector2(scale, scale), 0.0f, 0f);
                }
            }

            _spriteBatch.End();

            base.Draw(gameTime);
        }

Returns:

Argument 2: cannot convert from 'float' to 'Microsoft.Xna.Framework.Point'

Argument 1: cannot convert from 'float' to 'Microsoft.Xna.Framework.Point'

CodePudding user response:

The Rectangle constructor either takes (Point location, Point size) or (Int32 x, Int32 y, Int32 width, Int32 height) but not (Float, Float) (what would that even mean?) - See docs.

So, two issues here:

  • You need four values to uniquely specify a rectangle in 2D space, not two. Rectangle does that using X and Y of the top-left corner as well as width and height.
  • The Rectangle constructor expects those values as integers and not floats.

Looking at your code, I'm guessing that you want to create a rectangle that has sizeOfSprites * scale as length of both sides, in which case you'd need to specify the size as follows:

Microsoft.Xna.Framework.Rectangle position = new Microsoft.Xna.Framework.Rectangle(
  (int)Math.Floor(cell.X * sizeOfSprites * scale),
  (int)Math.Floor(cell.Y * sizeOfSprites * scale),
  (int)Math.Floor(sizeOfSprites * scale),
  (int)Math.Floor(sizeOfSprites * scale)
);

I'm not sure what Cell is and whether its X/Y can be floats or not, so in case Math.Floor (rounding down) isn't the type of rounding you need, you can use Math.Round instead.

  • Related