box2d for java_用box2d制作动画

这篇博客介绍了如何在基于libgdx的Java游戏中集成Box2D物理引擎,特别是如何将一个动画精灵(动态Body)与其他静态元素结合。作者展示了世界渲染类的部分代码,用于加载纹理、创建动画,并在`render()`方法中绘制Monkey、Platform和Banana。
摘要由CSDN通过智能技术生成

我用libgdx制作简单的游戏 . 我想添加box2d物理引擎 . 我有三个精灵,其中一个是动画(动态bodie) . 其余的都是静态的 . 我不知道如何将其与box2d集成 . 有没有解决方案 . 我花了很多时间进行搜索,但一无所获 . 我的世界渲染类:

public WorldRenderer(GameWorld world) {

this.world = world;

this.camera = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);

this.camera.position.set(CAMERA_WIDTH/2f, CAMERA_HEIGHT/2f, 0);

this.camera.update();

spriteBatch = new SpriteBatch();

loadTexture();

}

public void loadTexture() {

TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("gfx/textures.atlas"));

int indexr = 1;

int indexl = 1;

monkeyLeft = atlas.findRegion("left");

monkeyRight = atlas.findRegion("right");

platformTex = atlas.findRegion("platform");

bananaTex = atlas.findRegion("banana");

TextureRegion[] walkRightFrames = new TextureRegion[2];

TextureRegion[] walkLeftFrames = new TextureRegion[2];

for(int i=0;i<2;i++){

walkLeftFrames[i] = atlas.findRegion("left", indexl++);

walkRightFrames[i] = atlas.findRegion("right", indexr++);

}

goLeft = new Animation(RFD, walkLeftFrames);

goRight = new Animation(RFD, walkRightFrames);

}

public void render(){

spriteBatch.begin();

drawPlatform();

drawMonkey();

drawBanana();

spriteBatch.end();

}

public void drawMonkey() {

Monkey monkey = world.getMonkey();

monkeyFrame = monkey.isFacingLeft() ? monkeyRight : monkeyLeft;

if(monkey.getState().equals(Stan.WALK)){

monkeyFrame = monkey.isFacingLeft()? goLeft.getKeyFrame(monkey.getStateTime(), true) : goRight.getKeyFrame(monkey.getStateTime(), true);

}

spriteBatch.draw(monkeyFrame, monkey.getPosition().x*ppuX, monkey.getPosition().y*ppuY, Monkey.SIZE*ppuX, Monkey.SIZE*ppuY);

}

public void drawPlatform() {

for(Platform platform : world.getPlatforms()){

spriteBatch.draw(platformTex, platform.getPosition().x*ppuX, platform.getPosition().y*ppuY, Platform.SIZE*ppuX, Platform.SIZEH*ppuY);

}

}

public void drawBanana() {

Banana banana = world.getBanana();

spriteBatch.draw(bananaTex, banana.getPosition().x*ppuX, banana.getPosition().y*ppuY, Banana.SIZE*ppuX, Banana.SIZE*ppuY);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值