cocos2d-x-LuaProxy学习日志(1) -- Cocos2d-x HelloLua 介绍



最近网游使用lua做更新比较火,最近也有人问我能否写下lua的文章。刚新建了一个lua工程,看了下代码,就干脆先介绍下HelloLua吧。边看代码边写的注释,应该基本都写了。

[cpp]  view plain copy
  1. -- for CCLuaEngine traceback 输出绑定执行函数发生错误的信息  
  2. function __G__TRACKBACK__(msg)  
  3.  print("----------------------------------------")  
  4.  print("LUA ERROR: " .. tostring(msg) .. "\n")  
  5.  print(debug.traceback())  
  6.  print("----------------------------------------")  
  7. end  
  8.    
  9. local function main()  
  10.  -- avoid memory leak 设置脚本内存回收参数 避免内存泄露  
  11.  collectgarbage("setpause", 100)  
  12.  collectgarbage("setstepmul", 5000)  
  13.    
  14. -- 就是local function cclog(...) 定义局部Log函数  
  15.  local cclog = function(...)  
  16.  print(string.format(...))  
  17.  end  
  18.    
  19. -- 类似c++的include,会检查是否重复引入  
  20.  require "hello2"  
  21.    
  22. -- 调用外部函数,在hello2.lua中  
  23.  cclog("result is " .. myadd(3, 5))  
  24.    
  25. ---------------  
  26.    
  27. -- 获取可视区域  
  28.  local visibleSize = CCDirector:sharedDirector():getVisibleSize()  
  29.  -- 可视原点坐标 OpenGL坐标系 左下角为原点  
  30.  local origin = CCDirector:sharedDirector():getVisibleOrigin()  
  31.    
  32. -- add the moving dog 创建小松鼠  
  33.  local function creatDog()  
  34.    
  35. -- 每一帧尺寸设置,local表示局部变量  
  36.  local frameWidth = 105  
  37.  local frameHeight = 95  
  38.    
  39. -- create dog animate 加载动画资源并创建精灵帧  
  40.  -- 加载精灵动画所在纹理  
  41.  local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png")  
  42.  -- 设置第一帧帧区域  
  43.  local rect = CCRectMake(0, 0, frameWidth, frameHeight)  
  44.  -- 创建第一帧精灵Frame  
  45.  local frame0 = CCSpriteFrame:createWithTexture(textureDog, rect)  
  46.  -- 设置第二帧帧区域  
  47.  rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight)  
  48.  -- 创建第二帧精灵Frame  
  49.  local frame1 = CCSpriteFrame:createWithTexture(textureDog, rect)  
  50.    
  51. -- 基于使用第一帧Frame创建Sprite对象  
  52.  local spriteDog = CCSprite:createWithSpriteFrame(frame0)  
  53.  spriteDog.isPaused = false  
  54.  spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3)  
  55.    
  56. -- 将上面创建的两帧生成一个帧数组(这个松鼠一共就2帧)  
  57.  local animFrames = CCArray:create()  
  58.    
  59. animFrames:addObject(frame0)  
  60.  animFrames:addObject(frame1)  
  61.    
  62. -- 根据帧序列数组创建一个动画animation。帧间隔时间delay等于0.5秒  
  63.  local animation = CCAnimation:createWithSpriteFrames(animFrames, 0.5)  
  64.  -- 根据动画animation创建动作实例  
  65.  local animate = CCAnimate:create(animation);  
  66.  -- 松鼠精灵执行该动作  
  67.  spriteDog:runAction(CCRepeatForever:create(animate))  
  68.    
  69. -- moving dog at every frame 用来更新松鼠的位置,后面会调用该函数  
  70.  local function tick()  
  71.  if spriteDog.isPaused then return end  
  72.  local x, y = spriteDog:getPosition()  
  73.  if x > origin.x + visibleSize.width then  
  74.  x = origin.x  
  75.  else  
  76.  x = x + 1  
  77.  end  
  78.    
  79. spriteDog:setPositionX(x)  
  80.  end  
  81.    
  82. -- 生成一个schedule,每帧执行tick函数  
  83.  CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(tick, 0, false)  
  84.    
  85. return spriteDog  
  86.  end  
  87.    
  88. -- create farm 创建地面的农场  
  89.  local function createLayerFarm()  
  90.  -- 创建一个新的Layer用作农场管理  
  91.  local layerFarm = CCLayer:create()  
  92.    
  93. -- add in farm background 添加农场背景图  
  94.  local bg = CCSprite:create("farm.jpg")  
  95.  bg:setPosition(origin.x + visibleSize.width / 2 + 80, origin.y + visibleSize.height / 2)  
  96.  layerFarm:addChild(bg)  
  97.    
  98. -- add land sprite 添加地免砖块  
  99.  for i = 0, 3 do  
  100.  for j = 0, 1 do  
  101.  local spriteLand = CCSprite:create("land.png")  
  102.  spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2)  
  103.  layerFarm:addChild(spriteLand)  
  104.  end  
  105.  end  
  106.    
  107. -- add crop 添加庄稼,注意crop.png是多张图的合成贴图,所以只取了里面的部分贴图  
  108.  local frameCrop = CCSpriteFrame:create("crop.png", CCRectMake(0, 0, 105, 95))  
  109.  for i = 0, 3 do  
  110.  for j = 0, 1 do  
  111.  local spriteCrop = CCSprite:createWithSpriteFrame(frameCrop);  
  112.  spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2)  
  113.  layerFarm:addChild(spriteCrop)  
  114.  end  
  115.  end  
  116.    
  117. -- add moving dog 调用上面的creatDog()方法,创建一个移动的松鼠  
  118.  local spriteDog = creatDog()  
  119.  layerFarm:addChild(spriteDog)  
  120.    
  121. -- handing touch events 手指触摸事件处理  
  122.  local touchBeginPoint = nil  
  123.    
  124. -- 手指点击开始  
  125.  local function onTouchBegan(x, y)  
  126.  cclog("onTouchBegan: %0.2f, %0.2f", x, y)  
  127.  touchBeginPoint = {x = x, y = y} -- 保存点击位置  
  128.  spriteDog.isPaused = true -- 将松鼠暂停移动  
  129.  -- CCTOUCHBEGAN event must return true 这里必须返回ture,否则后续touch事件无法接收  
  130.  return true  
  131.  end  
  132.    
  133. -- 手指按住移动  
  134.  local function onTouchMoved(x, y)  
  135.  cclog("onTouchMoved: %0.2f, %0.2f", x, y)  
  136.  if touchBeginPoint then  
  137.  -- 将整个农场层拖动,因为之前已经将农场里面所有对象加入在layerFarm  
  138.  local cx, cy = layerFarm:getPosition()  
  139.  layerFarm:setPosition(cx + x - touchBeginPoint.x,  
  140.  cy + y - touchBeginPoint.y)  
  141.  touchBeginPoint = {x = x, y = y}  
  142.  end  
  143.  end  
  144.    
  145. -- 手指离开  
  146.  local function onTouchEnded(x, y)  
  147.  cclog("onTouchEnded: %0.2f, %0.2f", x, y)  
  148.  touchBeginPoint = nil -- 点击位置数据清空  
  149.  spriteDog.isPaused = false -- 回复松鼠移动  
  150.  end  
  151.    
  152. -- touch事件的接收函数  
  153.  local function onTouch(eventType, x, y)  
  154.  if eventType == "began" then  
  155.  return onTouchBegan(x, y)  
  156.  elseif eventType == "moved" then  
  157.  return onTouchMoved(x, y)  
  158.  else  
  159.  return onTouchEnded(x, y)  
  160.  end  
  161.  end  
  162.    
  163. -- 注册touch事件  
  164.  layerFarm:registerScriptTouchHandler(onTouch)  
  165.  layerFarm:setTouchEnabled(true)  
  166.    
  167. return layerFarm  
  168.  end  
  169.  -- create menu 创建界面菜单  
  170.  local function createLayerMenu()  
  171.  -- 创建一个新的CCLayer管理所有菜单  
  172.  local layerMenu = CCLayer:create()  
  173.    
  174. local menuPopup, menuTools, effectID  
  175.    
  176. -- 点击菜单回调函数  
  177.  local function menuCallbackClosePopup()  
  178.  -- stop test sound effect 关闭音效  
  179.  SimpleAudioEngine:sharedEngine():stopEffect(effectID)  
  180.  menuPopup:setVisible(false) -- 隐藏菜单  
  181.  end  
  182.    
  183. -- 点击菜单回调函数  
  184.  local function menuCallbackOpenPopup()  
  185.  -- loop test sound effect 打开音效  
  186.  local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav")  
  187.  effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath)  
  188.  menuPopup:setVisible(true) -- 显示菜单  
  189.  end  
  190.    
  191. -- add a popup menu 创建弹出的菜单面板  
  192.  local menuPopupItem = CCMenuItemImage:create("menu2.png""menu2.png")  
  193.  menuPopupItem:setPosition(0, 0)  
  194.  menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup) -- 注册点击回调地址  
  195.  menuPopup = CCMenu:createWithItem(menuPopupItem)  
  196.  menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)  
  197.  menuPopup:setVisible(false)  
  198.  layerMenu:addChild(menuPopup)  
  199.    
  200. -- add the left-bottom "tools" menu to invoke menuPopup 左下角的工具按钮,用来弹出菜单面板  
  201.  local menuToolsItem = CCMenuItemImage:create("menu1.png""menu1.png")  
  202.  menuToolsItem:setPosition(0, 0)  
  203.  menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup) -- 注册点击回调地址  
  204.  menuTools = CCMenu:createWithItem(menuToolsItem)  
  205.  local itemWidth = menuToolsItem:getContentSize().width  
  206.  local itemHeight = menuToolsItem:getContentSize().height  
  207.  menuTools:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2)  
  208.  layerMenu:addChild(menuTools)  
  209.    
  210. return layerMenu  
  211.  end  
  212.    
  213. -- play background music, preload effect  
  214.    
  215. -- uncomment below for the BlackBerry version  
  216.  -- local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.ogg")  
  217.  local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.mp3")  
  218.  SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true) -- 播放背景音乐  
  219.  local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav")  
  220.  SimpleAudioEngine:sharedEngine():preloadEffect(effectPath) -- 预加载音效  
  221.    
  222. -- run  
  223.  local sceneGame = CCScene:create() -- 创建场景  
  224.  sceneGame:addChild(createLayerFarm()) -- 将农场层加入场景  
  225.  sceneGame:addChild(createLayerMenu()) -- 将菜单界面层加入场景  
  226.  CCDirector:sharedDirector():runWithScene(sceneGame)  
  227. end  
  228.    
  229. --[[  
  230. xpcall( 调用函数, 错误捕获函数 );  
  231. lua提供了xpcall来捕获异常  
  232. xpcall接受两个参数:调用函数、错误处理函数。  
  233. 当错误发生时,Lua会在栈释放以前调用错误处理函数,因此可以使用debug库收集错误相关信息。  
  234. 两个常用的debug处理函数:debug.debug和debug.traceback  
  235. 前者给出Lua的提示符,你可以自己动手察看错误发生时的情况;  
  236. 后者通过traceback创建更多的错误信息,也是控制台解释器用来构建错误信息的函数。  
  237. --]]  
  238. xpcall(main, __G__TRACKBACK__)  
写得还算详细的,后面会慢慢介绍。

原文地址: http://www.cocos2dev.com/?p=393

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值