初学cocos2dx lua

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

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

最基本的层

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

最基本的场景

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

最基本的精灵

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

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


最基本的触摸事件

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

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

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

最基础的动作

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

最基础的字符格式化

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

最基础的按钮
  1. localstart=cc.Sprite:createWithSpriteFrameName("start.png")
  2. localstartItem=cc.MenuItemSprite:create(start,start,start)
  3. localfunctionmenuCallback(sender)
  4. cclog("menuCallback...")
  5. --tolua.cast(ret:getParent(),"cc.LayerMultiplex"):switchTo(1)
  6. end
  7. startItem:registerScriptTapHandler(menuCallback)
  8. startItem:setPosition(50,50)
  9. localmenu=cc.Menu:create()
  10. menu:addChild(startItem)
  11. menu:setPosition(0,0)
  12. layer:addChild(menu)
原文链接:https://www.f2er.com/cocos2dx/346272.html

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