gpt4 book ai didi

java - 您的 Sprite 或游戏对象与 Box2D 之间的链接

转载 作者:行者123 更新时间:2023-11-30 07:36:26 25 4
gpt4 key购买 nike

我有一个问题:在 libgdx 有关 box2d 的文档 ( libgdx box2d ) 在 Sprites and Bodies 部分,有解释如何设置 Sprite 或游戏对象与 Box2D 之间的链接,但是当我试图获取 setPosition 方法,我无法获取它(我的实体对象 e 没有像 setPosition 这样的方法)。

我不明白我应该在这里使用哪种类型的实体(是 libgdx 实体还是 Java 实体类)?

代码:

In show() method

private Texture ballTexture = new Texture("images/ball.png");  
private Image ballImage = new Image(ballTexture);
body.setUserData(ballImage);
stage.add(ballImage);

In render() method

// Create an array to be filled with the bodies
// (better don't create a new one every time though)
Array<Body> bodies = new Array<Body>();
// Now fill the array with all bodies
world.getBodies(bodies);

for (Body b : bodies) {
// Get the body's user data - in this example, our user
// data is an instance of the Entity class
Entity e = (Entity) b.getUserData();

if (e != null) {
// Update the entities/sprites position and angle
e.setPosition(b.getPosition().x, b.getPosition().y);
// We need to convert our angle from radians to degrees
e.setRotation(MathUtils.radiansToDegrees * b.getAngle());
}
}
stage.act();
stage.render();
<小时/>

我停在e.setPosition(b.getPosition().x, b.getPosition().y);行因为我无法获取setPosition() e中的方法目的。可能出了什么问题?

最佳答案

在下面的代码中:

private Image ballImage = new Image(ballTexture);
body.setUserData(ballImage);

您将 Image 类型的对象设置为用户数据。

这意味着您应该强制转换 b.getUserData();进入图像,而不是实体:

Image e = (Image) b.getUserData();

Image继承自Actor的setPosition和setRotation方法。

关于java - 您的 Sprite 或游戏对象与 Box2D 之间的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35330611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com