初学cocos2dx lua

最基本的层

function createInGameLayer() 
        local inGameLayer = cc.Layer:create()
        return inGameLayer
    end

最基本的场景

 local sceneGame = cc.Scene:create()
    sceneGame:addChild(createInGameLayer())

cc.Director:getInstance():runWithScene(sceneGame)
cc.Director:getInstance():replaceScene(cc.TransitionFade:create(1,WelcomeScene.createScene()))

最基本的精灵

function createInGameLayer() 
        local inGameLayer = cc.Layer:create()
        local bg = cc.Sprite:create("farm.jpg")
        bg:setAnchorPoint(0,0)
        inGameLayer:addChild(bg)
        return inGameLayer
end

最基本的定时器
 local function tick()
           
        end

        cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)


最基本的触摸事件

local touchBeginPoint = nil
        local function onTouchBegan(touch, event)
            local location = touch:getLocation()
            cclog("onTouchBegan: %0.2f, %0.2f", location.x, location.y)
            touchBeginPoint = {x = location.x, y = location.y}
            -- CCTOUCHBEGAN event must return true
            --[[多点
             for i = 1,table.getn(touches) do
             local location = touches[i]:getLocation()
             Sprite1.addNewSpriteWithCoords(Helper.currentLayer, location)
             end
            ]]--
            return true
        end

        local function onTouchMoved(touch, event)
            local location = touch:getLocation()
            cclog("onTouchMoved: %0.2f, %0.2f", location.x, location.y)
            if touchBeginPoint then
                local cx, cy = layerFarm:getPosition()
                layerFarm:setPosition(cx + location.x - touchBeginPoint.x,
                                      cy + location.y - touchBeginPoint.y)
                touchBeginPoint = {x = location.x, y = location.y}
            end
        end

        local function onTouchEnded(touch, event)
            local location = touch:getLocation()
            cclog("onTouchEnded: %0.2f, %0.2f", location.x, location.y)
            touchBeginPoint = nil
            spriteDog.isPaused = false
        end

        local listener = cc.EventListenerTouchOneByOne:create()
        --local listener = cc.EventListenerTouchAllAtOnce:create() 多点
        listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
        listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
        listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
        local eventDispatcher = layerFarm:getEventDispatcher()
        eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layerFarm)

最基本的音乐
 --local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.ogg")

    local bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("background.mp3")

    cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath, true)
    local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")

    cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)

    local function menuCallbackOpenPopup()
            -- loop test sound effect
            local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
            effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath)
            menuPopup:setVisible(true)
        end

最基本的加载图片
cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded)
local texture0 = cc.Director:getInstance():getTextureCache():addImage( "Images/grossini_dance_atlas.png")

function LoadingScene.imageLoaded( pObj)
	-- body
end

cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png")
cc.Director:getInstance():getTextureCache():removeAllTextures()
cc.Director:getInstance():getTextureCache():removeUnusedTextures()

local cache = cc.SpriteFrameCache:getInstance()
cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png")
SpriteFrameTest.m_pSprite1 = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")

最基础的动作

local function CallFucnCallback1()
	
end

local action = cc.Sequence:create(
        cc.MoveBy:create(2, cc.p(200,0)),
        cc.CallFunc:create(CallFucnCallback1) )
 grossini:runAction(action)


最基础的字符格式化

string.format("grossini_dance_%02d.png", j + 1)

最基础的按钮
local start = cc.Sprite:createWithSpriteFrameName("start.png")


local  startItem = cc.MenuItemSprite:create(start, start, start)


local function menuCallback(sender)
    cclog("menuCallback...")
    --tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(1)
end


startItem:registerScriptTapHandler(menuCallback)
startItem:setPosition(50,50)


local  menu = cc.Menu:create()
menu:addChild(startItem)
menu:setPosition(0,0)
layer:addChild(menu)



  • 1
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值