2016-12-06 23 views
0

我正在創建一個遊戲,其中一個蘋果正在用箭頭射擊。蘋果的位置是用戶輸入的XY位置,箭頭演員必須使用代碼actor.moveto移動到該位置。問題是箭頭只能移動一次到用戶輸入的方向。我知道,當我在update方法中調用stageArrow.act時,actor的moveTo動作每秒更新多次,所以我想知道爲什麼箭頭只移動一次。這裏是我的代碼:舞臺中的演員不會更新MoveTo XY位置

appleclass.java

public class AppleClass implements Screen { 

Arrow arrow; 
private final MainApp app; 
public Image ShotImage; 

public AppleClass(final MainApp app){ 
     this.app = app; 
     this.stageApple = new Stage(new StretchViewport(app.screenWidth,app.screenHeight , app.camera)); 
     this.stageArrow =new Stage(new StretchViewport(app.screenWidth,app.screenHeight , app.camera)); 

     arrow = new ArrowClass(app); 

    } 

@Override 
    public void show() { 

     InputMultiplexer inputMultiplexer = new InputMultiplexer(); 
     inputMultiplexer.addProcessor(stageApple); 
     inputMultiplexer.addProcessor(stageArrow); 
     Gdx.input.setInputProcessor(inputMultiplexer); 

     arrow(); 

    } 

    public void arrow(){ 
     arrow.isTouchable(); 
     stageArrow.addActor(arrow); 
     arrow.addAction((moveTo(Gdx.input.getX(),Gdx.input.getY(),0.3f))); //===> only executes once. 
     arrow.addListener(new InputListener(){ 
      public void enter(InputEvent event, float x, float y, int pointer, Actor fromActor){ 


      if (Gdx.input.isTouched()){ 
         ShotImage.setVisible(true); 

          } 
         } 
        });} 

} 

@Override 
    public void render(float delta) {       

     Gdx.gl.glClearColor(0, 0, 0, 1); 
     Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
     update(delta); 
} 

public void update(float deltaTime){ 


     stageApple.draw(); 
     stageArrow.draw(); 

     stageApple.act(deltaTime); 
     stageArrow.act(deltaTime); 


    } 

ArrowClass.java

public class ArrowClass extends Actor { 
    MainApp app; 
    AppleClass appleClass; 
    public Texture arrowTexture; 

    public ArrowClass(final MainApp app){ 
     this.app = app; 
     arrowTexture = new Texture("medievalarrow.png"); 
     this.setSize(arrowWidth, arrowHeight); 
     this.setTouchable(Touchable.enabled); 
     this.setBounds(app.screenWidth*0.45f,0,arrowWidth,arrowHeight); 
     this.setOrigin(0,0); 

} 
@Override 
    public void draw(Batch batch, float parentAlpha) { 
     super.draw(batch, parentAlpha); 


     final float delta = Gdx.graphics.getDeltaTime(); 
     this.act(delta); 

     app.batch.begin(); 
     app.batch.draw(arrowTexture, getX(),getY(),getWidth(),getHeight()); 
     app.batch.end(); 

    } 
} 

任何幫助將得到高度讚賞。謝謝。

回答

1

我認爲這個問題是因爲你在調用的ArrowClass的繪製方法。當你打電話給Stage#act()時,它會爲你的所有演員調用act方法。既然你在畫圖時調用它,當你更新舞臺時,它會以正常速度的兩倍移動,並可能導致它過早地到達目的地。

一些有關您的代碼的其他意見,如果我可以:

首先,你可能不希望兩個獨立的滑臺,除非你使用Scene2D.UI,你通常有箭頭和蘋果單級作爲演員添加到它。

其次,當您覆蓋Actor#draw()時,應該使用它傳遞給您的批量來執行渲染,而不是使用來自應用的批量。你也不想在你的繪圖方法中調用begin()end()-這些是「昂貴的」,你只想每幀調用一次。但是,如果您只使用傳遞給draw()的批處理,則Stage類將爲您處理起始和結束,並且您不需要明確調用它們。

第三,您實際上不需要撥打super.draw(batch, parentAlpha),因爲它是Actor類中的空方法。

因此,你的類可以簡化爲以下幾點:

public class ArrowClass extends Actor { 
    AppleClass appleClass; // You never set this; you may not need it 
    Texture arrowTexture; 

    public ArrowClass(MainApp app, arrowWidth, arrowHeight) { 
     arrowTexture = new Texture("medievalarrow.png"); 
     this.setSize(arrowWidth, arrowHeight); 
     this.setTouchable(Touchable.enabled); 
     this.setBounds(app.screenWidth*0.45f,0,arrowWidth,arrowHeight); 
     this.setOrigin(0,0); 

} 
@Override 
    public void draw(Batch batch, float parentAlpha) { 
     batch.draw(arrowTexture, getX(),getY(),getWidth(),getHeight()); 
    } 
} 
+0

能否請你看看我的問題在這裏:http://gamedev.stackexchange.com/questions/135748/how-to-讓會員-的-AN-ArrayList的 - 到 - 的 - 演員和添加他們到舞臺 – JAlmazan