初学cocos2dx lua

前端之家收集整理的这篇文章主要介绍了初学cocos2dx lua前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

From: http://www.jb51.cc/article/p-xenprwqz-kt.html

最基本的层

  1. functioncreateInGameLayer()@H_404_17@@H_404_17@
  2. localinGameLayer=cc.Layer:create()@H_404_17@
  3. returninGameLayer@H_404_17@
  4. end@H_404_17@

最基本的场景

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

最基本的精灵

  1. functioncreateInGameLayer()@H_404_17@@H_404_17@
  2. localinGameLayer=cc.Layer:create()@H_404_17@
  3. localbg=cc.Sprite:create("farm.jpg")@H_404_17@
  4. bg:setAnchorPoint(0,0)@H_404_17@
  5. inGameLayer:addChild(bg)@H_404_17@
  6. returninGameLayer@H_404_17@
  7. end@H_404_17@

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


最基本的触摸事件

  1. localtouchBeginPoint=nil@H_404_17@@H_404_17@
  2. localfunctiononTouchBegan(touch,event)@H_404_17@
  3. locallocation=touch:getLocation()@H_404_17@
  4. cclog("onTouchBegan:%0.2f,%0.2f",location.x,location.y)@H_404_17@
  5. touchBeginPoint={x=location.x,y=location.y}@H_404_17@
  6. --CCTOUCHBEGANeventmustreturntrue@H_404_17@
  7. --[[多点@H_404_17@
  8. fori=1,table.getn(touches)do@H_404_17@
  9. locallocation=touches[i]:getLocation()@H_404_17@
  10. Sprite1.addNewSpriteWithCoords(Helper.currentLayer,location)@H_404_17@
  11. end@H_404_17@
  12. ]]--@H_404_17@
  13. returntrue@H_404_17@
  14. end@H_404_17@
  15. @H_404_17@
  16. localfunctiononTouchMoved(touch,event)@H_404_17@
  17. locallocation=touch:getLocation()@H_404_17@
  18. cclog("onTouchMoved:%0.2f,location.y)@H_404_17@
  19. iftouchBeginPointthen@H_404_17@
  20. localcx,cy=layerFarm:getPosition()@H_404_17@
  21. layerFarm:setPosition(cx+location.x-touchBeginPoint.x,@H_404_17@
  22. cy+location.y-touchBeginPoint.y)@H_404_17@
  23. touchBeginPoint={x=location.x,y=location.y}@H_404_17@
  24. end@H_404_17@
  25. end@H_404_17@
  26. @H_404_17@
  27. localfunctiononTouchEnded(touch,event)@H_404_17@
  28. locallocation=touch:getLocation()@H_404_17@
  29. cclog("onTouchEnded:%0.2f,location.y)@H_404_17@
  30. touchBeginPoint=nil@H_404_17@
  31. spriteDog.isPaused=false@H_404_17@
  32. end@H_404_17@
  33. @H_404_17@
  34. locallistener=cc.EventListenerTouchOneByOne:create()@H_404_17@
  35. --locallistener=cc.EventListenerTouchAllAtOnce:create()多点@H_404_17@
  36. listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN)@H_404_17@
  37. listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED)@H_404_17@
  38. listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED)@H_404_17@
  39. localeventDispatcher=layerFarm:getEventDispatcher()@H_404_17@
  40. eventDispatcher:addEventListenerWithSceneGraPHPriority(listener,layerFarm)@H_404_17@

最基本的音乐
  1. --localbgMusicPath=CCFileUtils:getInstance():fullPathForFilename("background.ogg")@H_404_17@@H_404_17@
  2. @H_404_17@
  3. localbgMusicPath=cc.FileUtils:getInstance():fullPathForFilename("background.mp3")@H_404_17@
  4. @H_404_17@
  5. cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath,true)@H_404_17@
  6. localeffectPath=cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")@H_404_17@
  7. @H_404_17@
  8. cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)@H_404_17@
  9. @H_404_17@
  10. localfunctionmenuCallbackOpenPopup()@H_404_17@
  11. --looptestsoundeffect@H_404_17@
  12. localeffectPath=cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")@H_404_17@
  13. effectID=cc.SimpleAudioEngine:getInstance():playEffect(effectPath)@H_404_17@
  14. menuPopup:setVisible(true)@H_404_17@
  15. end@H_404_17@

最基本的加载图片
  1. cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded)@H_404_17@@H_404_17@
  2. localtexture0=cc.Director:getInstance():getTextureCache():addImage("Images/grossini_dance_atlas.png")@H_404_17@
  3. @H_404_17@
  4. functionLoadingScene.imageLoaded(pObj)@H_404_17@
  5. --body@H_404_17@
  6. end@H_404_17@
  7. @H_404_17@
  8. cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png")@H_404_17@
  9. cc.Director:getInstance():getTextureCache():removeAllTextures()@H_404_17@
  10. cc.Director:getInstance():getTextureCache():removeUnusedTextures()@H_404_17@
  11. @H_404_17@
  12. localcache=cc.SpriteFrameCache:getInstance()@H_404_17@
  13. cache:addSpriteFrames("animations/grossini_gray.plist","animations/grossini_gray.png")@H_404_17@
  14. SpriteFrameTest.m_pSprite1=cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")@H_404_17@

最基础的动作

  1. localfunctionCallFucnCallback1()@H_404_17@@H_404_17@
  2. @H_404_17@
  3. end@H_404_17@
  4. @H_404_17@
  5. localaction=cc.Sequence:create(@H_404_17@
  6. cc.MoveBy:create(2,cc.p(200,0)),@H_404_17@
  7. cc.CallFunc:create(CallFucnCallback1))@H_404_17@
  8. grossini:runAction(action)@H_404_17@

最基础的字符格式化

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

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

猜你在找的Cocos2d-x相关文章