【问题标题】:OOP player implementation libgdxOOP 播放器实现 libgdx
【发布时间】:2023-03-25 18:35:01
【问题描述】:

我想对播放器的实现对象提出一些建议。 玩家可以根据按下的按钮选择 2 个纹理,例如,向右箭头将在左侧显示一个纹理,而另一个纹理将显示在左侧。

我已经在播放器类中实现了接口输入处理器,但我不知道为什么它看起来不是很公平,很动态,所以我想请你提一些建议

非常感谢

代码:

public class Player  implements InputProcessor {

    private Texture up;
    private Texture down;
    private Sprite player;

    public Player(Texture one,Texture two){

        this.up=one;
        this.down=two;
        player=new Sprite(one);
    }


    @Override
    public boolean keyDown(int keycode) {
        // TODO Auto-generated method stub
        player.setTexture(this.down);
        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        // TODO Auto-generated method stub
        player.setTexture(this.up);
        return false;
    }

    @Override
    public boolean keyTyped(char character) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean scrolled(int amount) {
        // TODO Auto-generated method stub
        return false;
    }

}

我以这种方式实现播放器类,但我想要一些最好的实现建议。

【问题讨论】:

  • 您能添加一些代码来说明您的问题吗?我不确定我是否理解您的要求。

标签: java android oop libgdx


【解决方案1】:

我建议你继承非常有用的Sprite 类并使用TextureAtlas 来包含玩家纹理。这是实现的存根:

 public class Player extends Sprite {

    TextureAtlas textureAtlas;

    public Player(TextureAtlas atlas) {
        super(atlas.getRegions().get(0));
        textureAtlas = atlas;
    }

    public void setTexture(String textureName) {
        setRegion(textureAtlas.findRegion(textureName)));
    }

}

Here 是一个很好的教程,解释了如何使用TextureAtlas

另外,不要实现整个InputProcessor,而是使用InputAdapter,它提供了这个接口的一个空实现。这将是much more clearer。它可以是 Player 类中的一个字段:

   private InputProcessor inputProcessor = new InputAdapter() {
   // Override only methods you need here
   };

当然,别忘了注册这个InputProcessor:

Gdx.input.setInputProcessor(inputProcessor);

【讨论】:

  • 好的,但是如果我想在按键事件的其他类中实现 InputAdapter,我必须更改播放器的纹理。从我获取对象播放器的位置。不知道我解释的好不好
  • @dvdfrosiii 然后你必须以某种方式将 Player 的引用传递给这个类。
  • 尝试在 Player 类中设置输入处理器并覆盖函数。例如Gdx.input.setInputProcessor(new InputAdapter(){@Override public boolean keyDown(int keycode) {} });
  • 在这种情况下,播放器是唯一可以捕获输入的东西,我宁愿从屏幕上获取它(这正是我个人所做的,并将当前屏幕设置为输入处理器show() 方法)然后执行 stackoverflow.com/a/24511980/2413303 虽然如果你有一个正交相机,那么你只需要使用“camera.unproject”而不是有自己的输入转换。
【解决方案2】:

我会在几个小时后回家时用例子更新我的答案。

在播放器类中监听键似乎不是一个好习惯。我前段时间在我的游戏中实现了一个玩家类,它是这样的:

  • 在您的游戏类的渲染方法中,您实现关键侦听器,调用 player.up() 或 player.down() 或例如 player.jump()。
  • 在这些方法中的播放器类中,例如在 jump() 中设置纹理/动画。

我将发布代码示例。

public class Player {
float x;
float y;
float xSpeed;
float ySpeed;
private Animation playerRun;
private Animation playerJump;
private Animation playerStand;
private Animation current;
private Rectangle bounds;

public Player(){
    playerRun = new Animation(1/10f, Assets.atlas.findRegions("run"));
    playerRun.setPlayMode(Animation.PlayMode.LOOP);

    //set other animations in similar way

    current = playerStand;

    this.x = Gdx.graphics.getWidth()/2;
    this.y = Gdx.graphics.getHeight()/2;

    bounds = new Rectangle(x, y, 100, 100);
}

public Rectangle getBounds(){
    return bounds;
}

public void setPos(float x, float y){
    this.x = x;
    this.y = y;
    bounds.x = x;
    bounds.y = y;
}

public void changePos(float x, float y){
    this.x +=x;
    this.y +=y;
    bounds.x += x;
    bounds.y += y;
}

public void move(){
    changePos(xSpeed, ySpeed);
}

public Animation animate(){
    return current;
}

public void run(){
    current = playerRun;
    ySpeed = 0;
}

public void jump(){
    current = playerJump;
    ySpeed = 10;
}

而我的游戏类中的渲染方法只是调用玩家方法:

if (Gdx.input.isTouched()){
                player.jump();
            }

哎呀,忘了最重要的部分:

batch.draw(player.animate().getKeyFrame(time += delta), player.x, player.y);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多