Love2D游戏编程-3(回调函数 Callback Functions)

(本文使用Windows平台)

1、回调函数列表

在main.lua里我们要处理游戏逻辑,主要依靠回调函数,它们会被love自动调用。

love.draw    Callback function used to draw on the screen every frame.
love.focus    Callback function triggered when window receives or loses focus.
love.joystickpressed    Called when a joystick button is pressed.
love.joystickreleased    Called when a joystick button is released.
love.keypressed    Callback function triggered when a key is pressed.
love.keyreleased    Callback function triggered when a key is released.
love.load    This function is called exactly once at the beginning of the game.
love.mousepressed    Callback function triggered when a mouse button is pressed.
love.mousereleased    Callback function triggered when a mouse button is released.
love.quit    Callback function triggered when the game is closed.
love.run    The main function, containing the main loop. A sensible default is used when left out.
love.update    Callback function used to update the state of the game every frame.

2、部分回调函数详解

love.load

function love.load()
   image = love.graphics.newImage("cake.jpg")
   love.graphics.setNewFont(12)
   love.graphics.setColor(0,0,0)
   love.graphics.setBackgroundColor(255,255,255)
end

This function gets called only once, when the game is started, and is usually where you would load resources, initialize variables and set specific settings. All those things can be done anywhere else as well, but doing them here means that they are done once only, saving a lot of system resources.

love.update

function love.update(dt)
   if love.keyboard.isDown("up") then
      num = num + 100 * dt -- this would increment num by 100 per second
   end
end

This function is called continuously and will probably be where most of your math is done. ‘dt‘ stands for "delta time" and is the amount of seconds since the last time this function was called (which is usually a small value like 0.025714).

love.draw

function love.draw()
   love.graphics.draw(image, imgx, imgy)
   love.graphics.print("Click and drag the cake around or use the arrow keys", 10, 10)
end

love.draw is where all the drawing happens (if that wasn‘t obvious enough already) and if you call any of the love.graphics.draw outside of this function then it‘s not going to have any effect. This function is also called continuously so keep in mind that if you change the font/color/mode/etc at the end of the function then it will have a effect on things at the beginning of the function. For example:

function love.load()
   love.graphics.setColor(0,0,0)
end

function love.draw()
   love.graphics.print("This text is not black because of the line below", 100, 100)
   love.graphics.setColor(255,0,0)
   love.graphics.print("This text is red", 100, 200)
end

love.mousepressed

function love.mousepressed(x, y, button)
   if button == ‘l‘ then
      imgx = x -- move image to where mouse clicked
      imgy = y
   end
end

This function is called whenever a mouse button is pressed and it receives the button and the coordinates of where it was pressed. The button can be any of the constants. This function goes very well along with love.mousereleased.

love.mousereleased

function love.mousereleased(x, y, button)
   if button == ‘l‘ then
      fireSlingshot(x,y) -- this totally awesome custom function is defined elsewhere
   end
end

This function is called whenever a mouse button is released and it receives the button and the coordinates of where it was released. You can have this function together withlove.mousepressed or separate, they aren‘t connected in any way.

love.keypressed

function love.keypressed(key)
   if key == ‘b‘ then
      text = "The B key was pressed."
   elseif key == ‘a‘ then
      a_down = true
   end
end

This function is called whenever a keyboard key is pressed and receives the key that was pressed. The key can be any of the constants. This functions goes very well along with love.keyreleased.

love.keyreleased

function love.keyreleased(key)
   if key == ‘b‘ then
      text = "The B key was released."
   elseif key == ‘a‘ then
      a_down = false
   end
end

This function is called whenever a keyboard key is released and receives the key that was released. You can have this function together with love.keypressed or separate, they aren‘t connected in any way.

love.focus

function love.focus(f)
  if not f then
    print("LOST FOCUS")
  else
    print("GAINED FOCUS")
  end
end

This function is called whenever the user clicks off and on the LÖVE window. For instance, if they are playing a windowed game and a user clicks on his Internet browser, the game could be notified and automatically pause the game.

function love.focus(f) gameIsPaused = not f end

function love.update(dt)
    if gameIsPaused then return end

    -- The rest of your love.update code goes here
end

love.quit

function love.quit()
  print("Thanks for playing! Come back soon!")
end

This function is called whenever the user clicks the windows close button (often an X). For instance, if the user decides they are done playing, they could click the close button. Then, before it closes, the game can save its state.

Those are the callback functions and their basic usage.

