Cocos2d-x 3.2 Lua樣本CocosDenshionTest(音頻測試),cocos2dx3.2lua

來源:互聯網
上載者:User

Cocos2d-x 3.2 Lua樣本CocosDenshionTest(音頻測試),cocos2dx3.2lua
Cocos2d-x 3.2 Lua樣本CocosDenshionTest(音頻測試)

 本篇部落格介紹Cocos2d-x 3.2中Lua樣本的音頻測試,Cocos2d-x使用SimpleAudioEngine這個類來實現音訊控制,比如播放、暫停、停止等操作。Lua代碼中,使用的是AudioEngine,具體實現可以參考AudioEngine.lua檔案,只是把SimpleAudioEngin進行了封裝。

範例程式碼:

--[[CocosDenshionTest.luaCocos2d-x 音頻支援]]--require "AudioEngine"local EFFECT_FILE = "effect1.wav" local MUSIC_FILE = nil-- 擷取目標平台local targetPlatform = cc.Application:getInstance():getTargetPlatform()-- iphone或者ipadif (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) then  MUSIC_FILE = "background.caf" -- caf格式else  MUSIC_FILE = "background.mp3" -- mp3格式endlocal LINE_SPACE = 40 local function CocosDenshionTest()  local ret = cc.Layer:create()  local m_pItmeMenu = nil  local m_tBeginPos = cc.p(0, 0)  local m_nSoundId = 0  -- 測試功能表項目  local testItems = {    "play background music",    "stop background music",    "pause background music",    "resume background music",    "rewind background music",    "is background music playing",    "play effect",    "play effect repeatly",    "stop effect",    "unload effect",    "add background music volume",    "sub background music volume",    "add effects volume",    "sub effects volume",    "pause effect",    "resume effect",    "pause all effects",    "resume all effects",    "stop all effects"  }  -- 菜單回調方法  local function menuCallback(tag, pMenuItem)    local nIdx = pMenuItem:getLocalZOrder() - 10000    -- play background music    if nIdx ==  0 then      AudioEngine.playMusic(MUSIC_FILE, true) -- 播放音樂    elseif nIdx == 1 then      -- stop background music      AudioEngine.stopMusic()  -- 停止背景音樂    elseif nIdx == 2 then      -- pause background music      AudioEngine.pauseMusic() -- 暫停音樂    elseif nIdx == 3 then      -- resume background music      AudioEngine.resumeMusic() -- 繼續播放音樂      -- rewind background music    elseif nIdx == 4 then      AudioEngine.rewindMusic()  -- 迴圈播放    elseif nIdx == 5 then      -- is background music playing      if AudioEngine.isMusicPlaying () then -- 音樂現正播放        cclog("background music is playing")      else        cclog("background music is not playing")      end    elseif nIdx == 6 then      -- play effect      m_nSoundId = AudioEngine.playEffect(EFFECT_FILE)   -- 播放音效    elseif nIdx == 7 then      -- play effect      m_nSoundId = AudioEngine.playEffect(EFFECT_FILE, true) -- 播放音效,第二個參數表示是否迴圈,true表示迴圈    elseif nIdx == 8 then      -- stop effect      AudioEngine.stopEffect(m_nSoundId) -- 停止音效    elseif nIdx == 9 then      -- unload effect      AudioEngine.unloadEffect(EFFECT_FILE)  -- 不載入音效    elseif nIdx == 10 then      -- add bakcground music volume      AudioEngine.setMusicVolume(AudioEngine.getMusicVolume() + 0.1) -- 增加音量    elseif nIdx == 11 then      -- sub backgroud music volume      AudioEngine.setMusicVolume(AudioEngine.getMusicVolume() - 0.1) -- 減小音量    elseif nIdx == 12 then      -- add effects volume      AudioEngine.setEffectsVolume(AudioEngine.getEffectsVolume() + 0.1) -- 增加音效音量    elseif nIdx == 13 then      -- sub effects volume      AudioEngine.setEffectsVolume(AudioEngine.getEffectsVolume() - 0.1) -- 減少音效音量    elseif nIdx == 14 then      AudioEngine.pauseEffect(m_nSoundId)  -- 暫停音效    elseif nIdx == 15 then      AudioEngine.resumeEffect(m_nSoundId) -- 恢複音效    elseif nIdx == 16 then      AudioEngine.pauseAllEffects() -- 暫停所有音效    elseif nIdx == 17 then      AudioEngine.resumeAllEffects() -- 恢複所有音效    elseif nIdx == 18 then      AudioEngine.stopAllEffects() -- 停止所有音效    end  end  -- add menu items for tests  m_pItmeMenu = cc.Menu:create() -- 建立菜單  m_nTestCount = table.getn(testItems)  local i = 1  for  i = 1, m_nTestCount do    local  label = cc.Label:createWithTTF(testItems[i], s_arialPath, 24)    label:setAnchorPoint(cc.p(0.5, 0.5))    local  pMenuItem = cc.MenuItemLabel:create(label) -- 功能表標籤    pMenuItem:registerScriptTapHandler(menuCallback) -- 註冊菜單回調方法    m_pItmeMenu:addChild(pMenuItem, i + 10000 -1)    pMenuItem:setPosition( cc.p( VisibleRect:center().x, (VisibleRect:top().y - i * LINE_SPACE) ))  end  -- 設定菜單內容大小  m_pItmeMenu:setContentSize(cc.size(VisibleRect:getVisibleRect().width, (m_nTestCount + 1) * LINE_SPACE))  m_pItmeMenu:setPosition(cc.p(0, 0))  ret:addChild(m_pItmeMenu)  -- preload background music and effect  AudioEngine.preloadMusic( MUSIC_FILE ) -- 預先載入音樂  AudioEngine.preloadEffect( EFFECT_FILE ) -- 預先載入音效  -- set default volume  AudioEngine.setEffectsVolume(0.5) -- 設定音效音量  AudioEngine.setMusicVolume(0.5) -- 設定音樂音量  local function onNodeEvent(event)    if event == "enter" then -- 進來時      elseif event == "exit" then --  退出時      AudioEngine.destroyInstance() -- 銷毀對象    end  end  -- 註冊層的結時間點事件  ret:registerScriptHandler(onNodeEvent)  local prev = {x = 0, y = 0}  local function onTouchEvent(eventType, x, y)    if eventType == "began" then -- 開始點擊      prev.x = x      prev.y = y      m_tBeginPos = cc.p(x, y) -- 開始點擊位置      return true    elseif  eventType == "moved" then -- 移動事件      local touchLocation = cc.p(x, y) -- 擷取觸摸的位置      local nMoveY = touchLocation.y - m_tBeginPos.y -- 觸摸位置減去開始位置等於移動的距離      local curPosX, curPosY = m_pItmeMenu:getPosition() -- 擷取當前菜單的位置      local curPos = cc.p(curPosX, curPosY) --  當前位置      local nextPos = cc.p(curPos.x, curPos.y + nMoveY) -- 下一個位置      if nextPos.y < 0.0 then        m_pItmeMenu:setPosition(cc.p(0, 0))      end      if nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height) then        m_pItmeMenu:setPosition(cc.p(0, ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height)))      end      m_pItmeMenu:setPosition(nextPos)      m_tBeginPos.x = touchLocation.x  -- 重新記錄開始位置      m_tBeginPos.y = touchLocation.y      prev.x = x      prev.y = y    end  end  -- 觸摸開始回調方法  local function onTouchBegan(touch, event)    local location = touch:getLocation()    prev.x = location.x    prev.y = location.y    m_tBeginPos = location    return true  end  -- 觸摸移動的回調方法  local function onTouchMoved(touch, event)    local location = touch:getLocation()    local touchLocation = location    local nMoveY = touchLocation.y - m_tBeginPos.y    local curPosX, curPosY = m_pItmeMenu:getPosition()    local curPos = cc.p(curPosX, curPosY)    local nextPos = cc.p(curPos.x, curPos.y + nMoveY)    if nextPos.y < 0.0 then      m_pItmeMenu:setPosition(cc.p(0, 0))    end    if nextPos.y > ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height) then      m_pItmeMenu:setPosition(cc.p(0, ((m_nTestCount + 1)* LINE_SPACE - VisibleRect:getVisibleRect().height)))    end    m_pItmeMenu:setPosition(nextPos)    m_tBeginPos.x = touchLocation.x    m_tBeginPos.y = touchLocation.y    prev.x = location.x    prev.y = location.y  end  -- 單點觸摸  local listener = cc.EventListenerTouchOneByOne:create()  listener:setSwallowTouches(true)  -- 註冊指令碼監聽事件  listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )  listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )  local eventDispatcher = ret:getEventDispatcher()  eventDispatcher:addEventListenerWithSceneGraphPriority(listener, ret)  return retendfunction CocosDenshionTestMain()  cclog("CocosDenshionTestMain")  local scene = cc.Scene:create()  scene:addChild(CocosDenshionTest())  scene:addChild(CreateBackMenuItem())  return sceneend





cocos2d-x怎與Lua想結合使用 具體點

cocos2d-x帶有lua引擎了。看看這個。雖然是英語,但是很容易懂。
Cocos2d-x NEW Lua Engine README
Main features
Support autorelease CCObject object.
Call Lua function from C++ (local, global, closure), avoid memory leaks.
Add CCNode:setPosition(x, y), CCNode::getPosition() huge performance boost.
Remove needless class and functions from tolua++ .pkg files, improved performance.
CCMenuItem events handler.
CCNode onEnter/onExit events handler.
CCLayer touch & multi-touches events handler.
看看這2個串連。
www.cocos2d-x.org/...uments
Scripting and Translating between Programming Languages
 
誰可以提供cocos2d-x +lua 怎編譯成安卓的檔案

呃,其實oc裡面很多東西是需要Java來完成的coos2d主要在遊戲方面用的比較多 其是都差不多了 安坐裡面所有的方法都是打點調用 字串不要家@, 代碼開頭不要加oc裡面的首碼 沒有匯入標頭檔一說,安卓裡面是匯入類檔案,基本上其他的就沒區別了 沒事躲到論壇請教一下很快就能學會的!祝你成功!
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.