使用 Flixel 覆盖 onTouchEvent(Android 端口)
所以我跟着 Mathew Casperson 的在 Android 上制作游戏教程并在几天前运行了一个小游戏,现在我尝试将控件切换到触摸屏而不是方向键。
我遇到了一些问题,想知道这里是否有人可以帮助我。 Flixel 没有任何内置触摸屏功能,因此我在 Activity(教程中的 FlixelDemo.java
)中重写 onTouchEvent(MotionEvent event)
并希望获得触摸。
然后,我的 Player.java 中有一个函数,给定触摸坐标可以告诉我我的播放器是否已被触摸。
我遇到的问题是试图弄清楚如何从活动中访问/调用该函数(isCollision
)。
看来我只能重写 FlixelDemo.java 中的 onTouchEvent 并且只能使用 GameState.java 中的 isCollision 函数,我在其中添加玩家。
如何从覆盖的触摸事件中获取信息到我的任何其他类?谁能告诉我我做错了什么或帮助我找出实现触摸事件的不同方法?
So I followed Mathew Casperson's Making Games on Android Tutorial and got a small game running a few days ago, now I am trying to switch the controls to touchscreen instead of the D-pad.
I am running into some problems and was wondering if anyone here could help me. Flixel doesn't have any built in touchscreen functions so I am overriding onTouchEvent(MotionEvent event)
in my Activity (FlixelDemo.java
in the tutorial) and hopefully getting the coordinates of the touch.
I then have a function in my Player.java
that given the touch coordinates could tell me whether my player has been touched.
The problem I am having is trying to figure out how to get to/call that function (isCollision
) from in the activity.
It seems that I can only override the onTouchEvent in FlixelDemo.java
and that I can only use the isCollision
function in GameState.java
where I add the player.
How do I get the info from the overridden touch event to any of my other classes? Can anyone tell me what I am doing wrong or help me figure out a different way of implementing touch events?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看代码,
FlixelDemo
实际上只是org.flixel.FlxGameView
的容器(通过res/layout/main.xml
文件) 。onTouchEvent
方法可以应用于任何View
,因此您可以将其仅应用于 flixel 视口。事实上,这可能正是您想要执行的操作:将处理程序直接添加到
FlxGameView.java
中,然后让它调用内部GameThread
类上的方法。它已经以这种方式处理其他事件。请参阅
FlxGameView.onKeyDown
(以及相关的FlxGameView.GameThread.doKeyDown
)以获得一个很好的示例。Looking at the code,
FlixelDemo
is really just a container fororg.flixel.FlxGameView
(via theres/layout/main.xml
file).The
onTouchEvent
method can be applied to anyView
, so you can apply it to just the flixel viewport.And in fact, that's probably what you want to do here: Add your handler directly to
FlxGameView.java
, and then let it call a method on the internalGameThread
class.It's already handling the other events this way. See
FlxGameView.onKeyDown
(and the associatedFlxGameView.GameThread.doKeyDown
) for a good example.