分享

Cocos2d

 爱蓝斯 2014-01-05

Cocos2d-x HelloLua 介绍

猴子|2013-09-09 13:24|1769次浏览| cocos2d-x(80)lua(2)网游(1) 0

21

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

  1.  
  2. -- for CCLuaEngine traceback 输出绑定执行函数发生错误的信息 
  3.  
  4. function __G__TRACKBACK__(msg) 
  5.  
  6.  print("----------------------------------------") 
  7.  
  8.  print("LUA ERROR: " .. tostring(msg) .. "\n") 
  9.  
  10.  print(debug.traceback()) 
  11.  
  12.  print("----------------------------------------") 
  13.  
  14. end 
  15.  
  16.   
  17.  
  18. local function main() 
  19.  
  20.  -- avoid memory leak 设置脚本内存回收参数 避免内存泄露 
  21.  
  22.  collectgarbage("setpause", 100) 
  23.  
  24.  collectgarbage("setstepmul", 5000) 
  25.  
  26.   
  27.  
  28. -- 就是local function cclog(...) 定义局部Log函数 
  29.  
  30.  local cclog = function(...) 
  31.  
  32.  print(string.format(...)) 
  33.  
  34.  end 
  35.  
  36.   
  37.  
  38. -- 类似c++的include,会检查是否重复引入 
  39.  
  40.  require "hello2" 
  41.  
  42.   
  43.  
  44. -- 调用外部函数,在hello2.lua中 
  45.  
  46.  cclog("result is " .. myadd(3, 5)) 
  47.  
  48.   
  49.  
  50. --------------- 
  51.  
  52.   
  53.  
  54. -- 获取可视区域 
  55.  
  56.  local visibleSize = CCDirector:sharedDirector():getVisibleSize() 
  57.  
  58.  -- 可视原点坐标 OpenGL坐标系 左下角为原点 
  59.  
  60.  local origin = CCDirector:sharedDirector():getVisibleOrigin() 
  61.  
  62.   
  63.  
  64. -- add the moving dog 创建小松鼠 
  65.  
  66.  local function creatDog() 
  67.  
  68.   
  69.  
  70. -- 每一帧尺寸设置,local表示局部变量 
  71.  
  72.  local frameWidth = 105 
  73.  
  74.  local frameHeight = 95 
  75.  
  76.   
  77.  
  78. -- create dog animate 加载动画资源并创建精灵帧 
  79.  
  80.  -- 加载精灵动画所在纹理 
  81.  
  82.  local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png") 
  83.  
  84.  -- 设置第一帧帧区域 
  85.  
  86.  local rect = CCRectMake(0, 0, frameWidth, frameHeight) 
  87.  
  88.  -- 创建第一帧精灵Frame 
  89.  
  90.  local frame0 = CCSpriteFrame:createWithTexture(textureDog, rect) 
  91.  
  92.  -- 设置第二帧帧区域 
  93.  
  94.  rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight) 
  95.  
  96.  -- 创建第二帧精灵Frame 
  97.  
  98.  local frame1 = CCSpriteFrame:createWithTexture(textureDog, rect) 
  99.  
  100.   
  101.  
  102. -- 基于使用第一帧Frame创建Sprite对象 
  103.  
  104.  local spriteDog = CCSprite:createWithSpriteFrame(frame0) 
  105.  
  106.  spriteDog.isPaused = false 
  107.  
  108.  spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3) 
  109.  
  110.   
  111.  
  112. -- 将上面创建的两帧生成一个帧数组(这个松鼠一共就2帧) 
  113.  
  114.  local animFrames = CCArray:create() 
  115.  
  116.   
  117.  
  118. animFrames:addObject(frame0) 
  119.  
  120.  animFrames:addObject(frame1) 
  121.  
  122.   
  123.  
  124. -- 根据帧序列数组创建一个动画animation。帧间隔时间delay等于0.5秒 
  125.  
  126.  local animation = CCAnimation:createWithSpriteFrames(animFrames, 0.5) 
  127.  
  128.  -- 根据动画animation创建动作实例 
  129.  
  130.  local animate = CCAnimate:create(animation); 
  131.  
  132.  -- 松鼠精灵执行该动作 
  133.  
  134.  spriteDog:runAction(CCRepeatForever:create(animate)) 
  135.  
  136.   
  137.  
  138. -- moving dog at every frame 用来更新松鼠的位置,后面会调用该函数 
  139.  
  140.  local function tick() 
  141.  
  142.  if spriteDog.isPaused then return end 
  143.  
  144.  local x, y = spriteDog:getPosition() 
  145.  
  146.  if x > origin.x + visibleSize.width then 
  147.  
  148.  x = origin.x 
  149.  
  150.  else 
  151.  
  152.  xx = x + 1 
  153.  
  154.  end 
  155.  
  156.   
  157.  
  158. spriteDog:setPositionX(x) 
  159.  
  160.  end 
  161.  
  162.   
  163.  
  164. -- 生成一个schedule,每帧执行tick函数 
  165.  
  166.  CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(tick, 0, false) 
  167.  
  168.   
  169.  
  170. return spriteDog 
  171.  
  172.  end 
  173.  
  174.   
  175.  
  176. -- create farm 创建地面的农场 
  177.  
  178.  local function createLayerFarm() 
  179.  
  180.  -- 创建一个新的Layer用作农场管理 
  181.  
  182.  local layerFarm = CCLayer:create() 
  183.  
  184.   
  185.  
  186. -- add in farm background 添加农场背景图 
  187.  
  188.  local bg = CCSprite:create("farm.jpg") 
  189.  
  190.  bg:setPosition(origin.x + visibleSize.width / 2 + 80, origin.y + visibleSize.height / 2) 
  191.  
  192.  layerFarm:addChild(bg) 
  193.  
  194.   
  195.  
  196. -- add land sprite 添加地免砖块 
  197.  
  198.  for i = 0, 3 do 
  199.  
  200.  for j = 0, 1 do 
  201.  
  202.  local spriteLand = CCSprite:create("land.png") 
  203.  
  204.  spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2) 
  205.  
  206.  layerFarm:addChild(spriteLand) 
  207.  
  208.  end 
  209.  
  210.  end 
  211.  
  212.   
  213.  
  214. -- add crop 添加庄稼,注意crop.png是多张图的合成贴图,所以只取了里面的部分贴图 
  215.  
  216.  local frameCrop = CCSpriteFrame:create("crop.png", CCRectMake(0, 0, 105, 95)) 
  217.  
  218.  for i = 0, 3 do 
  219.  
  220.  for j = 0, 1 do 
  221.  
  222.  local spriteCrop = CCSprite:createWithSpriteFrame(frameCrop); 
  223.  
  224.  spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2) 
  225.  
  226.  layerFarm:addChild(spriteCrop) 
  227.  
  228.  end 
  229.  
  230.  end 
  231.  
  232.   
  233.  
  234. -- add moving dog 调用上面的creatDog()方法,创建一个移动的松鼠 
  235.  
  236.  local spriteDog = creatDog() 
  237.  
  238.  layerFarm:addChild(spriteDog) 
  239.  
  240.   
  241.  
  242. -- handing touch events 手指触摸事件处理 
  243.  
  244.  local touchBeginPoint = nil 
  245.  
  246.   
  247.  
  248. -- 手指点击开始 
  249.  
  250.  local function onTouchBegan(x, y) 
  251.  
  252.  cclog("onTouchBegan: %0.2f, %0.2f", x, y) 
  253.  
  254.  touchBeginPoint = {xx = x, yy = y} -- 保存点击位置 
  255.  
  256.  spriteDog.isPaused = true -- 将松鼠暂停移动 
  257.  
  258.  -- CCTOUCHBEGAN event must return true 这里必须返回ture,否则后续touch事件无法接收 
  259.  
  260.  return true 
  261.  
  262.  end 
  263.  
  264.   
  265.  
  266. -- 手指按住移动 
  267.  
  268.  local function onTouchMoved(x, y) 
  269.  
  270.  cclog("onTouchMoved: %0.2f, %0.2f", x, y) 
  271.  
  272.  if touchBeginPoint then 
  273.  
  274.  -- 将整个农场层拖动,因为之前已经将农场里面所有对象加入在layerFarm 
  275.  
  276.  local cx, cy = layerFarm:getPosition() 
  277.  
  278.  layerFarm:setPosition(cx + x - touchBeginPoint.x, 
  279.  
  280.  cy + y - touchBeginPoint.y) 
  281.  
  282.  touchBeginPoint = {xx = x, yy = y} 
  283.  
  284.  end 
  285.  
  286.  end 
  287.  
  288.   
  289.  
  290. -- 手指离开 
  291.  
  292.  local function onTouchEnded(x, y) 
  293.  
  294.  cclog("onTouchEnded: %0.2f, %0.2f", x, y) 
  295.  
  296.  touchBeginPoint = nil -- 点击位置数据清空 
  297.  
  298.  spriteDog.isPaused = false -- 回复松鼠移动 
  299.  
  300.  end 
  301.  
  302.   
  303.  
  304. -- touch事件的接收函数 
  305.  
  306.  local function onTouch(eventType, x, y) 
  307.  
  308.  if eventType == "began" then 
  309.  
  310.  return onTouchBegan(x, y) 
  311.  
  312.  elseif eventType == "moved" then 
  313.  
  314.  return onTouchMoved(x, y) 
  315.  
  316.  else 
  317.  
  318.  return onTouchEnded(x, y) 
  319.  
  320.  end 
  321.  
  322.  end 
  323.  
  324.   
  325.  
  326. -- 注册touch事件 
  327.  
  328.  layerFarm:registerScriptTouchHandler(onTouch) 
  329.  
  330.  layerFarm:setTouchEnabled(true) 
  331.  
  332.   
  333.  
  334. return layerFarm 
  335.  
  336.  end 
  337.  
  338.  -- create menu 创建界面菜单 
  339.  
  340.  local function createLayerMenu() 
  341.  
  342.  -- 创建一个新的CCLayer管理所有菜单 
  343.  
  344.  local layerMenu = CCLayer:create() 
  345.  
  346.   
  347.  
  348. local menuPopup, menuTools, effectID 
  349.  
  350.   
  351.  
  352. -- 点击菜单回调函数 
  353.  
  354.  local function menuCallbackClosePopup() 
  355.  
  356.  -- stop test sound effect 关闭音效 
  357.  
  358.  SimpleAudioEngine:sharedEngine():stopEffect(effectID) 
  359.  
  360.  menuPopup:setVisible(false) -- 隐藏菜单 
  361.  
  362.  end 
  363.  
  364.   
  365.  
  366. -- 点击菜单回调函数 
  367.  
  368.  local function menuCallbackOpenPopup() 
  369.  
  370.  -- loop test sound effect 打开音效 
  371.  
  372.  local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav") 
  373.  
  374.  effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath) 
  375.  
  376.  menuPopup:setVisible(true) -- 显示菜单 
  377.  
  378.  end 
  379.  
  380.   
  381.  
  382. -- add a popup menu 创建弹出的菜单面板 
  383.  
  384.  local menuPopupItem = CCMenuItemImage:create("menu2.png", "menu2.png") 
  385.  
  386.  menuPopupItem:setPosition(0, 0) 
  387.  
  388.  menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup) -- 注册点击回调地址 
  389.  
  390.  menuPopup = CCMenu:createWithItem(menuPopupItem) 
  391.  
  392.  menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2) 
  393.  
  394.  menuPopup:setVisible(false) 
  395.  
  396.  layerMenu:addChild(menuPopup) 
  397.  
  398.   
  399.  
  400. -- add the left-bottom "tools" menu to invoke menuPopup 左下角的工具按钮,用来弹出菜单面板 
  401.  
  402.  local menuToolsItem = CCMenuItemImage:create("menu1.png", "menu1.png") 
  403.  
  404.  menuToolsItem:setPosition(0, 0) 
  405.  
  406.  menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup) -- 注册点击回调地址 
  407.  
  408.  menuTools = CCMenu:createWithItem(menuToolsItem) 
  409.  
  410.  local itemWidth = menuToolsItem:getContentSize().width 
  411.  
  412.  local itemHeight = menuToolsItem:getContentSize().height 
  413.  
  414.  menuTools:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2) 
  415.  
  416.  layerMenu:addChild(menuTools) 
  417.  
  418.   
  419.  
  420. return layerMenu 
  421.  
  422.  end 
  423.  
  424.   
  425.  
  426. -- play background music, preload effect 
  427.  
  428.   
  429.  
  430. -- uncomment below for the BlackBerry version 
  431.  
  432.  -- local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.ogg") 
  433.  
  434.  local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.mp3") 
  435.  
  436.  SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true) -- 播放背景音乐 
  437.  
  438.  local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav") 
  439.  
  440.  SimpleAudioEngine:sharedEngine():preloadEffect(effectPath) -- 预加载音效 
  441.  
  442.   
  443.  
  444. -- run 
  445.  
  446.  local sceneGame = CCScene:create() -- 创建场景 
  447.  
  448.  sceneGame:addChild(createLayerFarm()) -- 将农场层加入场景 
  449.  
  450.  sceneGame:addChild(createLayerMenu()) -- 将菜单界面层加入场景 
  451.  
  452.  CCDirector:sharedDirector():runWithScene(sceneGame) 
  453.  
  454. end 
  455.  
  456.   
  457.  
  458. --[[ 
  459.  
  460. xpcall( 调用函数, 错误捕获函数 ); 
  461.  
  462. lua提供了xpcall来捕获异常 
  463.  
  464. xpcall接受两个参数:调用函数、错误处理函数。 
  465.  
  466. 当错误发生时,Lua会在栈释放以前调用错误处理函数,因此可以使用debug库收集错误相关信息。 
  467.  
  468. 两个常用的debug处理函数:debug.debug和debug.traceback 
  469.  
  470. 前者给出Lua的提示符,你可以自己动手察看错误发生时的情况; 
  471.  
  472. 后者通过traceback创建更多的错误信息,也是控制台解释器用来构建错误信息的函数。 
  473.  
  474. --]] 
  475.  
  476. xpcall(main, __G__TRACKBACK__) 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多