Cannot Implicitly Convert Type in XNA

2020-04-19 05:35发布

问题:

i have a bouncing ball, and i tried to make so when it bounces once, the speed gets higher.

In my ball class, i have a float speed;

and i initialized it: public ball(float speed) speed = 1f;

I have a method for my ball movement, that looks like this:

public void BallMovement()
{
    if (movingUp) { ballRect.Y -= speed; }//Error
    if (!movingUp) {  ballRect.Y += speed; }//Error
    if (movingLeft) {  ballRect.X -= speed; }//Error
    if (!movingLeft) {  ballRect.X += speed; }//Error

    if (ballPosition.Y < 85)
    {
        movingUp = false;
    }
    if (ballPosition.Y >= 480)
    {
        movingUp = true;
    }

I then add this in the update method: BallMovement();

It worked before i tried to use the speed variable, it won't compile because of this error:

Cannot implicitly convert type 'float' to 'int'.An explicit conversion exists(are you missing a cast?)

回答1:

The speed needs to be float. If you want to keep the speed as a float, you could create your own rectangle structure. You could do something like this:

        public struct RectangleF
    {
        float w = 0;
        float h = 0;
        float x = 0;
        float y = 0;

        public float Height
        {
            get { return h; }
            set { h = value; }
        }

        //put Width, X, and Y properties here

        public RectangleF(float width, float height, float X, float Y)
        {
            w = width;
            h = height;
            x = X;
            y = Y;
        }

        public bool Intersects(Rectangle refRectangle)
        {
            Rectangle rec = new Rectangle((int)x, (int)y, (int)w, (int)h);
            if (rec.Intersects(refRectangle)) return true;
            else return false;
        }
    }

The intersection checking won't be absolutely perfect, but at least your rectangle's X and Y could have 0.5 added on to them. HTH



回答2:

You're trying to subtract a float value (ex: 1.223488) from an int (ex: 12); You can't do this. Either convert (cast) both values to floats, or convert (cast) both values to ints:

 if (movingUp) { ballRect.Y -= (int)speed; }//Error

The error is basically saying "We can't automatically convert this for you (implicit), but you can convert it yourself (explicit)." I'd check out the MSDN article on type casting: http://msdn.microsoft.com/en-us/library/ms173105.aspx



回答3:

Does the speed needs to be float? If not you could make

int speed;

Or use a explicit cast

if (movingUp) { ballRect.Y -= (int)speed; }// No Error


回答4:

Perhaps speed is declared as type float.

You can do the math by converting speed from float to integer like this:

public void BallMovement()
{
    int speedInt = Convert.Int32(speed);

    if (movingUp) { ballRect.Y -= speedInt; }
    if (!movingUp) {  ballRect.Y += speedInt; }
    if (movingLeft) {  ballRect.X -= speedInt; }
    if (!movingLeft) {  ballRect.X += speedInt; }

    if (ballPosition.Y < 85)
    {
        movingUp = false;
    }
    if (ballPosition.Y >= 480)
    {
        movingUp = true;
    }
    ....

On the other hand, if you want the compiler to convert it for you (multiple times), you could cast each of the occasions where you reference speed with (int)speed.



标签: c# xna pong