创建尺寸后如何更改尺寸 [英] How to change size after it has been created
问题描述
我正在使用java,libgdx和box2d
I am using java, libgdx, and box2d
在主班我创建了一个球员.我想将player.class中的shape.setAsBox更改为100.所以换句话说,我想在创建shape.setAsBox之后对其进行更改.我相信唯一的方法就是删除固定装置,然后重新创建一个尺寸为100的新装置.我怎样才能做到这一点.
In main class I have created a player. I want to change shape.setAsBox to 100 in player class. So in other words I want to change shape.setAsBox after it has been created. I believe only way to do this is to delete fixture and recreate a new one with 100 size. How can I do this.
public class main{
...
public main(){
//create player
BodyDef bdef = new BodyDef();
Body body;
FixtureDef fdef = new FixtureDef();
PolygonShape shape = new PolygonShape();
/***Body - Player ***/
bdef.type = BodyType.DynamicBody;
bdef.position.set(50 / PPM, 50 / PPM);
bdef.linearVelocity.set(1.5f, 0);
body = world.createBody(bdef);
/*** 1st fixture ***/
shape.setAsBox(50/ PPM, 50 / PPM);
fdef.shape = shape;
fdef.filter.categoryBits = Constants.BIT_PLAYER;
fdef.filter.maskBits = Constants.BIT_GROUND;
body.createFixture(fdef).setUserData("player");
player = new Player(body);
}
....
public void update(float dt) {
playerObj.update(dt);
...
}
}
//玩家班级
public class player{
public player(Body body){
super(body);
}
....
public void update(){
//get player x position
currentX = this.getBody().getPosition().x;
// how can I delete old fixture and recreate a new one?
// which will has shape.setAsBox = 100.
}
}
推荐答案
销毁Fixture
并重新定义它.由于您的播放器有一个Fixture
,请跟踪它以将其删除或致电:
Destroy the Fixture
and redefine it. Since your player has one Fixture
, either keep track of it to remove it or call:
this.getBody().destroyFixture(this.getBody().getFixtureList().first());
然后在已经存在的Body中重新创建一个简单的形状:
Then recreate a simple shape in the already existing Body:
PolygonShape shape;
FixtureDef fdef;
// Create box shape
shape = new PolygonShape();
shape.setAsBox(100 / PPM, 100 / PPM);
// Create FixtureDef for player collision box
fdef = new FixtureDef();
fdef.shape = shape;
fdef.filter.categoryBits = Constants.BIT_PLAYER;
fdef.filter.maskBits = Constants.BIT_GROUND;
// Create player collision box fixture
this.getBody().createFixture(fdef).setUserData("player");
shape.dispose();
这篇关于创建尺寸后如何更改尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!