无法将'bool'隐式转换为'int'-冲突检测 [英] Cannot implicitly convert 'bool' to 'int' - collision detection

查看:48
本文介绍了无法将'bool'隐式转换为'int'-冲突检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为足球比赛创建一个简单的碰撞检测类.这是代码:

  int Collision(int x1,int y1,int radius1,int x2,int y2,int radius2){int dx = x2-x1;int dy = y2-y1;整数半径=半径1 +半径2;如果((dx * dy)+(dy * dy)<半径*半径){返回true;}别的{返回false;}} 

问题在于代码返回true或false.Visual Studio表示它不能将bool隐式转换为int,我理解这一点,但是我该如何解决呢?感谢您的帮助.

解决方案

像这样定义您的函数:

  bool Collision(int x1,int y1,int radius1,int x2,int y2,int radius2) 

现在,您可以返回 true false .如果保留 int ,则需要返回一个整数值,例如 0 1 ,但这并不表示函数意图.

您还可以将代码缩短一点:

  bool Collision(int x1,int y1,int radius1,int x2,int y2,int radius2){int dx = x2-x1;int dy = y2-y1;整数半径=半径1 +半径2;return((dx * dy)+(dy * dy)<半径*半径);} 

I am trying to make a simple collision detection class for a soccer game. Here is the code:

int Collision(int x1, int y1, int radius1, int x2, int y2, int radius2)  
{  
    int dx = x2 - x1;  
    int dy = y2 - y1;  
    int radii = radius1 + radius2;  
    if ((dx*dy)+(dy*dy)< radii * radii)  
    {  
        return true;  
    }  
    else  
    {  
        return false;  
    }  
}  

The problem is with the code returning true or false. Visual Studio says it cannot implicitly convert bool to int, and I understand that, but how can I fix it? Thanks for any help.

解决方案

Define your function like this:

bool Collision(int x1, int y1, int radius1, int x2, int y2, int radius2)

Now you can return true or false. If you keep int then you need to return an integer value such as 0 and 1 but this doesn't express the function intent.

You could also shorten your code a bit:

bool Collision(int x1, int y1, int radius1, int x2, int y2, int radius2)  
{  
    int dx = x2 - x1;  
    int dy = y2 - y1;  
    int radii = radius1 + radius2;  
    return ((dx * dy) + (dy * dy) < radii * radii);
}

这篇关于无法将'bool'隐式转换为'int'-冲突检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