Pregunta

In my Xna game, I am trying to have my playfield scale to the screen that it is running on. To do this, I use proportions to find the percent that the real window is scaled relative to my playfield. To do this, I divide the real width by the virtual width:

float _percent = _realViewport.Width / this._viewport.Width;

I always get 0, though, in the _percent variable. I set up a debug stop point at that line in my code, and analyzed the variables. this._viewport.Width equals 640, and _realViewport.Width equals 1280. So, using my calculator, 640 / 1280 SHOULD equal 0.5, but in my code I always get 0 out, instead of 0.5. Why is it doing this?

¿Fue útil?

Solución

Because integer division truncates (XNA's Viewport type has integer width and height properties), and 640 / 1280 is 0.5, which truncates to zero.

Cast one of your values to a float if you want floating point division:

float _percent = _realViewport.Width / (float)this._viewport.Width;

Otros consejos

try this float _percent = _realViewport.Width * 1.0/ (float)this._viewport.Width;,by adding multiplication by 1.0, the integer will automatically convert to float.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top