简体   繁体   中英

Code for Sprite in Different Class

I want to make a game wherein when the main character sprite's X coordinate is less than the middle of the screen, he moves to the right and when it's more than the middle of the screen, he moves to the left. The sprite's animation changes when he is moving and when he is still (after reaching its destination). What I want to know is how can I do this when the sprite and its code for animation is in one class and the code for changing its X coordinate is in another class? Is it best to draw the same sprite in every class? How can I change the sprite when it is moving horizontally and when it is still? I plan to separate the code for what the sprite's actions will be in different classes because i want to call them randomly. Here is my code for the sprite's animation and i have no class for changing the x coordinate yet :

    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();
}

}

I tried to research on how to do this but the answers I get wasn't that helpful.

Simplest way is to create a Player class and put all the code for the animation and frames in there. Then give it a way to save its position. Like a vector or just a float for the x coordinate. Or even better, use a Rectangle. A rectangle will make moving and collision detection much easier.

Something like this:

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;
    }

}

This is just a quick untested example.

You say you want to move the player from another class. I don't know how you plan to do that, but all you need to do to move the player is to manipulate the x and y of the bound.

Just some other comments on you code:

@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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM