码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-04-19 17:47:47      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:

(本文使用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.

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

标签:

原文地址:http://www.cnblogs.com/reyn-soulate/p/4439424.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!