我有一个弹跳球,我试图让它弹跳一次时,速度会变得更高。
在我的球课上,我有一个float speed;
我初始化了它:public ball(float speed)
speed = 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;
}
然后我将其添加到更新方法中:BallMovement();
在我尝试使用速度变量之前它就工作了,由于这个错误它不会编译:
无法将类型“float”隐式转换为“int”。存在显式转换(是否缺少强制转换?)
速度需要浮动。如果您想将速度保持为浮动,您可以创建自己的矩形结构。你可以这样做:
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。华泰
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)