This site requires JavaScript, please enable it in your browser!
Greenfoot back
Wasupmacuz
Wasupmacuz wrote ...

2017/3/11

Doubles and Integers

Wasupmacuz Wasupmacuz

2017/3/11

#
I'm trying to make a 2D game that moves actors around to look 3D but when I try and compile to test bugs I get this error: "possible lossy conversion converting double to int" or something like that. I just don't know how to get around this. Code (I put "//error HERE" next to where I got the errors):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * A tree in the world.
 *
 * @author (Wasupmacuz)
 * @version ()
 */
public class Tree extends ThreeD
{
    private double worldX;
    private double worldY;
    private double worldZ;
    private double distance;
    private double distanceX;
    private double distanceY;
    private double perspective;
    private int screenX;
    private int screenY;
    private int actorsShouldBeDistance=100;
    public Tree()
    {
        actorsShouldBeDistance=100;
        worldX=Greenfoot.getRandomNumber(800)-400;
        worldY=Greenfoot.getRandomNumber(800)-400;
        worldZ=Greenfoot.getRandomNumber(800)-400;
    }
     
    /**
     * make sure the tree in front of the player when needed
     */
    public void act()
    {
        if(getY()>215)
        {
            getWorld().setPaintOrder(Crosshair.class,AmmoDisplay.class,Tree.class);
        }
        else
        {
            getWorld().setPaintOrder(AmmoDisplay.class,Player.class);
        }
    }   
     
    /**
     * makes things look 3D-ish
     */
    public void cameraUpdate()
    {
        distanceX=worldX-cameraX;
        distanceY=worldY-cameraY;
        distance=(cameraYX*distanceX)+(cameraYY*distanceY);
        if(distance<0.1)
        {
            getImage().setTransparency(0);
        }
        else
        {
            perspective=actorsShouldBeDistance/distance;
            screenX=((cameraXX*distanceX)+(cameraXY+distanceY))*(perspective/cameraClipping); //error HERE
            if(screenX<600)
            {
                if(screenX>0)
                {
                    screenY=worldZ*perspective; //error HERE
                    if(screenY<400)
                    {
                        if(screenY>0)
                        {
                            setLocation(screenX,screenY);
                            setSize(100*perspective); //error HERE
                            getImage().setTransparency(255);
                        }
                    }
                }
            }
        }
    }
     
    /**
     * Change the size of the image based on a percentage
     */
    public void setSize(int percent)
    {
        GreenfootImage image=new GreenfootImage("tree.png");
        setImage(image);
        getImage().scale(getImage().getWidth()*percent/100, getImage().getHeight()*percent/100);
    }
}
I'd love some help from anyone who knows how to fix this issue :)
Impoleon117 Impoleon117

2017/3/12

#
Integer can only hold whole numbers. So you're trying to put something like 4.3534 into an int-type variable which can only hold whole numbers like 1,2,3,-5,-6, etc The solution to this is, that you have to do "parsing", which is for example "int i = (int) 4.3534;". So i will be 4 after this. Look up "java parsing" on the internet, it's not difficult to get :) have fun
davmac davmac

2017/3/12

#
The solution to this is, that you have to do "parsing", which is for example "int i = (int) 4.3534;".
That's nothing to do with parsing; perhaps you meant casting.
_ben_ _ben_

2017/3/12

#
Your problem is that you write double vales to int. > Theory: an int is a 32bit value, thus it can hold 2^32 different values, a double is double an in thus 2^64 values. Thus, if you have a huge double int might be too small and you have "lossy conversion" > Fix: typcast the double to int. Typecasting works by butting the desired type in brackets before the expression. Here: screen = (int)(worldZ*perspective); this works since it is all just memory. Attention: you might still loose data during the conversion, but there won't be an error. An alternative way would be to check if the product of the doubles is too big and do some error handling or you make everything double or int. Note that most functions in greenfoot take int as input and not double. Hope this was helpful :)
Wasupmacuz Wasupmacuz

2017/3/12

#
_ben_ wrote...
Your problem is that you write double vales to int. > Theory: an int is a 32bit value, thus it can hold 2^32 different values, a double is double an in thus 2^64 values. Thus, if you have a huge double int might be too small and you have "lossy conversion" > Fix: typcast the double to int. Typecasting works by butting the desired type in brackets before the expression. Here: screen = (int)(worldZ*perspective); this works since it is all just memory. Attention: you might still loose data during the conversion, but there won't be an error. An alternative way would be to check if the product of the doubles is too big and do some error handling or you make everything double or int. Note that most functions in greenfoot take int as input and not double. Hope this was helpful :)
Thank you! No more errors!
You need to login to post a reply.