Question

So my problem is that I am trying to position a sprite in the middle of the screen. How to do that is very simple but the calculations made during runtime are wrong. I have come to the conclusion that it is about floating points being messy in Java.

So, this is the code I have right now, which works:

float posX = ((1280f / 2f) - 128f);
float posY = ((720f / 2f) - 112.5f);
splash.setScale(1.5f);
splash.setPosition((posX), (posY));

But if I replace the divisions above with 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 does not seem to work anymore. It seems to me that the first part of the posX and posY calculations result in a 0.0 and my sprite ends up in the upper left corner, half-visible.

Why is it that if I replace the different calculations with these variables, it stops working?

EDIT:

So to clarify:

This is how I want the code to look 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 something go wrong. Even though, to me, they look exactly the same as the other calculation. Am I making a rookie mistake somewhere? I have been constructing this calculation from the bottom to the top with just numbers, and it works, but as soon as I substitute one of the calculations with a variable (saving it in a variable) it stops working, and the sprite goes off the screen, as if the screenCenterW and screenCenterH is calculated to 0.0 through the division of 2f. Example of screenWidthH: (1280f / 2f) = 0 | 0 - 112.5f = -112.5f

Why I have issues debuging this is because I don't know how to show these calculations on the screen in the AndEngine for Android that I am using. I sincerely apologise for not being detailed and I realise now that the question didn't say much at the start, but I hope this clarifies what I'm asking about. And if somebody out there that use AndEngine could help me that would be amazing.

Was it helpful?

Solution

You show too little code to tell you where you went wrong, but I suspect you either alter the variable at some place without noting, or you have a class initialization order issue (you are using the variables before they are actually calculated).

To rule out accidental alteration, declare the variables as final:

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

If there's any place where you assign them, the compiler will complaing about it.

Initialization order is harder to track down, you can use a conditional check just before using the variable:

 if (screenCenterW == 0) {
     throw new RuntimeException("Something is horribly wrong!");
 }

You should see the exception in your IDE's console window if it triggers.

OTHER TIPS

Nothing is wrong with the calculations. The way you're doing it is right, and you can test it:

public static void main(String[] args) {
        float screenCenterW = (1280.0f / 2f);
        float screenCenterH = (720.0f / 2f);
        float splashCenterW = (225.0f / 2f);
        float splashCenterH = (256.0f / 2f);

        System.out.println(screenCenterW);
        System.out.println(screenCenterH);
        System.out.println(splashCenterW);
        System.out.println(splashCenterH);
    }

Output:

640.0
360.0
112.5
128.0

Show the rest of the code.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top