cocos2d-x hello.lua解析

--[[

     先说明  lua两个--相当于两个//,注释的意思

  基本上调用的cocos2dx函数和类的时候就是以cocos2d.*这样子来用

]]--


-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)                                                            --  //函数  或者是 宏?
    print("----------------------------------------")
    print("LUA ERROR: " .. tostring(msg) .. "\n")
    print(debug.traceback())
    print("----------------------------------------")
end

local function main()                                                                                             --  //局部函数 main函数开始
    -- avoid memory leak
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)

    local cclog = function(...)
        print(string.format(...))
    end

    require "hello2"                                                                                                --    //包含脚本 hello2
    cclog("result is " .. myadd(3, 5))                                   -- 打印log信息  ,myadd()是hello2.lua中的函数                              
                                                                        
                   

    ---------------

    local visibleSize = CCDirector:sharedDirector():getVisibleSize()            --  局部变量  获取屏幕大小                                                  

    local origin = CCDirector:sharedDirector():getVisibleOrigin()                 --   获取屏幕的原点

    -- add the moving dog                                                --  添加精灵 小松鼠 ,  明明是小松鼠来的
    local function creatDog()                                         --   建立小松鼠函数                                createDog函数开始
        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                                                                   -- 全局变量 bool值
        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)          --     建立帧动作 0.5s切换帧
        local animate = CCAnimate:create(animation);
        spriteDog:runAction(CCRepeatForever:create(animate))

        -- moving dog at every frame                                               -- 建立动作函数
        local function tick()                                                        --                    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                                                                                     --                     tick函数结束

        CCDirector:sharedDirector():getScheduler():scheduleScriptFunc(tick, 0, false)    --  更新 调动tick函数 
        return spriteDog                                                           --返回精灵                       
                                               
                                                     
    end                                                                      --                                            createDog函数结束

    -- create farm
    local function createLayerFarm()                                                         --                                           建立函数 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
        end

        local function onTouchMoved(x, y)
            -- cclog("onTouchMoved: %0.2f, %0.2f", x, y)
            if touchBeginPoint then
                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                                                                                                           --     createLayer函数结束
    end                                                                                                       


    -- create menu
    local function createLayerMenu()                                                                                                          --  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():fullPathForFilename("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                                                                                                                                                                                    -- createLayerMenu函数结束

    -- play background music, preload effect

    -- uncomment below for the BlackBerry version
    -- local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.ogg")
    local bgMusicPath = CCFileUtils:sharedFileUtils():fullPathForFilename("background.mp3")           ---  预加载音乐
    SimpleAudioEngine:sharedEngine():playBackgroundMusic(bgMusicPath, true)
    local effectPath = CCFileUtils:sharedFileUtils():fullPathForFilename("effect1.wav")
    SimpleAudioEngine:sharedEngine():preloadEffect(effectPath)

    -- run
    local sceneGame = CCScene:create()                                                                                                  --  建立场景
    sceneGame:addChild(createLayerFarm())                                                                                           -- 在场景中添加层
    sceneGame:addChild(createLayerMenu())                                                                                           -- 在场景中添加层菜单
    CCDirector:sharedDirector():runWithScene(sceneGame)                                                                 -- 运行场景
end                                                                                                                                                                                                                           -- main函数结束

xpcall(main, __G__TRACKBACK__)


--[[

功能:与pcall类似,在保护模式下调用函数(即发生的错误将不会反射给调用者)

  但可指定一个新的错误处理函数句柄

  当调用函数成功能返回true,失败时将返回false加err返回的结果

]]--

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值