(12) 初学cocos2dx lua

最基本的层

[plain] view plain copy
  1. functioncreateInGameLayer()
  2. localinGameLayer=cc.Layer:create()
  3. returninGameLayer
  4. end

最基本的场景

    localsceneGame=cc.Scene:create()
  1. sceneGame:addChild(createInGameLayer())
  2. cc.Director:getInstance():runWithScene(sceneGame)
  3. cc.Director:getInstance():replaceScene(cc.TransitionFade:create(1,WelcomeScene.createScene()))

最基本的精灵

    localbg=cc.Sprite:create("farm.jpg")
  1. bg:setAnchorPoint(0,0)
  2. inGameLayer:addChild(bg)
  3. returninGameLayer
  4. end

最基本的定时器
    localfunctiontick()
  1. end
  2. cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick,false)


最基本的触摸事件

    localtouchBeginPoint=nil
  1. localfunctiononTouchBegan(touch,event)
  2. locallocation=touch:getLocation()
  3. cclog("onTouchBegan:%0.2f,%0.2f",location.x,location.y)
  4. touchBeginPoint={x=location.x,y=location.y}
  5. --CCTOUCHBEGANeventmustreturntrue
  6. --[[多点
  7. fori=1,table.getn(touches)do
  8. locallocation=touches[i]:getLocation()
  9. Sprite1.addNewSpriteWithCoords(Helper.currentLayer,location)
  10. ]]--
  11. returntrue
  12. end
  13. localfunctiononTouchMoved(touch,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> cclog("onTouchMoved:%0.2f,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> iftouchBeginPointthen
  14. localcx,cy=layerFarm:getPosition()
  15. layerFarm:setPosition(cx+location.x-touchBeginPoint.x,
  16. cy+location.y-touchBeginPoint.y)
  17. localfunctiononTouchEnded(touch,event)
  18. locallocation=touch:getLocation()
  19. cclog("onTouchEnded:%0.2f,location.y)
  20. touchBeginPoint=nil
  21. spriteDog.isPaused=false
  22. locallistener=cc.EventListenerTouchOneByOne:create()
  23. --locallistener=cc.EventListenerTouchAllAtOnce:create()多点
  24. listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)
  25. listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED)
  26. listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)
  27. localeventdispatcher=layerFarm:getEventdispatcher()
  28. eventdispatcher:addEventListenerWithSceneGraPHPriority(listener,layerFarm)

最基本的音乐
    --localbgMusicPath=CCFileUtils:getInstance():fullPathForFilename("background.ogg")
  1. localbgMusicPath=cc.FileUtils:getInstance():fullPathForFilename("background.mp3")
  2. cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath,true)
  3. localeffectPath=cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
  4. cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)
  5. localfunctionmenuCallbackOpenPopup()
  6. --looptestsoundeffect
  7. effectID=cc.SimpleAudioEngine:getInstance():playEffect(effectPath)
  8. menuPopup:setVisible(true)
  9. 最基本的加载图片
      cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded)
    1. localtexture0=cc.Director:getInstance():getTextureCache():addImage("Images/grossini_dance_atlas.png")
    2. functionLoadingScene.imageLoaded(pObj)
    3. --body
    4. cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png")
    5. cc.Director:getInstance():getTextureCache():removeAllTextures()
    6. cc.Director:getInstance():getTextureCache():removeUnusedTextures()
    7. localcache=cc.SpriteFrameCache:getInstance()
    8. cache:addSpriteFrames("animations/grossini_gray.plist","animations/grossini_gray.png")
    9. SpriteFrameTest.m_pSprite1=cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")

    最基础的动作

      localfunctionCallFucnCallback1()
    1. localaction=cc.Sequence:create(
    2. cc.MoveBy:create(2,cc.p(200,0)),
    3. cc.CallFunc:create(CallFucnCallback1))
    4. grossini:runAction(action)

    最基础的字符格式化

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

    最基础的按钮
      localstart=cc.Sprite:createWithSpriteFrameName("start.png")
    1. localstartItem=cc.MenuItemSprite:create(start,start,start)
    2. localfunctionmenuCallback(sender)
    3. cclog("menuCallback...")
    4. --tolua.cast(ret:getParent(),"cc.LayerMultiplex"):switchTo(1)
    5. startItem:registerScriptTapHandler(menuCallback)
    6. startItem:setPosition(50,50)
    7. localmenu=cc.Menu:create()
    8. menu:addChild(startItem)
    9. menu:setPosition(0,0)
    10. layer:addChild(menu)

    相关文章

        本文实践自 RayWenderlich、Ali Hafizji 的文章《...
    Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
    第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
        Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
    1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
       Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...