2017-06-15 15 views
1

現在、私の主人公「Student」のクラスを持っていますが、左右の動き以外の振る舞いはありません。私は自分のスプライトシートのすべてのフレームをレンダリングするようにしましたので、LEFT/RIGHTキーを押すと最初の3つのフレーム(歩行サイクル)を描くのに助けが必要です。 ここにスプライトシートがあります:http://imgur.com/a/HHdm9TextureRegion(LibGDX)を使用してJavaで部分スプライトを描画する

編集:UPキーとDownキーを押したときの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(); 
    } 
} 
+0

いくつのフレームが 'student.png'ファイルの中にありますか、3フレームは実行中のアクションのためのもので、残りは何ですか? – Aryan

+0

@AbhishekAryanそれらはすべて3x3で、一番上の行は歩行サイクルです。 2行目と3行目は部屋の入り口と出口のためのものです。私のゲームのサイドカローラでは、キャラクターは敵が通り過ぎると隠れる部屋に入ることができます。ここに一つのイメージがあります。http://imgur.com/a/HHdm9 –

答えて

0

あなただけ実行するためのアニメーションを作成し、プレイヤーは左または右に実行されて表示するようにそのアニメーションを使用する必要があります。あなたはこのようなプレーヤーの方向に応じて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); 
    } 
} 

他のアクションの別のアニメーションを作成し、フラグを使用して、アニメーションのフレームを描画します。

+0

私はここで進歩していると思います。私はlibgdx btwで新しいです。 playerAnimation =新しいアニメーション(speed、runningFrames、Animation.PlayMode.LOOP);でアニメーションのコンストラクタを解決できないというエラーが表示されます(float、com.badlogic.gdx.graphics.g2d.TextureRegion []、com.badlogic.gdx.graphics.g2d.Animation.PlayMode)。それは学生のコンストラクタで私のパラメータと関係がありますか? –

+0

@ErrolEmpeñoご確認ください、私の回答が更新されました – Aryan

+0

私はあなたのアップデートをチェックしただけで動作します!他の2つの動作(部屋に入る/出る)では、基本的に新しいアニメーションを作成するだけで、それらのフレームを分離して独自のスプライトシートを作成する必要がありますか、TextureRegionを分割した後に同じフレームを使用できますか? –

関連する問題