possible lossy conversion from double to float [duplicate]

我们两清 提交于 2020-03-04 05:29:54

问题


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);
}

回答1:


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));


来源:https://stackoverflow.com/questions/35135442/possible-lossy-conversion-from-double-to-float

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!