gpt4 book ai didi

java - LibGDX applyLinearImpulse 试图实现重力....困惑

转载 作者:行者123 更新时间:2023-12-02 09:55:33 24 4
gpt4 key购买 nike

街头霸王 2d 克隆 我正在尝试让玩家肯在跳跃后回来。他只是越来越高,但我希望它像重力一样,这样他就会被拉下来。然后我会设置一个他无法通过的地面。

我研究了要在主体上调用的 applyLinearImpulse 方法,该方法是在我不断收到空指针异常之前初始化的,似乎现在它不会崩溃,但 Ken 只是在 Y 轴上进一步绘制。这让我非常困惑。

非常感谢任何建议链接。

肯级

public class Ken extends Player {

private static final int FRAME_COLS = 6, FRAME_ROWS = 1;
private static final int COLUMNS_KICK = 6;
private static final int COLUMNS_LEFT = 8;
private static final int COLUMNS_RIGHT = 8;
private static final int COLUMNS_JUMP = 10;
private static final int COLUMNS_PUNCH = 6;
private static final int COLUMNS_FRONTFLIP = 8;
private static final int COLUMNS_BACKFLIP = 8;
public static final int FRAME_FRONTFLIP = 1;
public static final int FRAME_BACKLIP = 1;

float x, y;
Animation<TextureRegion> walkAnimation;
Animation<TextureRegion> kickAnimation;
Animation<TextureRegion> punchAnimation;
Animation<TextureRegion> leftAnimation;
Animation<TextureRegion> rightAnimation;
Animation<TextureRegion> jumpAnimation;
Animation<TextureRegion> frontFlipAnimation;
Animation<TextureRegion> backFlipAnimation;
Texture walkSheet;
Texture kickSheet;
Texture punchSheet;
Texture leftSheet;
Texture rightSheet;
Texture jumpSheet;
Texture frontFlipSheet;
Texture backFlipSheet;

public Body body;

public World world;
boolean alive = true;

private final static int STARTING_X = 50;
private final static int STARTING_Y = 30;
TextureRegion reg;
float stateTime;

public Ken(GameScreen screen){
this.world = screen.getWorld();
defineKen();
createIdleAnimation();
kickAnimation();
punchAnimation();
lefttAnimation();
righttAnimation();
jumpAnimation();

frontFlipAnimation();

backFlipAnimation();


this.setPosition(STARTING_X, STARTING_Y);
}

public void createIdleAnimation() {
walkSheet = new Texture(Gdx.files.internal("ken/idle.png"));

TextureRegion[][] tmp = TextureRegion.split(walkSheet,
walkSheet.getWidth() / FRAME_COLS,
walkSheet.getHeight() / FRAME_ROWS);

TextureRegion[] walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}

walkAnimation = new Animation<TextureRegion>(0.1f, walkFrames);
stateTime = 0f;
reg=walkAnimation.getKeyFrame(0);
}


public void kickAnimation(){

kickSheet = new Texture(Gdx.files.internal("ken/kick_low.png"));

TextureRegion [][] tmp = TextureRegion.split(kickSheet, kickSheet.getWidth() / COLUMNS_KICK,
kickSheet.getHeight() / FRAME_ROWS);

TextureRegion[] kickFrames = new TextureRegion[COLUMNS_KICK * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
kickFrames[index++] = tmp[i][j];
}
}

kickAnimation = new Animation<TextureRegion>(8f, kickFrames);
stateTime = 6f;
reg = kickAnimation.getKeyFrame(1);

}

public void lefttAnimation(){

leftSheet = new Texture(Gdx.files.internal("ken/parry_b.png"));

TextureRegion [][] tmp = TextureRegion.split(leftSheet, leftSheet.getWidth() / COLUMNS_LEFT,
leftSheet.getHeight() / FRAME_ROWS);

TextureRegion[] leftFrames = new TextureRegion[COLUMNS_LEFT * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < COLUMNS_LEFT; j++) {
leftFrames[index++] = tmp[i][j];
}
}

leftAnimation = new Animation<TextureRegion>(0.1f, leftFrames);
stateTime = 0f;
reg = punchAnimation.getKeyFrame(0);

}

public void righttAnimation(){

rightSheet = new Texture(Gdx.files.internal("ken/parry_f.png"));

TextureRegion [][] tmp = TextureRegion.split(rightSheet, rightSheet.getWidth() / COLUMNS_RIGHT,
rightSheet.getHeight() / FRAME_ROWS);

TextureRegion[] rightFrames = new TextureRegion[COLUMNS_RIGHT * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < COLUMNS_RIGHT; j++) {
rightFrames[index++] = tmp[i][j];
}
}

rightAnimation = new Animation<TextureRegion>(0.1f, rightFrames);
stateTime = 0f;
reg = rightAnimation.getKeyFrame(0);

}


public void punchAnimation(){

punchSheet = new Texture(Gdx.files.internal("ken/punch.png"));

TextureRegion [][] tmp = TextureRegion.split(punchSheet, punchSheet.getWidth() / COLUMNS_PUNCH,
punchSheet.getHeight() / FRAME_ROWS);

TextureRegion[] punchFrames = new TextureRegion[COLUMNS_PUNCH * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < COLUMNS_PUNCH; j++) {
punchFrames[index++] = tmp[i][j];
}
}

punchAnimation = new Animation<TextureRegion>(0.1f, punchFrames);
stateTime = 0f;
reg = punchAnimation.getKeyFrame(0);

}



