Hello.lua注释
来源:互联网 发布:现在淘宝卖什么挣钱 编辑:程序博客网 时间:2024/06/01 10:46
这个注释cocos2d-x给我们写的hello.lua
-- for CCLuaEngine traceback 输出绑定执行函数发生错误的信息function __G__TRACKBACK__(msg) print("----------------------------------------") print("LUA ERROR: " .. tostring(msg) .. "\n") print(debug.traceback()) print("----------------------------------------")endlocal function main() -- avoid memory leak 设置脚本内存回收参数 避免内存泄露 collectgarbage("setpause", 100) collectgarbage("setstepmul", 5000) -- 就是local function cclog(...) 定义局部Log函数 local cclog = function(...) print(string.format(...)) end--类似c++的include,会检查是否重复引入 require "hello2" cclog("result is " .. myadd(3, 5)) ----------------- 读取可视的区域 local visibleSize = CCDirector:sharedDirector():getVisibleSize() local origin = CCDirector:sharedDirector():getVisibleOrigin() -- add the moving dog 创建一个小松鼠 local function creatDog() local frameWidth = 105 local frameHeight = 95 -- create dog animate local textureDog = CCTextureCache:sharedTextureCache():addImage("dog.png")-- 读取纹理的区域大小 local rect = CCRectMake(0, 0, frameWidth, frameHeight)-- 读取纹理 local frame0 = CCSpriteFrame:createWithTexture(textureDog, rect) rect = CCRectMake(frameWidth, 0, frameWidth, frameHeight) local frame1 = CCSpriteFrame:createWithTexture(textureDog, rect) local spriteDog = CCSprite:createWithSpriteFrame(frame0) spriteDog.isPaused = false spriteDog:setPosition(origin.x, origin.y + visibleSize.height / 4 * 3) local animFrames = CCArray:create() animFrames:addObject(frame0) animFrames:addObject(frame1) local animation = CCAnimation:createWithSpriteFrames(animFrames, 0.5) local animate = CCAnimate:create(animation);-- 初始状态是frame0 spriteDog:runAction(CCRepeatForever:create(animate)) -- moving dog at every frame。这个是来决定松鼠往右走 local function tick() if spriteDog.isPaused then return end local x, y = spriteDog:getPosition() if x > origin.x + visibleSize.width then x = origin.x else x = x + 1 end spriteDog:setPositionX(x) end-- 生成一个schedule,每帧执行tick函数 CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(tick, 0, false) return spriteDog end -- create farm local function createLayerFarm() local layerFarm = CCLayer:create() -- add in farm background 添加背景 local bg = CCSprite:create("farm.jpg") bg:setPosition(origin.x + visibleSize.width / 2 + 80, origin.y + visibleSize.height / 2) layerFarm:addChild(bg) -- add land sprite 添加草地 for i = 0, 3 do for j = 0, 1 do local spriteLand = CCSprite:create("land.png") spriteLand:setPosition(200 + j * 180 - i % 2 * 90, 10 + i * 95 / 2) layerFarm:addChild(spriteLand) end end -- add crop 添加植物 local frameCrop = CCSpriteFrame:create("crop.png", CCRectMake(0, 0, 105, 95)) for i = 0, 3 do for j = 0, 1 do local spriteCrop = CCSprite:createWithSpriteFrame(frameCrop); spriteCrop:setPosition(10 + 200 + j * 180 - i % 2 * 90, 30 + 10 + i * 95 / 2) layerFarm:addChild(spriteCrop) end end -- add moving dog 添加移动的狗 local spriteDog = creatDog() layerFarm:addChild(spriteDog) -- handing touch events 手指触摸事件的处理 local touchBeginPoint = nil-- 如果有安到 local function onTouchBegan(x, y) cclog("onTouchBegan: %0.2f, %0.2f", x, y) touchBeginPoint = {x = x, y = y} -- 记住点击的位置 spriteDog.isPaused = true --将松鼠暂停 -- CCTOUCHBEGAN event must return true 这里要return true,否则后面将无法运行 return true end local function onTouchMoved(x, y) -- 结束的位置减去初始的位置就是移动的位置 -- cclog("onTouchMoved: %0.2f, %0.2f", x, y) if touchBeginPoint then -- 这个变量是在上面声明的-- 将整个农场层拖动,因为之前已经将农场里面所有对象加入在layerFarm local cx, cy = layerFarm:getPosition() layerFarm:setPosition(cx + x - touchBeginPoint.x, cy + y - touchBeginPoint.y) touchBeginPoint = {x = x, y = y} end end-- 手指离开 local function onTouchEnded(x, y) cclog("onTouchEnded: %0.2f, %0.2f", x, y) touchBeginPoint = nil -- 数据清空 spriteDog.isPaused = false -- 松鼠显示出来 end local function onTouch(eventType, x, y) if eventType == CCTOUCHBEGAN then return onTouchBegan(x, y) elseif eventType == CCTOUCHMOVED then return onTouchMoved(x, y) else return onTouchEnded(x, y) end end-- 设置监听器 layerFarm:registerScriptTouchHandler(onTouch) layerFarm:setTouchEnabled(true) return layerFarm end -- create menu 建立目录 local function createLayerMenu() local layerMenu = CCLayer:create() local menuPopup, menuTools, effectID local function menuCallbackClosePopup() -- stop test sound effect 停止音乐 SimpleAudioEngine:sharedEngine():stopEffect(effectID) menuPopup:setVisible(false) end local function menuCallbackOpenPopup() -- loop test sound effect local effectPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("effect1.wav") effectID = SimpleAudioEngine:sharedEngine():playEffect(effectPath) menuPopup:setVisible(true) end -- add a popup menu 创建弹出的菜单面板 local menuPopupItem = CCMenuItemImage:create("menu2.png", "menu2.png") menuPopupItem:setPosition(0, 0) menuPopupItem:registerScriptTapHandler(menuCallbackClosePopup) menuPopup = CCMenu:createWithItem(menuPopupItem) menuPopup:setPosition(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2) menuPopup:setVisible(false) layerMenu:addChild(menuPopup) -- add the left-bottom "tools" menu to invoke menuPopup 这里是建立工具按钮 local menuToolsItem = CCMenuItemImage:create("menu1.png", "menu1.png") menuToolsItem:setPosition(0, 0) menuToolsItem:registerScriptTapHandler(menuCallbackOpenPopup) -- 响应函数 menuTools = CCMenu:createWithItem(menuToolsItem) local itemWidth = menuToolsItem:getContentSize().width local itemHeight = menuToolsItem:getContentSize().height menuTools:setPosition(origin.x + itemWidth/2, origin.y + itemHeight/2) layerMenu:addChild(menuTools) return layerMenu end -- play background music, preload effect -- uncomment below for the BlackBerry version -- local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("background.ogg") local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("background.mp3") SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true) local effectPath = CCFileUtils:sharedFileUtils():fullPathFromRelativePath("effect1.wav") SimpleAudioEngine:sharedEngine():preloadEffect(effectPath) -- run local sceneGame = CCScene:create() sceneGame:addChild(createLayerFarm()) sceneGame:addChild(createLayerMenu()) CCDirector:sharedDirector():runWithScene(sceneGame)endxpcall(main, __G__TRACKBACK__)--[[ xpcall( 调用函数, 错误捕获函数 ); lua提供了xpcall来捕获异常xpcall接受两个参数:调用函数、错误处理函数。 当错误发生时,Lua会在栈释放以前调用错误处理函数,因此可以使用debug库收集错误相关信息。两个常用的debug处理函数:debug.debug和debug.traceback 前者给出Lua的提示符,你可以自己动手察看错误发生时的情况;后者通过traceback创建更多的错误信息,也是控制台解释器用来构建错误信息的函数。--]]
- Hello.lua注释
- hello lua
- lua注释
- Lua注释
- [cocos2dx-lua]"Hello Lua"分析
- Hello world with LUA
- lua hello world
- hello-os注释
- 02.Hello World、注释
- lua的注释
- lua的注释
- LUA注释语法
- [cocos2dx-lua]"Hello Lua"分析
- redis-lua(一):Hello,World!
- cocos2d-x hello.lua解析
- 第一章,Hello World(Lua)
- Lua注释的解析方法
- lua源码注释1【转】
- Z-Stack 绑定中的原码补码反码小插曲
- asp.net Page_Load事件加载两次
- Java6 WebService @WebService
- android Volley自定义request的方法
- 枚举类型
- Hello.lua注释
- php:根据中文裁减字符串函数方法
- 基于51单片机-温度监控系统
- 仪表
- Oracle 培训笔记
- UITextView 添加placeholder
- 1-0组成的串中,1和0个数相等的最长子串
- “黑马程序员”宣传语有奖大征集《参赛作品》
- Telnet HTTP 测试步骤 解决返回400 bad Request