From: http://www.jb51.cc/article/p-xenprwqz-kt.html
最基本的层
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 inGameLayerend
最基本的定时器
[plain] view plain copy print ? localfunctiontick() end cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick,false)
local function tick() end cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick,false)
最基本的触摸事件
local touchBeginPoint = nil local function ontouchBegan(touch,event) local location = touch:getLocation() cclog("ontouchBegan: %0.2f,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,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,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)
最基本的音乐 [plain] view plain copy print ? --localbgMusicPath=CCfileUtils:getInstance():fullPathForfilename("background.ogg") localbgMusicPath=cc.fileUtils:getInstance():fullPathForfilename("background.mp3") cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath,true) localeffectPath=cc.fileUtils:getInstance():fullPathForfilename("effect1.wav") cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath) localfunctionmenuCallbackOpenPopup() --looptestsoundeffect localeffectPath=cc.fileUtils:getInstance():fullPathForfilename("effect1.wav") effectID=cc.SimpleAudioEngine:getInstance():playEffect(effectPath) menuPopup:setVisible(true) end
--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
最基本的加载图片 [plain] view plain copy print ? cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded) localtexture0=cc.Director:getInstance():getTextureCache():addImage("Images/grossini_dance_atlas.png") functionLoadingScene.imageLoaded(pObj) --body end cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testAlpha.png") cc.Director:getInstance():getTextureCache():removeAllTextures() cc.Director:getInstance():getTextureCache():removeUnusedTextures() localcache=cc.SpriteFrameCache:getInstance() cache:addSpriteFrames("animations/grossini_gray.pList","animations/grossini_gray.png") SpriteFrameTest.m_pSprite1=cc.Sprite:createWithSpriteFramename("grossini_dance_01.png")
cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded)local texture0 = cc.Director:getInstance():getTextureCache():addImage( "Images/grossini_dance_atlas.png")function LoadingScene.imageLoaded( pObj) -- bodyendcc.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() endlocal action = cc.Sequence:create( cc.MoveBy:create(2,cc.CallFunc:create(CallFucnCallback1) ) grossini:runAction(action)
最基础的字符格式化
string.format("grossini_dance_%02d.png",j + 1)
最基础的按钮
[plain] view plain copy print ? localstart=cc.Sprite:createWithSpriteFramename("start.png") localstartItem=cc.MenuItemSprite:create(start,start,start) localfunctionmenuCallback(sender) cclog("menuCallback...") --tolua.cast(ret:getParent(),"cc.LayerMultiplex"):switchTo(1) end startItem:registerScriptTapHandler(menuCallback) startItem:setposition(50,50) localmenu=cc.Menu:create() menu:addChild(startItem) menu:setposition(0,0) layer:addChild(menu) 总结
以上是内存溢出为你收集整理的初学cocos2dx lua全部内容,希望文章能够帮你解决初学cocos2dx lua所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)