简体   繁体   English

LibGDX-Scene2D平移

[英]LibGDX - Scene2D panning

I've been working on a project lately, for my homework. 我最近一直在做一个项目,以做作业。
Well, going to the point, I have a Screen implementation of Scene2D, the issue I have is that my touch events on the widgets I have don't stop on the widgets. 好吧,到这一点,我有一个Scene2D的Screen实现,我遇到的问题是我在窗口小部件上的触摸事件没有停止在窗口小部件上。
I mean, If i popup a window, and try to move it, it also triggers the panning on my graphic, or if I move a slider, it also pans the camera. 我的意思是,如果我弹出一个窗口并尝试移动它,它也会触发图形上的平移,或者如果我移动一个滑块,它也会平移相机。

Here's my project on GitHub 这是我在GitHub上项目
My events are configured on the class Pantalla on Core, Here 我的事件是对核心类Pantalla配置, 在这里
Thanks in advance. 提前致谢。

Your problem is that you are using the stage with all actors (widgets etc) as input processor for camera gesture actions. 您的问题是您将舞台与所有演员(小部件等)一起用作相机手势动作的输入处理器。 It means that whenever you will apply some gesture on any actor that belongs to it it will trigger. 这意味着,只要您对属于它的任何角色施加某种手势,就会触发。

The solution is to create another stage only for camera gestures over the current stage . 解决方案是在当前阶段仅针对摄像机手势创建另一个阶段 So your code should looks like: 因此,您的代码应如下所示:

    //show method
    viewport = new FitViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    // Creamos el stage, el cual albergara los botones entre otras cosas
    stage = new Stage(viewport);

    cameraStage = new Stage(viewport); //I'm not super-sure if you can user viewport second time - if not create new one

    ...

    //render method
    stage.act();
    stage.draw(); 

    cameraStage.act();
    cameraStage.draw(); //cameraStage is drawn after stage so it will be over it!

    ...

Then you should add all listeners connected with camera gestures to cameraStage not stage but of course its functions should affect still actor stage . 然后,您应该将与摄像机手势相关的所有侦听器添加到cameraStage而不是stage中, 但是当然它的功能应该影响静止演员的stage

Ok then you have two stages, the camera stage is over the stage with actors so you can touch wherever you want and you are sure you are touching both stages and what you have to do now is to set both stages as input processors actor stage as first and camera stage as second that you will be sure that event from actors stage are proceeded as first. 好的,那么您有两个阶段,摄影机阶段位于包含演员的阶段,因此您可以触摸任意位置,并且可以确定同时触摸了两个阶段,现在要做的就是将这两个阶段都设置为输入处理器actor阶段,首先是摄影台,其次摄影台 ,您将确保首先进行演员舞台的活动。

You will need InputMultiplexer to do this. 您将需要InputMultiplexer来执行此操作。 The main scheme of code is: 代码的主要方案是:

    InputMultiplexer inputMultiplexer = new InputMultiplexer();

    inputMultiplexer.addProcessor(stage);
    inputMultiplexer.addProcessor(cameraStage);

    Gdx.input.setInputProcessor(inputMultiplexer);

Now your widgets events are handled as first. 现在,首先处理窗口小部件事件。

If somethin won't work think also about removing cameraStage from inputMultiplexer when stade is touched down and adding it again when is touched up. 如果某事行不通,请考虑一下在按下stade时从inputMultiplexer中删除cameraStage,并在按下stade时再次添加它。


One simple advice also - especially when you are creating a tool that will be shared in the future use english variables/methods/etc names in the code - it will be more clear for other users 一个简单的建议-特别是当您创建一个将来将共享的工具时,请在代码中使用英文变量/方法/ etc的名称-对于其他用户而言,它将更加清楚

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM