Dividing two numbers always equals zero?

前端 未结 2 924
刺人心
刺人心 2021-01-22 04:19

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 rela

相关标签:
2条回答
  • 2021-01-22 04:54

    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.

    0 讨论(0)
  • 2021-01-22 04:56

    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;
    
    0 讨论(0)
提交回复
热议问题