时间: 2024-11-05 15:50:26

Love2D游戏编程-3(回调函数 Callback Functions)的相关文章

C++中回调函数(CallBack)的使用

如果试图直接使用C++的成员函数作为回调函数将发生错误,甚至编译就不能通过. 其错误是普通的C++成员函数都隐含了一个传递函数作为参数,亦即“this”指针,C++通过传递this指针给其成员函数从而实现成员函数可以访问C++的数据成员.这也可以理解为什么C++类的多个实例可以共享成员函数却-有不同的数据成员.由于this指针的作用,使得将一个CALL-BACK型的成员函数作为回调函数安装时就会因为隐含的this指针使得函数参数个数不匹配,从而导致回调函数安装失败.要解决这一问题的关键就是不让t

C++回调函数(callback)的使用

什么是回调函数(callback)    模块A有一个函数foo,他向模块B传递foo的地址,然后在B里面发生某种事件(event)时,通过从A里面传递过来的foo的地址调用foo,通知A发生了什么事情,让A作出相应反应. 那么我们就把foo称为回调函数.   例子:      回调函数是个很有用,也很重要的概念.当发生某种事件时,系统或其他函数将会自动调用您定义的一段函数.回调函数在windows编程使用的场合很多, 比如Hook回调函数:MouseProc,GetMsgProc连同EnumW

理解javascript中的回调函数(callback)【转】

在JavaScrip中,function是内置的类对象,也就是说它是一种类型的对象,可以和其它String.Array.Number.Object类的对象一样用于内置对象的管理.因为function实际上是一种对象,它可以"存储在变量中,通过参数传递给(别一个)函数(function),在函数内部创建,从函数中返回结果值". 因为function是内置对象,我们可以将它作为参数传递给另一个函数,延迟到函数中执行,甚至执行后将它返回.这是在JavaScript中使用回调函数的精髓.本篇文

理解javascript中的回调函数(callback)

以下内容来源于:http://www.jb51.net/article/54641.htm 最近在看 express,满眼看去,到处是以函数作为参数的回调函数的使用.如果这个概念理解不了,nodejs.express 的代码就会看得一塌糊涂.比如: app.use(function(req, res, next) {    var err = new Error('Not Found');    err.status = 404;    next(err);}); app是对象,use是方法,方

回调函数callback使用例子

代码如下: <!DOCTYPE HTML> <html> <head> <meta charset="GBK" /> <title>回调函数(callback)</title> <script type="text/javascript" src="http://cdn.bootcss.com/jquery/1.9.0/jquery.min.js"></sc

js回调函数(callback)

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://luxiao1223.blog.51cto.com/2369118/482885 Mark! js学习 不喜欢js,但是喜欢jquery,不解释. 自学jquery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速google之,发现原来中文翻译成回调.也就是回调函数了.不懂啊,于是在google回调函数,发现网上的中文解释实在是太"深奥"了,我承认自己

js回调函数(callback)理解

Mark! js学习 不喜欢js,但是喜欢jquery,不解释. 自学jquery的时候,看到一英文词(Callback),顿时背部隐隐冒冷汗.迅速google之,发现原来中文翻译成回调.也就是回调函数了.不懂啊,于是在google回调函数,发现网上的中文解释实在是太“深奥”了,我承认自己才疏学浅了.看了几个回调的例子后,貌似有点理解了.下面是我对回调函数的理解,要是理解错了,请指正,不甚感激. 首先还是从jquery网站上的英文定义入手,身为国人,我真感到悲剧.一个回调的定义被国内的“高手”解

php 回调函数(callback)

下面的内容是来自php.net 官方文档 callbacks call_user_func() ,usort()等函数接受用户自定义的回调函数作为参数. 回调函数可以不仅仅是简单的函数,也可以是对象方法,包括对象的静态方法. php 函数可以通过字符串类型的函数名进行调用,任何内建的或者用户自定义的函数都可以被使用.但是这其中不包括 php的语言结构,比如:array(),echo,empty(),eval(),exit(),isset(),list(),print,unset() 调用类的方法

javascript中的回调函数(callback) (转载)

代码如下: app.use(function(req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); app是对象,use是方法,方法的参数是一个带参的匿名函数,函数体直接在后面给出了.这段代码怎么理解呢?我们先来了解回调函数这个概念. 首先要了解,在 js 中,函数也是对象,可以赋值给变量,可以作为参数放在函数的参数列表中.比如: 代码如下: var doSomething = f