java - 使用 TextureRegion (LibGDX) 在 Java 中绘制部分 Sprite

标签 java android libgdx

我目前有我的主角“学生”的类(class),除了左右移动外,它没有任何行为。我设法做到了,所以我的 spritesheet 的所有帧都会渲染,所以当我按下左/右键时,我需要帮助绘制前 3 帧(这是步行周期)。 这是 Sprite 表:http://imgur.com/a/HHdm9

编辑:AND 第 2 行和第 3 行时按向上键和向下键。

学生类

    public class Student {

    private Texture player;
    private Animation<TextureRegion> playerAnimation;
    private int pX;
    private int pY;
    private SpriteBatch batch;
    private int HP;

    public Student(float speed, int x, int y){
        player = new Texture("student.png");
        TextureRegion[][] tmp= TextureRegion.split(player,450/3,450/3);
        TextureRegion[] character = new TextureRegion[9];
        int index = 0;
        for(int i=0; i <3; i++){
            for(int j=0; j < 3; j++){
                character[index++] = tmp[i][j];}
        }

        playerAnimation = new Animation<TextureRegion>(speed,character);
        pX=x;
        pY=y;
    }

    public SpriteBatch getBatch() {
        return batch;
    }

    public void setBatch(SpriteBatch batch) {
        this.batch = batch;
    }

    public void goLeft(){
        pX-=5;
        if(pX < -10){
            pX = -10;
        }
    }

    public void goRight(){
        pX+=5;
    }

    public void renderStudent(float stateTime){
        TextureRegion currentFrame = playerAnimation.getKeyFrame(stateTime,true);
        batch.draw(currentFrame, (float) pX, (float) pY, 0, 0, currentFrame.getRegionWidth(),
                currentFrame.getRegionHeight(),1.5f,1.5f,0);
    }

    public void dispose(){
        player.dispose();
    }
}

主类

    public class HaxMain extends ApplicationAdapter {
    SpriteBatch batch;
    Texture bg;                                 

    float stateTime;

    private Student student1;
    private Guard mguard, fguard;
    private Admin madmin, fadmin;

    @Override
    public void create () {
        batch = new SpriteBatch();                        
        bg = new Texture("Level1.png");

        student1 = new Student(.5f, 100, 0);
        student1.setBatch(batch);
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(1, 0, 0, 1);    
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);                       

        stateTime += Gdx.graphics.getDeltaTime();

        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            student1.goLeft();
            //Code to render image to the left
        }else if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
            //Code to render image to the right
            student1.goRight();
        }

        batch.begin();                                                  /*default code*/

        batch.draw(bg, 0, 0);
        student1.renderStudent(stateTime);
        batch.end();                                                
    }

    @Override
    public void dispose () {
        batch.dispose();
        bg.dispose();
        student1.dispose();
    }
}

最佳答案

您只需要为奔跑创建动画,并使用该动画来显示您的播放器向左或向右奔跑。您可以像这样根据播放器的方向翻转 TextureRegion:

TextureRegion[] character;
boolean isRight;    

public Student(float speed, int x, int y){
    player = new Texture("student.png");
    TextureRegion[][] tmp= TextureRegion.split(player,450/3,450/3);
    character = new TextureRegion[9];
    int index = 0;
    for(int i=0; i <3; i++){
        for(int j=0; j < 3; j++){
            character[index++] = tmp[i][j];}
    }

    TextureRegion runningFrames[]=new TextureRegion[3];

    for (int i=0;i<runningFrames.length;i++)
        runningFrames[i]= character[i];

    playerAnimation = new Animation<TextureRegion>(speed, runningFrames);
    playerAnimation.setPlayMode(Animation.PlayMode.LOOP);
    pX=x;
    pY=y;

}

public void goLeft(){
    pX-=5;
    if(pX < -10){
        pX = -10;
    }
    if(isRight){
       isRight=false;
       for (TextureRegion textureRegion:playerAnimation.getKeyFrames())
           if(textureRegion.isFlipX()) textureRegion.flip(true,false);
    } 
}

public void goRight(){
    pX+=5;
    if(!isRight){
       isRight=true;
       for (TextureRegion textureRegion: playerAnimation.getKeyFrames())
           if(!textureRegion.isFlipX()) textureRegion.flip(true,false);
    }
}

创建其他 Action 的另一个动画并使用标志绘制动画帧。

关于java - 使用 TextureRegion (LibGDX) 在 Java 中绘制部分 Sprite ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44568577/

相关文章:

java - Java 中的字符串大十进制数

java - 无法将 GPS 坐标发送到服务器

java - LibGDX:在另一个线程中创建新的 scene2d 对象?

java - 如何在进度条上显示进度号?

eclipse - 将自动生成的LibGDX gradle项目导入Eclipse时出现问题

java - 基于瓦片的碰撞检测,物体穿过瓦片掉落

java - Android 检查 WIFI 状态(断开连接或用户更改 WIFI)如何标记它?

java - 使用Java流从列表中获取包含键和该键出现次数的映射

Java正则表达式使表达式匹配a) b) c) d)

android - 是否有API可以访问移动设备上的位置记录?