简体   繁体   中英

Unable to create collision rectangle

I am trying to make a small game for my programming class at college and I am having a problem trying to create a rectangle for collision.

When I try and use the width and height from my texture. I get an error telling me that I can't convert from a float to an int. But the pixel size of the image is not a float value?

Here is the code I have in my game objects class (there is a lot of comments to help direct where things are meant to go):

class ButtonSprite
{
    public Texture2D Art;
    public Vector2 Position;

    public ButtonSprite(Vector2 pos, Texture2D tex)
    {
        // Copy the texture "tex" into the "Art" class variable
        Art = tex;
        // Copy the vector "pos" into the "Position" class variable
        Position = pos;
    }

    public void DrawMe(SpriteBatch sb, Color col)
    {
        // use the spritebatch "sb" to draw the sprite at "Position" using the texture "Art" with the tint from "col"
        sb.Draw(Art, Position, col);
    }
}

class PlayerSprite
{
    public Texture2D Art;
    public Vector2 Position;
    public Rectangle CollisionRect;

    public PlayerSprite(Vector2 pos, Texture2D tex)
    {
        // Copy the texture "tex" into the "Art" class variable
        Art = tex;
        // Copy the vector "pos" into the "Position" class variable
        Position = pos;
        // create a new CollisionRect Rectangle using the X and Y from Position and the Width and Height from Art
        CollisionRect = new Rectangle(Position.X, Position.Y, Art.Width, Art.Height);
    }

    public void UpdateMe(ButtonState leftB, ButtonState rightB, ButtonState downB, ButtonState upB)
    {
        // if leftB is pressed
        if (leftB == ButtonState.Pressed)
        {
            // subtract 1 from the X that belongs to Position
            Position.X -= 1;
        }
        // endif

        // if rightB is pressed
        if (rightB == ButtonState.Pressed)
        {
            // add 1 to the X that belongs to Position
            Position.X += 1;
        }
        // endif

        // if downB is pressed
        if (downB == ButtonState.Pressed)
        {
            // add 1 to the Y that belongs to Position
            Position.Y += 1;
        }
        // endif

        // if upB is pressed
        if (upB == ButtonState.Pressed)
        {
            // subtract 1 from the Y that belongs to Position
            Position.Y -= 1;
        }
        // endif

        // set the X that belongs to CollisionRect to equal the integer version of the X that belongs to Position
        // set the Y that belongs to CollisionRect to equal the integer version of the Y that belongs to Position
    }

    public void DrawMe(SpriteBatch sb)
    {
        // use the spritebatch "sb" to draw the sprite at "Position" using the texture "Art" with a white tint
        sb.Draw(Art, Position, Color.White);
    }
}
}

Right off the bat, I would suggest you calculate your collisions using the movement too. This will prevent more glitches (eg passing through walls).

A good way to do this is to inflate the target rectangle (temporarily) by the size of the moving rectangle. Then, you perform a line to bounding box intersection. This will give you an intersection point, while being safer against glitches.

Try this line when building your collision rect. It will convert the height & width floats to integers. The + 1 is optional.

CollisionRect = new Rectangle(Position.X, Position.Y, (int)Art.Width + 1, (int)Art.Height + 1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM