scene2d:如何在场景/ root上触发(事件)和处理?(scene2d: how to fire(event) and handle at scene/root?)

系统教程 行业动态 更新时间:2024-06-14 17:00:14
scene2d:如何在场景/ root上触发(事件)和处理?(scene2d: how to fire(event) and handle at scene/root?)

我试图将touchDown和touchUp事件从actor Object升级到我的applicationListener类。 为此,我打电话给火(事件); 在我的Actor的InputListener中

this.addListener(new InputListener(){ public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){ Gdx.app.log("Example", "touch started at (" + x + ", " + y + ")"); fire(event); return true; } public void touchUp(InputEvent event, float x, float y, int pointer, int buttons){ Gdx.app.log("Example", "touch ended at (" + x + ", " + y + ")"); } });

为了处理我的ApplicationListener类(包含actor的阶段)中的事件,我向舞台添加了一个InputListener

Gdx.input.setInputProcessor(stage); stage.addListener(new InputListener(){ public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){ Gdx.app.log("FIRE!!!", "I CAUGHT A FIRED EVENT!"); event.stop(); return true; } public void touchUp(InputEvent event, float x, float y, int pointer, int buttons){ Gdx.app.log("FIRE!!!", "the fired event even touchupped."); } });

然而,当我触摸我的actor时,我得到了一个StackOverflowError以及来自几个InputListeners的大量异常(我假设事件未正确停止并传播到我场景中的所有actor)。 我在这里缺少什么?

在触发事件之后,我不能再将event.getTarget()(这是一个Actor)转换为我的Actor-Subclass,如果我在ActorSubclass本身中执行此操作,则可以正常工作。 这意味着以下代码在ApplicationListener中使用时会产生错误,但在MyActor类中有效:

MyActor actor = (MyActor)event.getTarget();

由于目标实际上是一个MyActor对象,我怎么能不仅将其作为Actor访问,而且作为MyActor访问?

i am trying to escalate touchDown and touchUp events from an actor Object to my applicationListener class. To do so, i called fire(event); in the InputListener of my Actor

this.addListener(new InputListener(){ public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){ Gdx.app.log("Example", "touch started at (" + x + ", " + y + ")"); fire(event); return true; } public void touchUp(InputEvent event, float x, float y, int pointer, int buttons){ Gdx.app.log("Example", "touch ended at (" + x + ", " + y + ")"); } });

To handle the event in my ApplicationListener class (which contains the stage of the actors), i added an InputListener to the stage

Gdx.input.setInputProcessor(stage); stage.addListener(new InputListener(){ public boolean touchDown(InputEvent event, float x, float y, int pointer, int buttons){ Gdx.app.log("FIRE!!!", "I CAUGHT A FIRED EVENT!"); event.stop(); return true; } public void touchUp(InputEvent event, float x, float y, int pointer, int buttons){ Gdx.app.log("FIRE!!!", "the fired event even touchupped."); } });

However when i touch my actor, i get a StackOverflowError as well as a ton of exceptions from several InputListeners (i assume that the event is not stopped properly and propagated to all actors in my scene). What am i missing here?

Also after firing the event i cant cast event.getTarget() (which is an Actor) to my Actor-Subclass anymore, which works just fine if i do this in the ActorSubclass itself. Meaning the following code creates an error when used in the ApplicationListener, but works in the MyActor class:

MyActor actor = (MyActor)event.getTarget();

Since the target is in fact a MyActor Object, how can i access it not only as Actor, but as MyActor?

最满意答案

正如@chase所说,你是递归调用fire(even)方法,因为你只能设置1个InputProcessor(你的stage ),所以总是相同的方法接收事件。 您应该使用InputMultiplexer :

将Stage和ApplicationListener作为InputProcessor到InputMultiplexer 通过调用Gdx.input.setInputProcessor(multiplexer);将InputProcessor设置为InputMulitplexer Gdx.input.setInputProcessor(multiplexer); 。

然后, InputMultiplexer将事件提供给第一个InputProcessor ,如果返回false,它将把事件发送到下一个。 所以在你的Stage你只需要返回false。 但我不明白为什么你要将你的Stage添加为InputProcessor如果你不想让它处理输入... 无论如何,在scene2d中你不需要为舞台添加一个InputProcessor ,因为它已经有了一个。 Actor allready还有一个boolean touchDown(float x, float y, int pointer)和其他有用的方法。 一个有用的链接: Scene2D wiki文章

As @chase said you are recursivelly calling the fire(even) method, as you can only set 1 InputProcessor (your stage) and so always the same method recieves the event. You should instead use a InputMultiplexer:

add the Stage and your ApplicationListener as InputProcessor to the InputMultiplexer Set the InputMulitplexer as the InputProcessor by calling Gdx.input.setInputProcessor(multiplexer);.

The InputMultiplexer will then give the event to the first InputProcessor and if it returns false, it will send the event to the next one. So in your Stage you just need to return false. But i don't understand why you are adding your Stage as a InputProcessor if you don't want it to handle inputs... Anyways in scene2d you don't need to add an InputProcessor to the stage as it allready has one. Also the Actor allready has a boolean touchDown(float x, float y, int pointer) and other helpful methods. A useful link: Scene2D wiki article

更多推荐

本文发布于:2023-04-18 00:55:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/b5bdde69019cdea160eeaa14c047b356.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:场景   事件   root   scene2d   scene

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!