gpt4 book ai didi

java - Libgdx,我可以在 Body 中附加动画吗?

转载 作者:行者123 更新时间:2023-12-01 09:06:28 25 4
gpt4 key购买 nike

我对 Android 游戏应用程序开发很感兴趣,我正在自己学习如何做到这一点,使用 Libgdx 作为我的游戏引擎,并且已经制作了一个不令人兴奋的小游戏。

我最近开始学习如何使用 World 变量并在其中创建主体,我想创建一个扩展 Actor 的类,为其提供动画(我知道如何提供动画)并尝试将其显示在body,所以无论body走到哪里,动画都会在上面播放(基本上动画下面就是不显示的body)

所以我的问题是:有没有办法让这个服装 Actor 进入 body ,这样它就能在 body 上表演?(附加它们)

非常感谢您的帮助!

** 注意:请注意,我的知识仍然有限,我是一名程序工程专业的大学生,而且我对 Java 还没有完全深入(我的大学不教授游戏引擎)**

更新

我创建了一个服装类,为每个 Action 制作了动画(例如herowalk、herosit...等等...)

我在其中编写了一个渲染方法:

public void render(SpriteBatch batch) {
float posX = bbody.getPosition().x * PPM;
float posY = bbody.getPosition().y * PPM;

float rotation = (float) Math.toDegrees(bbody.getAngle());

sprite.setPosition(posX,posY);
sprite.setRotation(rotation);

sprite.draw(batch);
}

并创建了一个主体,将其放置在屏幕中间,并使用 SetUserData() 将 Sprite 附加到其上:

BodyDef bdef = new BodyDef();
bdef.position.set(160 / PPM, 200 / PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
PolygonShape shape = new PolygonShape();
shape.setAsBox(5 / PPM, 5 / PPM);
bbody = world.createBody(bdef);
Bodyarray.add(bbody);
FixtureDef fdef = new FixtureDef();
fdef.shape = shape;
fdef.filter.categoryBits = BIT_BOX;
fdef.filter.maskBits = BIT_PLATFORM | BIT_BALL;
bbody.createFixture(fdef);
bbody.setUserData(sprite);

在我的主课中,我用批处理进行绘制:

player.sprite.setRegion(player.getAnimationup().getKeyFrame(dt, true));
dt += elapsedTime;
player.render(batch);

不断改变动画取决于玩家是否向右、向左、向上、向下转动。

我遇到的唯一问题是 Sprite 本身(动画效果完美)被绘制在屏幕的左下角(不是在 0,0 上,看起来它得到了 body 的 x,y但仍然远离它)并且可以在移动 body 时看到它附着在它上面(我放置控件来控制 body 运动)并且我看到 Sprite 随之移动。

例如,我尝试像往常一样使用 Gdx.app.log 检查主体和 Sprite 的 X、Y 协调性,并且两者具有相同的精确 X 和 Y。(ofc Sprite 将其乘以 PPM(将其设置为 100) 因为 Sprite 不是实体)

是什么导致 Sprite 位置错误?

最佳答案

欢迎来到 Stack Overflow!

您的问题的答案是“不完全是”。 Box2D 为您提供了一个名为 Body#setUserData() 的方法,它允许您创建从 Body 到任何对象的引用(在本例中为 Animation 对象,但它取决于您可以保持它们的位置同步。请参阅下面改编的伪代码from the libGDX wiki:

// 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
Animation anim = (Animation) b.getUserData();

if (anim != null) {
// Update the entities/sprites position and angle
TextureRegion frame = anim.getKeyFrame( ... );
// Now draw the frame using b.getPosition() and b.getAngle()
}
}

顺便说一下,libGDX wiki当您开始学习 libGDX 时,这是一个优秀资源和引用。

关于java - Libgdx,我可以在 Body 中附加动画吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41229466/

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