我有一个反弹球,我试图让所以当它反弹一次,速度变得更高。
在我的球课,我有一个浮速度;
和我初始化它: 公共球(浮点速度) 速度= 1F;
我有我的球运动的方法,看起来像这样:
公共无效BallMovement() {如果(movingUp){ballRect.Y - =速度; } //错误如果{ballRect.Y + =速度(movingUp!); } //错误如果(movingLeft){ballRect.X - =速度; } //错误如果{ballRect.X + =速度(movingLeft!); } //错误 如果(ballPosition.Y< 85) { movingUp = FALSE; } 如果(ballPosition.Y> = 480) { movingUp = TRUE; }
然后我在更新方法补充一点: BallMovement ();
它的工作之前,我试图用速度可变的,它不会因为这个错误的编译:
无法隐式转换类型'浮动'到'int'.An显式转换存在(是否缺少强制转换?)
解决方案速度必须是浮动。如果你想保持速度为float,你可以创建自己的矩形结构。你可以做这样的事情:
公共结构的RectangleF {浮动W = 0; 浮动H = 0; 浮动X = 0; 浮动Y = 0; 公众持股量身高 {得到{H; } 集合{H =价值; } } //把宽度,X和Y属性在这里 公众的RectangleF(浮点宽度,高度浮球,浮球X,浮Y) {$ b $带宽=宽度; H =高度; X = X; Y = Y; } 公共BOOL相交(矩形refRectangle) {矩形REC =新的Rectangle((INT)X,(INT)Y(INT)W, (INT)H); 如果(rec.Intersects(refRectangle))返回true; ,否则返回false; } }
交集检查将不会是绝对完美的,但至少你的矩形的X和Y可以有0.5添加到他们。 HTH
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?)
解决方案
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
更多推荐
不能隐式转换类型在XNA
发布评论