java - Float division being silly when saved in variable - but not when "used directly" -


so problem trying position sprite in middle of screen. how simple calculations made during runtime wrong. have come conclusion floating points being messy in java.

so, code have right now, works:

float posx = ((1280f / 2f) - 128f); float posy = ((720f / 2f) - 112.5f); splash.setscale(1.5f); splash.setposition((posx), (posy)); 

but if replace divisions above these variables:

private float screencenterw = (1280.0f / 2f); private float screencenterh = (720.0f / 2f); private float splashcenterw = (225.0f / 2f); private float splashcenterh = (256.0f / 2f); 

things not seem work anymore. seems me first part of posx , posy calculations result in 0.0 , sprite ends in upper left corner, half-visible.

why if replace different calculations these variables, stops working?

edit:

so clarify:

this how want code like:

private float screencenterw = (1280.0f / 2f); private float screencenterh = (720.0f / 2f); private float splashcenterw = (225.0f / 2f); private float splashcenterh = (256.0f / 2f); float posx = (screencenterw - splashcenterw); float posy = (screencenterh - splashcenterh); splash.setscale(1.5f); splash.setposition((posx), (posy)); 

this, however, makes go wrong. though, me, same other calculation. making rookie mistake somewhere? have been constructing calculation bottom top numbers, , works, substitute 1 of calculations variable (saving in variable) stops working, , sprite goes off screen, if screencenterw , screencenterh calculated 0.0 through division of 2f. example of screenwidthh: (1280f / 2f) = 0 | 0 - 112.5f = -112.5f

why have issues debuging because don't know how show these calculations on screen in andengine android using. sincerely apologise not being detailed , realise question didn't @ start, hope clarifies i'm asking about. , if out there use andengine me amazing.

you show little code tell where went wrong, suspect either alter variable @ place without noting, or have class initialization order issue (you using variables before calculated).

to rule out accidental alteration, declare variables final:

 private final float screencenterw = (1280.0f / 2f); 

if there's place assign them, compiler complaing it.

initialization order harder track down, can use conditional check before using variable:

 if (screencenterw == 0) {      throw new runtimeexception("something horribly wrong!");  } 

you should see exception in ide's console window if triggers.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -