天天看點

box2d for java_用box2d制作動畫

我用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);

}