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
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
Does the speed needs to be float? If not you could make
int speed;
Or use a explicit cast
if (movingUp) { ballRect.Y -= (int)speed; }// No Error
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.
You're trying to subtract a float value (ex: 1.223488) from an int (ex: 12); You can't do this. Either convert (cast) both values to floats, or convert (cast) both values to ints:
if (movingUp) { ballRect.Y -= (int)speed; }//Error
The error is basically saying "We can't automatically convert this for you (implicit), but you can convert it yourself (explicit)." I'd check out the MSDN article on type casting: http://msdn.microsoft.com/en-us/library/ms173105.aspx