【问题标题】:possible lossy conversion from double to float [duplicate]从双精度到浮点数的可能有损转换[重复]
【发布时间】:2016-02-01 16:28:28
【问题描述】:

我正在尝试分裂,但我不断收到此错误?

分级错误;不兼容的类型;从 double 到 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);
}

【问题讨论】:

  • 如果您调用的方法接受float 但传递给它double,则double 必须转换为float。例如,将(float) 放在参数前面。
  • 我调用的唯一两个方法是 set 和 setAsBox 吗?我如何转换这些?
  • (float)放在每个参数的前面。例如。 setAsBox((float) (bounds.getWidth()/2 /MyGdxGame.PPM), (float) (bounds.getHeight()/2 /MyGdxGame.PPM));

标签: java android libgdx


【解决方案1】:

如果您正在调用接受float 的方法,但您传递给它的是double,则必须将double 显式转换为float。您可以通过在每个参数前面加上(float) 来做到这一点。例如:

shape.setAsBox((float) (bounds.getWidth()/2/MyGdxGame.PPM),
               (float) (bounds.getHeight()/2/MyGdxGame.PPM));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2016-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 2015-06-12
    相关资源
    最近更新 更多