public void jumpAnimation(){

jumpSheet = new Texture(Gdx.files.internal("ken/jump.png"));

TextureRegion [][] tmp = TextureRegion.split(jumpSheet, jumpSheet.getWidth() / COLUMNS_JUMP,
jumpSheet.getHeight() / FRAME_ROWS);

TextureRegion[] jumpFrames = new TextureRegion[COLUMNS_JUMP * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < COLUMNS_JUMP; j++) {
jumpFrames[index++] = tmp[i][j];
}
}



jumpAnimation = new Animation<TextureRegion>(0.1f, jumpFrames);
stateTime = 0f;


reg = jumpAnimation.getKeyFrame(0);



}



public void frontFlipAnimation(){

frontFlipSheet = new Texture(Gdx.files.internal("ken/front_flip.png"));

TextureRegion [][] tmp = TextureRegion.split(frontFlipSheet, frontFlipSheet.getWidth() / COLUMNS_FRONTFLIP,
frontFlipSheet.getHeight() / FRAME_ROWS);

TextureRegion[] frontFlipFrames = new TextureRegion[COLUMNS_FRONTFLIP * FRAME_FRONTFLIP];
int index = 0;
for (int i = 0; i < FRAME_FRONTFLIP; i++) {
for (int j = 0; j < COLUMNS_FRONTFLIP; j++) {
frontFlipFrames[index++] = tmp[i][j];
}
}

frontFlipAnimation = new Animation<TextureRegion>(0.1f, frontFlipFrames);
stateTime = 0f;
reg = frontFlipAnimation.getKeyFrame(0);

}


public void backFlipAnimation(){

backFlipSheet = new Texture(Gdx.files.internal("ken/back_flip.png"));

TextureRegion [][] tmp = TextureRegion.split(backFlipSheet, backFlipSheet.getWidth() / COLUMNS_BACKFLIP,
backFlipSheet.getHeight() / FRAME_BACKLIP);

TextureRegion[] backFlipFrames = new TextureRegion[COLUMNS_BACKFLIP * FRAME_BACKLIP];
int index = 0;
for (int i = 0; i < FRAME_BACKLIP; i++) {
for (int j = 0; j < COLUMNS_BACKFLIP; j++) {
backFlipFrames[index++] = tmp[i][j];
}
}

backFlipAnimation = new Animation<TextureRegion>(0.1f, backFlipFrames);
stateTime = 0f;
reg = backFlipAnimation.getKeyFrame(0);

}



@Override
public void act(float delta) {
super.act(delta);
stateTime += delta;





stateTime += delta;
reg = walkAnimation.getKeyFrame(stateTime,true);

if(Gdx.input.isKeyPressed(Input.Keys.A)){
reg = kickAnimation.getKeyFrame(stateTime, false);
this.addAction(Actions.moveTo(getX() +2, getY(), 1 / 10F));

}

if(Gdx.input.isKeyPressed(Input.Keys.S)){
reg = punchAnimation.getKeyFrame(stateTime, false);
}

if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
reg = leftAnimation.getKeyFrame(stateTime, true);
this.addAction(Actions.moveTo(getX() - 10, getY(), 1 / 10f ));
}

if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
reg = rightAnimation.getKeyFrame(stateTime, true);
this.addAction(Actions.moveTo(getX() + 10, getY(), 1 /10f));
}

if(Gdx.input.isKeyPressed(Input.Keys.UP)){
body.applyLinearImpulse(new Vector2(0, 20), body.getWorldCenter(), true);
reg = jumpAnimation.getKeyFrame(stateTime, false);
this.addAction(Actions.moveTo(getX(), getY() + 10, 1/ 10f));


}

if(Gdx.input.isKeyPressed(Input.Keys.D)){
reg = frontFlipAnimation.getKeyFrame(stateTime, true);
this.addAction(Actions.moveTo(getX() + 5, getY(), 1 / 10f));
}

if(Gdx.input.isKeyPressed(Input.Keys.W)){
reg = backFlipAnimation.getKeyFrame(stateTime, true);
this.addAction(Actions.moveTo(getX() - 5, getY(), 1 / 10F));
}
}





@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);

Color color = getColor();
batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
batch.draw(reg,getX(),getY(),getWidth()/2,getHeight()/2,getWidth(),getHeight(),getScaleX(),getScaleY(),getRotation());
}

private void defineKen(){
BodyDef bdef = new BodyDef();
bdef.position.set(32 / 100, 32 / 100);
bdef.type = BodyDef.BodyType.DynamicBody;

body = world.createBody(bdef);

FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(7 / 100);


fdef.shape = shape;
body.createFixture(fdef).setUserData(this);
body.createFixture(fdef).setUserData(this);
}




}

No gravity........

Repo

非常感谢

最佳答案

您使用 Box2d,Box2d 是一个 2d 物理引擎,因此它还集成了重力。

首先,当您创建世界时,您可以定义重力:

world = new World(new Vector2(0,-15f), true);

这会拉低你的播放器。

那么你必须每一帧更新世界。所以在render()方法中调用:

world.step(delta, 6, 2);

现在, body 位置将由世界计算,并将重力应用于 body 。

现在重要的是,你首先要有一个静态物体作为地面,否则,物体会无限下落。其次,您必须将绘制 Ken 图像的位置更改为 Body 的位置,因此在 act() 方法中更新位置:

setX(body.getPosition().x);
setY(body.getPosition().y);

也许您寻找一些 Box2d 教程来更好地理解: https://www.gamedevelopment.blog/full-libgdx-game-tutorial-box2d/

关于java - LibGDX applyLinearImpulse 试图实现重力....困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56035031/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com