Cannot Implicitly Convert Type in XNA

前端 未结 4 1820
夕颜
夕颜 2021-01-24 20:19

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 initialize

4条回答
  •  孤独总比滥情好
    2021-01-24 20:51

    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.

提交回复
热议问题