【问题标题】:Actor in Stage Does Not Update the MoveTo XY Location舞台中的演员不更新 MoveTo XY 位置
【发布时间】:2017-04-20 15:41:57
【问题描述】:

我正在创建一个游戏,其中一个苹果被箭射中。苹果的位置是用户输入的 XY 位置,箭头 actor 必须使用代码 actor.moveto 移动到该位置。问题是箭头只向用户输入的方向移动一次。我知道当我在 update 方法中调用 stageArrow.act 时,演员的 moveTo 动作每秒更新多次,所以我想知道为什么箭头只移动一次。这是我的代码:

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

    }
}

我们将不胜感激任何帮助。谢谢。

【问题讨论】:

    标签: libgdx action actor stage scene2d


    【解决方案1】:

    我认为问题在于您在 ArrowClass' 绘制方法中调用了 this.act(delta)。当你调用Stage#act() 时,它会为你调用它所有actors 的act 方法。由于您在绘制时调用它一次,并且在更新阶段时再次调用它,因此它的移动速度是正常速度的两倍,这可能会导致它过早到达目的地。

    如果可以的话,还有一些关于您的代码的其他信息:

    首先,您可能不想要两个单独的阶段 - 除非您使用 Scene2D.UI,否则您通常会有一个单独的阶段,其中添加了 Arrow 和 Apple 作为演员。

    其次,当您覆盖Actor#draw() 时,您应该使用它传递给您的批处理来进行渲染,而不是使用来自应用程序的批处理。你也不想在你的 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());
        }
    }
    

    【讨论】:

    猜你喜欢
    • 2014-07-20
    • 1970-01-01
    • 2014-04-03
    • 2014-11-30
    • 2014-03-05
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多