Reputation: 39456
I'm making my way through the beginners tutorials for XNA (C#) and have veered off in my own direction once I learned rendering and positioning, having my own game development experience.
I'm trying to make a property VelocityY
on my class Ship
. I want to be able to increment this value by values that are decimal, ie:
VelocityY += 0.45;
I figured that float
was the type required here, but when I try compile I get this error:
Literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type.
I'm not really sure what the first part means as I haven't made use of double
as far as I know. VelocityY
is declared like this:
public float VelocityY = 0;
I tried using double
and even int
instead but I still can't increment by non-whole numbers. Whole numbers work fine.
Upvotes: 2
Views: 6114
Reputation: 12874
By Default all values you give as 0.45 or .68 are double in C# environment, but here you need to say the compiler that the number you gave was float by adding a suffix F
to it.
variable += 0.45F;
Upvotes: 0
Reputation: 19445
you should change
public float VelocityY = 0;
to
public double VelocityY = 0;
or
VelocityY += 0.45;
to
VelocityY += 0.45F;
Upvotes: 1
Reputation: 1500395
The type of the literal 0.45 is double
. If you want to make it a float, use the suffix f
or F
, like the compiler error says:
VelocityY += 0.45F;
Basically, if you don't specify a suffix for a literal including a decimal point, it's implicitly double
. You can use a suffix to make it explicit:
decimal a = 0.45M;
float b = 0.45F;
double c = 0.45D;
Upvotes: 7