0

我有我的图像(为了解释起见,我们称之为Image.png),我试图让它缩放以适合我绘制的身体。身体和一切都与调试一起工作,但我不喜欢它。我正在使用 aSpriteBatch将背景图像绘制到屏幕上(也可以缩放它)。如何缩放Image.png到与已渲染的动态主体相同的大小/位置?

编辑: 在绘制图像时,我无法让它们与调试渲染体相匹配。

创建正文:

                BodyDef bodydef2 = new BodyDef();  
                bodydef2.type = BodyType.DynamicBody;  
                bodydef2.position.set(camera.viewportWidth + 30.0f, (int)(Math.random()*camera.viewportHeight)%(LastPlatformY+5)+20);  
                //System.out.println(bodydef2.position.y + " " + LastPlatformY);
                Body block = world.createBody(bodydef2);  
                PolygonShape Shape2 = new PolygonShape();  
                Shape2.setAsBox(750*WORLD_TO_BOX, 200*WORLD_TO_BOX);  
                FixtureDef fixtureDef2 = new FixtureDef();  
                fixtureDef2.shape = Shape2;  
                fixtureDef2.density = 1000.0f; 
                fixtureDef2.friction = 0.0f;  
                fixtureDef2.restitution = 0; 
                block.setLinearVelocity(new Vector2((float) (Platform_Velocity*-1), 0));
                block.createFixture(fixtureDef2); 
                //Lock 'block' to X-Axis, Relative to floor.
                PrismaticJointDef jointDef = new PrismaticJointDef();
                jointDef.collideConnected = true;
                jointDef.initialize(block, groundBody, block.getWorldCenter(), new Vector2(1, 0)); 
                world.createJoint(jointDef);
                Platforms.add(block);
                Platforms_Created++;
                LastPlatformY = (int)bodydef2.position.y;

绘图图像:

sp.draw(platforms, (float)(Platforms.get(i).getPosition().x), (float)(Platforms.get(i).getPosition().y), 75,20);

编辑#2: 事实证明,如果您的相机小于屏幕尺寸,则必须进行一些补偿以解决位置变化。问题已解决,谢谢!

4

1 回答 1

1

只需在创建身体时将身体的大小保存在两个变量中,然后调用适当的绘制方法。要获取对象的位置,请调用 body.getPosition()。

例如

Texture t = assetManager.get("your texture", Texture.class);

BodyDef bodyDef = new BodyDef();
body.type = BodyType.DynamicBody

float width = 64 * WORLD_TO_BOX;
float height = 64 * WORLD_TO_BOX;

Shape shape = new PolygonShape();
float[] points = new float[]{0,0,width,0,width,height,0, height};
shape.set(points);
FixtureDef def = new FixtureDef();
def.shape = shape;
...
body.createFixture(def);
shape.dispose();

以后想画的时候

public void render(Spritebatch batch) {
    batch.draw(t, body.getPosition().x * BOX_TO_WORLD, body.getPosition().y * BOX_TO_WORLD, width * BOX_TO_WORLD, height * BOX_TO_WORLD);
}
于 2013-09-07T15:53:35.480 回答