简体   繁体   English

将精灵附加到Box2d主体以进行移动

[英]Attaching a sprite to a Box2d body for movement

Im new on Box2d got a problem and couldnt solve it. 我在Box2d上的新手遇到了问题,无法解决。 I want to move my player left and right when the user touch my left and right buttons. 当用户触摸我的左右按钮时,我想左右移动播放器。 I created a fixture I can move body and fixture but not the player sprite How can I attach my player sprite to my body ? 我创建了一个固定装置,可以移动身体和固定装置,但不能移动播放器精灵,如何将播放器精灵附加到我的身体上? and How should I control body because I cant stop it. 我应该如何控制身体,因为我无法阻止它。 I want to find a proper way of controlling player in box2d. 我想在box2d中找到一种控制播放器的正确方法。 I couldnt use setLinerVelocity etc. 我无法使用setLinerVelocity等。

this is my codes 这是我的密码

public World world;
public Body bplayer;
public Box2DDebugRenderer b2dr;
public Matrix4 cameraBox2D;

PlayScreen 播放屏幕

buttonimage.addListener(new ClickListener() {
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
    {

    bplayer.setLinearVelocity(-5*PPM , 0);



    return true;
    }
});
world = new World(new Vector2(player.getPosition().x , player.getPosition().y) , false);
b2dr = new Box2DDebugRenderer();
bplayer = createPlayer(player.getPosition().x , player.getPosition().y);

show method 显示方法

buttonimage.setPosition(160,0);
rightbuttonimage.setPosition(320,0);
pauseimage.setPosition(220,-20);
cameraBox2D = camera.combined.cpy();

Render method 渲染方法

Gdx.gl.glClearColor(0, 0, 2f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


sb.setProjectionMatrix(camera.combined);

player.position.y += 500 * Gdx.graphics.getDeltaTime();
sb.begin();
sb.draw(bg, 0, camera.position.y - (camera.viewportHeight/2));
sb.draw(player.sprite, player.getPosition().x , player.getPosition().y);
for (Tube tube : tubes) {
    sb.draw(tube.getlefttube(), tube.getposlefttube().x, tube.getposlefttube().y);
    sb.draw(tube.getrighttube(), tube.getposrighttube().x, tube.getposrighttube().y);
    sb.draw(tube.getLight() , tube.getPoslight().x , tube.getPoslight().y);
}





delta*=speed;
sb.end();

update(delta);
b2dr.render(world , cameraBox2D);
stage.draw();

app.batch.begin();
app.font23.draw(app.batch,"Lights collected :" + dropsGathered , 0, 720);
app.batch.end();

cameraUpdate method cameraUpdate方法

Vector3 position = camera.position;
position.x = player.position.x;
position.y = player.position.y;
camera.position.set(position);

createPlayer method createPlayer方法

Body pBody;
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.DynamicBody;
def.position.set(x * PPM, y * PPM  );
def.fixedRotation = true;
pBody = world.createBody(def);

return pBody;

update method 更新方法

world.step(1 / 60f , 6 , 2);

for(int i = 0; i < tubes.size; i++) {

    Tube tube = tubes.get(i);

    if (camera.position.y - (camera.viewportWidth/2) > tube.getposlefttube().y + tube.getlefttube().getWidth()) {
        tube.reposition(tube.getposlefttube().y + ( TUBE_COUNT) );

    }

    if (tube.collides(player.getBounds())){
        app.setScreen(new GameOver(app));


    }

    if (tube.gathered(player.getBounds())){

            dropsGathered++;




    }

    if (dropsGathered >= 50){
        //app.setScreen(new Stage2(app));
    }


}
camera.update();

handleInput();
camera.position.y = player.getPosition().y + 300;
player.update(delta);
camera.update();

cameraUpdate(delta);

stage.act(delta);

Do not use the Sprite class. 不要使用Sprite类。 Use the TextureRegion class instead. 请改用TextureRegion类。 Sprite is confusingly subclassed from TextureRegion, so when you call batch.draw(sprite, ...) its position and rotation parameters are ignored because it is being treated as a TextureRegion. Sprite是TextureRegion的令人困惑的子类,因此当您调用batch.draw(sprite, ...)其位置和旋转参数将被忽略,因为它被视为TextureRegion。

You could use a Sprite by calling sprite.draw(batch) but a Sprite is redundant because your Body already has position and rotation parameters. 您可以通过调用sprite.draw(batch)使用Sprite,但是Sprite是多余的,因为您的Body已经具有位置和旋转参数。

Use a TextureRegion directly with the SpriteBatch. 直接在SpriteBatch中使用TextureRegion。 You can orient it with rotation parameters passed into the draw method. 您可以使用传递到draw方法中的旋转参数来使其定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM