1

i am trying to get to divide and i keep getting this error ?

gradle error; incompatible types; possible lossy conversion from double to float?

protected World world;
protected TiledMap map;
protected TiledMapTile tile;
protected Rectangle bounds;
protected Body body;

public InteractiveTileObject(World world, TiledMap map, Rectangle bounds ){
    this.world = world;
    this.map = map;
    this.bounds =  bounds;

    BodyDef bdef = new BodyDef();
    FixtureDef fdef = new FixtureDef();
    PolygonShape shape = new PolygonShape();

    bdef.type = BodyDef.BodyType.StaticBody;
    bdef.position.set((bounds.getX() + bounds.getWidth()/2)/MyGdxGame.PPM, (bounds.getY() + (bounds.getHeight() / 2)) / MyGdxGame.PPM);

    body = world.createBody(bdef);

    shape.setAsBox(bounds.getWidth()/2 /MyGdxGame.PPM, bounds.getHeight()/2 /MyGdxGame.PPM);
    fdef.shape = shape;
    body.createFixture(fdef);
}
user5849344
  • 25
  • 1
  • 6
  • http://stackoverflow.com/a/10075093/2002257 – jheimbouch Feb 01 '16 at 16:31
  • If you are calling a method that accepts a `float` but passing it a `double`, the `double` must be converted to a `float`. For instance, put `(float)` in front of the argument. – khelwood Feb 01 '16 at 16:31
  • the only two methods i am calling are set and setAsBox ? how do i convert those? – user5849344 Feb 01 '16 at 16:34
  • Put `(float)` in front of each argument. E.g. `setAsBox((float) (bounds.getWidth()/2 /MyGdxGame.PPM), (float) (bounds.getHeight()/2 /MyGdxGame.PPM));` – khelwood Feb 01 '16 at 16:35

1 Answers1

2

If you are calling a method that accepts a float, but you are passing it a double, the double must be explicitly converted to a float. You can do this by putting (float) in front of each argument. For example:

shape.setAsBox((float) (bounds.getWidth()/2/MyGdxGame.PPM),
               (float) (bounds.getHeight()/2/MyGdxGame.PPM));
khelwood
  • 55,782
  • 14
  • 81
  • 108