java - 不同类中 Sprite 的代码

标签 java android class android-studio libgdx

我想制作一个游戏,其中当主角 Sprite 的X坐标小于屏幕中间时,他向右移动,当大于屏幕中间时,他向左移动。当 Sprite 移动和静止时(到达目的地后), Sprite 的动画会发生变化。 我想知道的是,当 Sprite 及其动画代码位于一个类中,而更改其 X 坐标的代码位于另一个类中时,我该如何执行此操作? 最好是在每个类中绘制相同的 Sprite ? 如何在 Sprite 水平移动和静止时更改 Sprite ? 我计划将 Sprite Action 的代码分开不同的类(class),因为我想随机调用它们。这是我的 Sprite 动画代码,我还没有用于更改 x 坐标的类:

    import com.badlogic.gdx.Gdx;
    import com.badlogic.gdx.Screen;
    import com.badlogic.gdx.audio.Music;
    import com.badlogic.gdx.audio.Sound;
    import com.badlogic.gdx.graphics.GL20;
    import com.badlogic.gdx.graphics.Texture;
    import com.badlogic.gdx.graphics.g2d.Animation;
    import com.badlogic.gdx.graphics.g2d.Sprite;
    import com.badlogic.gdx.graphics.g2d.TextureRegion;
    import com.jpfalmazan.ninjaassault.NinjaAssault;



public class MainScreen implements Screen {
private static final int  FRAME_COLS = 3;
private static final int  FRAME_ROWS = 2;


Animation  walkAnimation;
Texture    walkSheet  = new Texture ("ninjaWalk.png");
TextureRegion[] walkFrames;
TextureRegion   currentFrame;
Texture holdStart;

float stateTime;

public Texture background;

private NinjaAssault game;
private Music BackgroundSFX;
public float screenWidth = Gdx.graphics.getWidth();
public float screenHeight = Gdx.graphics.getHeight();
float x = screenWidth/2;
float y = screenHeight/2;


public float walkSheetWidth = walkSheet.getWidth();
public float walkSheetHeight = walkSheet.getHeight();


public MainScreen (NinjaAssault game){
    this.game = game;
}

@Override
public void show(){

    background = new Texture("BGBlue.png");
    holdStart = new Texture ("HoldStart.png");


    BackgroundSFX = Gdx.audio.newMusic(Gdx.files.internal("data/RADWIMPS-iindesuka.mp3"));

    TextureRegion[][] tmp = TextureRegion.split(walkSheet, (int )walkSheetWidth/FRAME_COLS, (int) walkSheetHeight/FRAME_ROWS);
    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(0.0887f, walkFrames);
    stateTime = 0f;
}

@Override
public void render(float delta) {


    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stateTime += Gdx.graphics.getDeltaTime();
    currentFrame = walkAnimation.getKeyFrame(stateTime, true);
    float hsWidth = holdStart.getWidth();
    float hsHeight = holdStart.getHeight();
    float currentFrameWidth = (float)(screenHeight*0.15);
    float currentFrameHeight = (float)(screenHeight*0.15);
    float holdStartWidth = (float)(screenWidth * 0.75);




    game.batch.begin();


    game.batch.draw(background,0,0, screenWidth,screenHeight);
    BackgroundSFX.play();
    game.batch.draw(currentFrame, x -currentFrameWidth/2, 0,currentFrameWidth,currentFrameHeight);
    game.batch.draw(holdStart, (screenWidth / 2 - (holdStartWidth / 2)), (float) (screenHeight * 0.5), holdStartWidth, holdStartWidth * (hsHeight / hsWidth));


    game.batch.end();
}

@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {
    BackgroundSFX.dispose();
    background.dispose();
    walkSheet.dispose();
    holdStart.dispose();
}

}

我尝试研究如何做到这一点,但我得到的答案并没有那么有帮助。

最佳答案

最简单的方法是创建一个 Player 类并将动画和帧的所有代码放入其中。然后给它一个保存位置的方法。类似于 x 坐标的 vector 或 float 。或者更好的是,使用矩形。矩形将使移动和碰撞检测变得更加容易。

类似这样的事情:

public class Player{

    private Animation  walkAnimation;
    private Texture    walkSheet;
    private TextureRegion[] walkFrames;
    private TextureRegion   currentFrame;
    private float stateTime;
    private Rectangle bound; //used for positioning and collision detection

    public Player(float x, float y, float width, float height){
        bound = new Rectangle(); 
        bound.x = x;
        bound.y = y;
        bound.width = width;
        bound.height = height;

        walkSheet = new Texture ("ninjaWalk.png");
        TextureRegion[][] tmp = TextureRegion.split(walkSheet,(int)walkSheetWidth/FRAME_COLS, (int) walkSheetHeight/FRAME_ROWS);
        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(0.0887f, walkFrames);
        stateTime = 0f;
    }

    public rectangle getBound(){
        return bound;
    }

    public void update(float delta){
        statetime += delta;
        currentFrame = walkAnimation.getKeyFrame(stateTime, true);
    }

    public TextureRegion getCurrentFrame(){
        return currentFrame;
    }

}

这只是一个未经测试的快速示例。

你说你想将玩家从另一个职业中移走。我不知道你打算如何做到这一点,但移动玩家所需要做的就是操纵边界的 x 和 y。

对您的代码的一些其他评论:

@Override
public void render(float delta) {

    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);    
    player.update(delta); // to update the player

    /***
    * This does not have to be set every single frame. Move it to show()
    *
    float hsWidth = holdStart.getWidth();
    float hsHeight = holdStart.getHeight();
    float currentFrameWidth = (float)(screenHeight*0.15);
    float currentFrameHeight = (float)(screenHeight*0.15);
    float holdStartWidth = (float)(screenWidth * 0.75);
    ****************************************************/


    BackgroundSFX.play(); // I am sure you don't need to start playing this every single frame? 60 times a second.

    game.batch.begin();
    game.batch.draw(background,0,0, screenWidth,screenHeight);
    game.batch.draw(player.getCurrentFrame(), player.getBound().x, player.getbound().y, player.getBound().width, player.getBound().height)

    game.batch.draw(holdStart, (screenWidth / 2 - (holdStartWidth / 2)), (float) (screenHeight * 0.5), holdStartWidth, holdStartWidth * (hsHeight / hsWidth));
    game.batch.end();
}

关于java - 不同类中 Sprite 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40104093/

相关文章:

python - 如何访问对象中的对象方法?

Java 在表格单元格中添加 JLayeredPane?

java - jd gui在win10上打不开

android - GCM,用户通知,同一设备上的两个 GCM 感知应用程序,当 GCM 消息与通知 key 一起发送时触发哪个应用程序?

python - 在 Python 中将类的实例追加到列表中会产生副本而不是引用

javascript - 有没有办法在 ES6 类上使用内部方法?

java - 找不到符号,我不知道为什么

java - 并行运行多个 "java -version"时 CPU 使用率过高

android - 多个 dex 文件定义 Lcom/google/ads/AdRequest$ErrorCode 和多个 dex 文件定义 Lcom/google/ads/AdRequest$ErrorCode

java - 在 GridView 组件中显示多个图像时,Android 应用程序崩溃