2016-10-18 5 views
0

メインキャラクタスプライトのX座標が画面の中央よりも小さい場合は右に移動し、画面の中央よりも大きい場合は移動します左に。スプライトのアニメーションは、彼が動いているときと彼が静止しているとき(目的地に到達した後)に変化します。 私が知りたいのは、スプライトとアニメーションのコードが1つのクラスにあり、そのX座標を変更するコードが別のクラスにある場合、どうすればよいのでしょうか?すべてのクラスに同じスプライトを描画するのが最善でしょうか?スプライトが水平方向に移動しているときや、静止しているときに、スプライトをどのように変更できますか?私はスプライトのアクションをランダムに呼びたいので、別のクラスでスプライトのアクションをどのようにするかについてコードを分ける予定です。ここでは、スプライトのアニメーションのための私のコードがあると私は、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(); 
} 

}

私はこれを行う方法の研究を試みたが、私が得る答えはそれではなかったです役に立った

答えて

1

最も簡単な方法は、Playerクラスを作成し、アニメーションとフレームのすべてのコードをそこに配置することです。その後、その地位を救う方法を与えてください。 x座標のベクトルや浮動小数点のようなものです。または、Rectangleを使用してください。四角形を使用すると、移動や衝突の検出がずっと簡単になります。

このような何か:

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

、それはレンダリング赤で私の変数を強調しています。 – JAlmazan

+0

はい、思考せずに移動するだけの場合:) show()で宣言すると、そのメソッドでのみ表示されます。あなたが必要とするのは、それらをクラス変数として宣言し(プライベート/パブリックとしてクラスの最上位にある)、show()でそれらの値を代入することです。このようにして、このクラスのすべてのメソッドで変数が表示されます。 – IronMonkey

+0

これに答えることができますか? http://gamedev.stackexchange.com/questions/134005/actor-in-stage-does-not-update-the-moveto-xy-location – JAlmazan

関連する問題