简体   繁体   English

lua函数中的事件是什么

[英]What are events in lua functions

I get the idea of parameters in functions, but I don't know what an event is. 我在函数中得到了参数的概念,但我不知道事件是什么。 I have also heard of JavaScript function events but since I have no js experience, I don't know what they are. 我也听说过JavaScript函数事件,但是由于我没有js经验,所以我不知道它们是什么。

There is nothing special in Lua that can be called 'event'. Lua中没有什么特别的东西可以称为“事件”。 I guess you are talking about general events (from Wikipedia): 我猜您在谈论一般事件 (来自Wikipedia):

In computing an event is an action that is usually initiated outside the scope of a program and that is handled by a piece of code inside the program 在计算事件时,事件是通常在程序范围之外启动并由程序内部的一段代码处理的动作

An example of events are a mouse click, key press, download finished, anything you can imagine. 事件示例包括鼠标单击,按键按下,下载完成,以及您可以想象的任何事情。

In order to react to an event, you need to write a so-called handler , sometimes also called listener or callback , which is a piece of code that you register to react to a certain event. 为了对事件做出反应,您需要编写所谓的处理程序 ,有时也称为侦听器回调 ,这是您注册以对特定事件做出反应的一段代码。 The available events, handling process and handler registration are all dictated by the library/framework you are using, ie it's not Lua specific, but Lua does provide functions as way to write handlers. 可用的事件,处理过程和处理程序注册均由您所使用的库/框架决定,即,它不是Lua特定的,但是Lua确实提供了用于编写处理程序的函数

For example, in Corona SDK ( overview of events/listeners ), you can handle the 'touch' event as follows: 例如,在Corona SDK( 事件/监听器的概述 )中,您可以按以下方式处理“触摸”事件:

Runtime:addEventListener("touch", function(event)
    print("A touch event is being handled")
    ...
end)

There is no such thing as an 'event' in a standard Lua function. 在标准的Lua函数中没有“事件”这样的东西。 Perhaps there is some library that provides you with them, or you could script your way around to emulate the wanted behaviour. 也许有一些库为您提供它们,或者您可以编写脚本来模拟想要的行为。

You can get more concrete answers if you provide info about what you want to accomplish, and show some code. 如果您提供有关要完成的任务的信息并显示一些代码,则可以获得更具体的答案。

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

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