2016-12-06 25 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