Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法隐式转换XNA中的类型_C#_Xna_Pong - Fatal编程技术网

C# 无法隐式转换XNA中的类型

C# 无法隐式转换XNA中的类型,c#,xna,pong,C#,Xna,Pong,我有一个弹跳的球,我试着让它弹跳一次,速度会更快 在我的球类课程中,我有一个浮动速度 我初始化了它: 公共球(浮动速度) 速度=1f 我有一个球移动的方法,看起来像这样: public void BallMovement() { if (movingUp) { ballRect.Y -= speed; }//Error if (!movingUp) { ballRect.Y += speed; }//Error if (movingLeft) { ballRect.X

我有一个弹跳的球,我试着让它弹跳一次,速度会更快

在我的球类课程中,我有一个
浮动速度

我初始化了它:
公共球(浮动速度)
速度=1f

我有一个球移动的方法,看起来像这样:

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;
    }
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;
    }
    ....
        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;
        }
    }
public void ballmotation()
{
如果(movingUp){ballcrect.Y-=速度;}//错误
如果(!movingUp){ballcrect.Y+=speed;}//错误
if(movingLeft){ballRect.X-=速度;}//错误
如果(!movingLeft){ballRect.X+=速度;}//错误
if(球位Y<85)
{
movingUp=false;
}
如果(球位置Y>=480)
{
movingUp=true;
}
然后我将其添加到更新方法中:
ballmotation();

在我尝试使用速度变量之前,它已工作,但由于以下错误,它无法编译:

无法将类型“float”隐式转换为“int”。存在显式转换(是否缺少强制转换?)


可能
speed
被声明为type
float

您可以通过将速度从浮点转换为整数来进行计算,如下所示:

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;
    }
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;
    }
    ....
        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;
        }
    }
public void ballmotation()
{
int-speedInt=Convert.Int32(速度);
如果(movingUp){ballcrect.Y-=speedInt;}
如果(!movingUp){ballcrect.Y+=speedInt;}
如果(movingLeft){ballRect.X-=speedInt;}
如果(!movingLeft){ballRect.X+=speedInt;}
if(球位Y<85)
{
movingUp=false;
}
如果(球位置Y>=480)
{
movingUp=true;
}
....

另一方面,如果您希望编译器为您转换它(多次),您可以使用
(int)speed
(int)speed
,在引用
speed
的每种情况下进行转换,可能
speed
被声明为type
float

您可以通过将速度从浮点转换为整数来进行计算,如下所示:

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;
    }
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;
    }
    ....
        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;
        }
    }
public void ballmotation()
{
int-speedInt=Convert.Int32(速度);
如果(movingUp){ballcrect.Y-=speedInt;}
如果(!movingUp){ballcrect.Y+=speedInt;}
如果(movingLeft){ballRect.X-=speedInt;}
如果(!movingLeft){ballRect.X+=speedInt;}
if(球位Y<85)
{
movingUp=false;
}
如果(球位置Y>=480)
{
movingUp=true;
}
....

另一方面,如果您希望编译器为您转换它(多次),您可以使用
(int)speed
(int)speed

,对引用
speed
的每种情况进行强制转换,因为您试图从int(ex:12)中减去浮点值(ex:1.223488);您不能这样做。转换(强制转换)将两个值转换为浮点数,或将两个值转换(强制转换)为整数:

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

错误基本上是说“我们不能为您自动转换(隐式),但您可以自己转换(显式)。”我会查看MSDN关于类型转换的文章:

您试图从int(ex:12)中减去一个浮点值(ex:1.223488);您不能这样做。要么将这两个值转换(强制转换)为浮点值,要么转换(强制转换)将两个值转换为整数:

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

错误基本上是说“我们不能为您自动转换(隐式),但您可以自己转换(显式)。”我会查看MSDN关于类型转换的文章:

速度需要是
浮点值吗

int speed;
或者使用显式强制转换

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

速度
是否需要
浮动

int speed;
或者使用显式强制转换

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

速度需要是浮动的。如果要保持浮动的速度,可以创建自己的矩形结构。可以执行以下操作:

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;
    }
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;
    }
    ....
        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;
        }
    }

交叉点检查并非绝对完美,但至少矩形的X和Y可以加上0.5。HTH

速度需要浮动。如果要将速度保持为浮动,可以创建自己的矩形结构。可以执行以下操作:

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;
    }
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;
    }
    ....
        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;
        }
    }

交叉点检查不是绝对完美的,但至少你的矩形的X和Y可以加上0.5。HTH

但是我有一种方法,在碰撞时使速度增加0.5,这会弄乱碰撞:/但是我有一种方法,在碰撞时使速度增加0.5,这会弄乱碰撞:/