Java 如何将纹理附着到夹具LibGDX/Box2D
如何将纹理附着到夹具LibGDX/Box2D 我有一个身体有两个装置,目前我有一个纹理附加到一个装置,我如何得到另一个纹理附加到另一个装置。目前,我在一个单独的类中有另一个纹理,但我无法找到一种方法将纹理附加到第二个装置 这是用于创建主体、两个装置和纹理的类:Java 如何将纹理附着到夹具LibGDX/Box2D,java,libgdx,textures,box2d,Java,Libgdx,Textures,Box2d,如何将纹理附着到夹具LibGDX/Box2D 我有一个身体有两个装置,目前我有一个纹理附加到一个装置,我如何得到另一个纹理附加到另一个装置。目前,我在一个单独的类中有另一个纹理,但我无法找到一种方法将纹理附加到第二个装置 这是用于创建主体、两个装置和纹理的类: public isaac(World world, playScreen screen){ super(screen.getAtlas().findRegion("isaac_body")); this.world = w
public isaac(World world, playScreen screen){
super(screen.getAtlas().findRegion("isaac_body"));
this.world = world;
currentState = State.STANDING;
previousState = State.STANDING;
stateTimer = 0;
walkingRight = true;
walkingDown = true;
Array<TextureRegion> frames = new Array<TextureRegion>();
for(int i = 6; i < 8; i++)
frames.add(new TextureRegion(getTexture(), i*32, 0, 32, 32));
for(int i = 0; i < 6; i++)
frames.add(new TextureRegion(getTexture(), i*32, 32, 32, 32));
isaacWalkY = new Animation<TextureRegion>(0.1f,frames);
frames.clear();
for(int i = 0; i < 8; i++)
frames.add(new TextureRegion(getTexture(), i * 32, 64, 32, 32));
for(int i = 0; i < 2; i++)
frames.add(new TextureRegion(getTexture(), i * 32, 96, 32, 32));
isaacWalkX = new Animation<TextureRegion>(0.1f, frames);
isaacStand = new TextureRegion(getTexture(), 0,32, 32, 32);
isaacDefault = new TextureRegion(getTexture(), 0, 0, 32, 32);
defineIsaac();
setBounds(0,0,28 / icsGame.PPM, 28 / icsGame.PPM);
setRegion(isaacStand);
}
public void update(float dt){
setPosition(b2Body.getPosition().x - getWidth() / 2, b2Body.getPosition().y - getHeight() / 2);
setRegion(getFrame(dt));
}
public TextureRegion getFrame(float dt){
currentState = getState();
TextureRegion region;
switch(currentState){
case WALKINGY:
region = isaacWalkY.getKeyFrame(stateTimer, true);
break;
case WALKINGX:
region = isaacWalkX.getKeyFrame(stateTimer, true);
break;
case STANDING:
default:
region = isaacStand;
break;
}
if((b2Body.getLinearVelocity().x < 0 || !walkingRight) && !region.isFlipX()){
region.flip(true, false);
walkingRight = false;
}
else if((b2Body.getLinearVelocity().x > 0 || walkingRight) && region.isFlipX()){
region.flip(true, false);
walkingRight = true;
}
if((b2Body.getLinearVelocity().y > 0) && !region.isFlipY()){
region.flip(false, true);
walkingDown = false;
}
else if((b2Body.getLinearVelocity().y < 0) && region.isFlipY()){
region.flip(false, true);
walkingDown = true;
}
stateTimer = currentState == previousState ? stateTimer + dt : 0;
previousState = currentState;
return region;
}
public State getState(){
if(b2Body.getLinearVelocity().y > 0 || b2Body.getLinearVelocity().y < 0){
return State.WALKINGY;
}
else if((b2Body.getLinearVelocity().x > 0 || b2Body.getLinearVelocity().x < 0)){
return State.WALKINGX;
}
else{
return State.STANDING;
}
}
public void defineIsaac(){
BodyDef bdef = new BodyDef();
bdef.position.set(64 / icsGame.PPM, 64 / icsGame.PPM);
bdef.type = BodyDef.BodyType.DynamicBody;
b2Body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(4 / icsGame.PPM);
fdef.shape = shape;
b2Body.createFixture(fdef).setUserData("body");
FixtureDef fdef2 = new FixtureDef();
shape2 = new CircleShape();
shape2.setRadius(10 / icsGame.PPM);
Vector2 vec = new Vector2(shape.getPosition().x / 2, shape2.getRadius());
shape2.setPosition(vec);
fdef2.shape = shape2;
b2Body.createFixture(fdef2);
}
}
public isaac(世界世界,游戏屏幕){
super(screen.getAtlas().findRegion(“isaac_body”);
这个世界=世界;
currentState=State.STANDING;
previousState=State.STANDING;
stateTimer=0;
walkingRight=正确;
向下走=真;
数组帧=新数组();
对于(int i=6;i<8;i++)
添加(新的TextureRegion(getTexture(),i*32,0,32,32));
对于(int i=0;i<6;i++)
添加(新的TextureRegion(getTexture(),i*32,32,32,32));
isaacWalkY=新动画(0.1f,帧);
frames.clear();
对于(int i=0;i<8;i++)
添加(新的TextureRegion(getTexture(),i*32,64,32,32));
对于(int i=0;i<2;i++)
添加(新的TextureRegion(getTexture(),i*32,96,32,32));
isaacWalkX=新动画(0.1f,帧);
isaacStand=newTextureRegion(getTexture(),0,32,32,32);
isaacDefault=newTextureRegion(getTexture(),0,0,32,32);
定义aac();
挫折(0,0,28/icsGame.PPM,28/icsGame.PPM);
setRegion(isaacStand);
}
公共无效更新(浮动dt){
setPosition(b2Body.getPosition().x-getWidth()/2,b2Body.getPosition().y-getHeight()/2);
setRegion(getFrame(dt));
}
公共纹理区域getFrame(浮点dt){
currentState=getState();
结构区;
开关(当前状态){
案例漫游:
region=isaacWalkY.getKeyFrame(stateTimer,true);
打破
案例演练X:
region=isaacWalkX.getKeyFrame(stateTimer,true);
打破
案情:
违约:
地区=艾萨克斯站;
打破
}
if((b2Body.getLinearVelocity().x<0 | | |!walkingRight)和&!region.isFlipX()){
区域。翻转(真、假);
walkingRight=错误;
}
else if((b2Body.getLinearVelocity().x>0 | |向右行走)和®ion.isFlipX()){
区域。翻转(真、假);
walkingRight=正确;
}
如果((b2Body.getLinearVelocity().y>0)和&!region.isFlipY()){
区域。翻转(假、真);
向下行走=错误;
}
else if((b2Body.getLinearVelocity().y<0)和®ion.isFlipY()){
区域。翻转(假、真);
向下走=真;
}
stateTimer=currentState==previousState?stateTimer+dt:0;
previousState=当前状态;
返回区;
}
公共状态getState(){
if(b2Body.getLinearVelocity().y>0 | | b2Body.getLinearVelocity().y<0){
返回状态:WALKINGY;
}
else if((b2Body.getLinearVelocity().x>0 | | b2Body.getLinearVelocity().x<0)){
返回State.WALKINGX;
}
否则{
返回状态。状态;
}
}
公共无效定义SAAC(){
BodyDef bdef=新的BodyDef();
bdef.position.set(64/icsGame.PPM,64/icsGame.PPM);
bdef.type=BodyDef.BodyType.DynamicBody;
b2Body=world.createBody(bdef);
FixtureDef fdef=新的FixtureDef();
圆形形状=新的圆形形状();
形状设定半径(4/PPM);
fdef.shape=形状;
b2Body.createFixture(fdef).setUserData(“body”);
FixtureDef fdef2=新的FixtureDef();
shape2=新的圆形();
形状2.设定半径(10/PPM);
vector2vec=newvector2(shape.getPosition().x/2,shape2.getRadius());
形状2.设置位置(vec);
fdef2.shape=shape2;
b2Body.createFixture(fdef2);
}
}
这是创建头部的类:
public class isaacHead extends Sprite {
public enum State {EASTWEST, NORTH, SOUTH, DEFAULT}
public State currentState;
public State previousState;
public World world;
public Body b2Head;
private TextureRegion isaacDefault;
private Animation<TextureRegion> eastWest;
private Animation<TextureRegion> north;
private Animation<TextureRegion> south;
private float stateTimer;
private boolean shootingRight;
private isaac player;
public isaacHead(World world, playScreen screen) {
super(screen.getAtlas().findRegion("isaac_body"));
this.world = world;
currentState = State.DEFAULT;
previousState = State.DEFAULT;
stateTimer = 0;
Array<TextureRegion> frames = new Array<TextureRegion>();
for (int i = 0; i < 2; i++)
frames.add(new TextureRegion(getTexture(), i * 32, 0, 32, 32));
south = new Animation<TextureRegion>(0.15f, frames);
frames.clear();
for (int i = 2; i < 4; i++)
frames.add(new TextureRegion(getTexture(), i * 32, 0, 32, 32));
eastWest = new Animation<TextureRegion>(0.15f, frames);
frames.clear();
for (int i = 4; i < 6; i++)
frames.add(new TextureRegion(getTexture(), i * 32, 0, 32, 32));
north = new Animation<TextureRegion>(0.15f, frames);
isaacDefault = new TextureRegion(getTexture(), 0, 0, 32, 32);
setBounds(0, 0, 32 / icsGame.PPM, 32 / icsGame.PPM);
setRegion(isaacDefault);
}
public void update(float dt) {
setRegion(getFrame(dt));
}
public TextureRegion getFrame(float dt) {
currentState = getState();
TextureRegion region;
switch (currentState) {
case SOUTH:
region = south.getKeyFrame(stateTimer, true);
break;
case EASTWEST:
region = eastWest.getKeyFrame(stateTimer, true);
break;
case NORTH:
region = north.getKeyFrame(stateTimer, true);
break;
case DEFAULT:
default:
region = isaacDefault;
break;
}
if (((Gdx.input.isKeyPressed(Input.Keys.LEFT) || !shootingRight) && !region.isFlipX())) {
region.flip(true, false);
shootingRight = false;
} else if (((Gdx.input.isKeyPressed(Input.Keys.RIGHT) || shootingRight) && region.isFlipX())) {
region.flip(true, false);
shootingRight = true;
}
stateTimer = currentState == previousState ? stateTimer + dt : 0;
previousState = currentState;
return region;
}
public State getState() {
if (Gdx.input.isKeyPressed(Input.Keys.UP)) {
return State.NORTH;
} else if ((Gdx.input.isKeyPressed(Input.Keys.LEFT)) || (Gdx.input.isKeyPressed(Input.Keys.RIGHT))) {
return State.EASTWEST;
} else if (Gdx.input.isKeyPressed(Input.Keys.DOWN)) {
return State.SOUTH;
} else {
return State.DEFAULT;
}
}
}
public类isaacHead扩展了Sprite{
公共枚举状态{东西、北、南、默认值}
公共国家;
公共国家优先国家;
公共世界;
公共机构负责人;
私有结构区域isaacDefault;
私人动画东西方;
私人动画北;
私人动画南方;
专用浮点状态计时器;
私人布尔射击权;
私人艾萨克球员;
公共iSachead(世界世界,游戏屏幕){
super(screen.getAtlas().findRegion(“isaac_body”);
这个世界=世界;
currentState=State.DEFAULT;
previousState=State.DEFAULT;
stateTimer=0;
数组帧=新数组();
对于(int i=0;i<2;i++)
添加(新的TextureRegion(getTexture(),i*32,0,32,32));
南部=新动画(0.15f,帧);
frames.clear();
对于(int i=2;i<4;i++)
添加(新的TextureRegion(getTexture(),i*32,0,32,32));
东西方=新动画(0.15f,帧);
frames.clear();
对于(int i=4;i<6;i++)
添加(新的TextureRegion(getTexture(),i*32,0,32,32));
北=新动画(0.15f,帧);
isaacDefault=newTextureRegion(getTexture(),0,0,32,32);
挫折(0,0,32/icsGame.PPM,32/icsGame.PPM);
setRegion(isaacDefault);
}
公共无效更新(浮动dt){
setRegion(getFrame(dt));
}
公共纹理区域getFrame(浮点dt){
currentState=getState();
结构区;
开关(当前状态){
南方案例:
region=south.getKeyFrame(stateTimer,true);
打破
案例东西方:
region=eastWest.getKeyFrame(stateTimer,true);
打破
案例北:
region=north.getKeyFrame(stateTimer,true);
打破
案例默认值:
违约:
区域=默认值;
打破
}
如果((Gdx.input.isKeyPressed(input.Keys.LEFT)| | |!shootingRight)和&!region.isFlipX()){
区域。翻转(真、假);
shootingRight=false;
}如果((Gdx.input.isKeyPressed(input.Keys.RIGHT)| | shootingRight)和®ion.isFlipX()
public class playScreen implements Screen {
private icsGame game;
private TextureAtlas atlas;
private OrthographicCamera gameCam;
private Viewport gamePort;
private Scenes.hud hud;
private TmxMapLoader mapLoader;
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
private World world;
private Box2DDebugRenderer b2dr;
private isaac player;
private isaacHead head;
public playScreen(icsGame game) {
atlas = new TextureAtlas("isaac.pack");
this.game = game;
gameCam = new OrthographicCamera();
gamePort = new FitViewport(icsGame.V_WIDTH / icsGame.PPM,icsGame.V_HEIGHT / icsGame.PPM,gameCam);
hud = new hud(game.batch);
mapLoader = new TmxMapLoader();
map = mapLoader.load("Level1.tmx");
renderer = new OrthogonalTiledMapRenderer(map, 1 / icsGame.PPM);
gameCam.position.set(gamePort.getWorldWidth() / 2, gamePort.getWorldHeight() / 2, 0);
world = new World(new Vector2(0,0), true);
b2dr = new Box2DDebugRenderer();
new b2WorldCreator(world, map);
player = new isaac(world, this);
head = new isaacHead(world, this);
world.setContactListener(new worldContactListener());
}
public TextureAtlas getAtlas(){
return atlas;
}
@Override
public void show() {
}
public void handleInput(float dt){
if (Gdx.input.isKeyPressed(Input.Keys.A) && player.b2Body.getLinearVelocity().x >= -2) {
player.b2Body.setLinearVelocity(-1.5f,0f);
} else if (Gdx.input.isKeyPressed(Input.Keys.D) && player.b2Body.getLinearVelocity().x <= 2) {
player.b2Body.setLinearVelocity(1.5f,0f);
} else if (Gdx.input.isKeyPressed(Input.Keys.A) == Gdx.input.isKeyPressed(Input.Keys.D)) {
player.b2Body.setLinearVelocity(0, 0);
}
if (Gdx.input.isKeyPressed(Input.Keys.W) && player.b2Body.getLinearVelocity().y <= 2)
player.b2Body.setLinearVelocity(0,1.5f);
if (Gdx.input.isKeyPressed(Input.Keys.S) && player.b2Body.getLinearVelocity().y >= -2)
player.b2Body.setLinearVelocity(0,-1.5f);
if ((Gdx.input.isKeyPressed(Input.Keys.A)) && (Gdx.input.isKeyPressed(Input.Keys.S)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(-1.5f, -1.5f);
}
if ((Gdx.input.isKeyPressed(Input.Keys.D)) && (Gdx.input.isKeyPressed(Input.Keys.S)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(1.5f, -1.5f);
}
if ((Gdx.input.isKeyPressed(Input.Keys.A)) && (Gdx.input.isKeyPressed(Input.Keys.W)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(-1.5f, 1.5f);
}
if ((Gdx.input.isKeyPressed(Input.Keys.D)) && (Gdx.input.isKeyPressed(Input.Keys.W)) && (player.b2Body.getLinearVelocity().x <= 2) && (player.b2Body.getLinearVelocity().y <= 2)) {
player.b2Body.setLinearVelocity(1.5f, 1.5f);
}
}
public void update(float dt){
handleInput(dt);
world.step(1/60f,6,2);
head.setPosition(player.shape2.getPosition().x+1, player.shape2.getPosition().y+1);
player.update(dt);
head.update(dt);
gameCam.update();
renderer.setView(gameCam);
}
@Override
public void render(float delta) {
update(delta);
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
renderer.render();
b2dr.render(world, gameCam.combined);
game.batch.setProjectionMatrix(gameCam.combined);
game.batch.begin();
player.draw(game.batch);
head.draw(game.batch);
game.batch.end();
game.batch.setProjectionMatrix(hud.stage.getCamera().combined);
hud.stage.draw();
}