Cocos2d-x 3.2 Lua sample CocosDenshionTest (audio test), cocos2dx3.2lua
Cocos2d-x 3.2 Lua sample CocosDenshionTest (audio test)
This blog introduces the Lua sample of Cocos2d-x 3.2 audio testing, the Cocos2d-x uses SimpleAudioEngine class to achieve audio control, such as playing, pause, stop and other operations. In the Lua code, the AudioEngine is used. For specific implementation, refer to the AudioEngine. lua file, but the SimpleAudioEngin is encapsulated.
Sample Code:
-- [CocosDenshionTest. luaCocos2d-x audio support] -- require "AudioEngine" local EFFECT_FILE = "effect1.wav" local MUSIC_FILE = nil -- get the target platform local targetPlatform = cc. application: getInstance (): getTargetPlatform () -- iphone or ipadif (cc. PLATFORM_ OS _IPHONE = targetPlatform) or (cc. PLATFORM_ OS _IPAD = targetPlatform) then MUSIC_FILE = "background. caf "-- caf format else MUSIC_FILE =" background.pdf "-- mp3 format 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 -- test menu item 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"} -- menu callback method local function menuCallback (tag, pMenuItem) local nIdx = pMenuItem: getLocalZOrder () -10000 -- play background music if nIdx = 0 then AudioEngine. playMusic (MUSIC_FILE, true) -- play the music elseif nIdx = 1 then -- stop background music AudioEngine. stopMusic () -- stop background music elseif nIdx = 2 then -- pause background music AudioEngine. pauseMusic () -- pause the music elseif nIdx = 3 then -- resume background music AudioEngine. resumeMusic () -- continue playing music -- rewind background music elseif nIdx = 4 then AudioEngine. rewindMusic () -- loop playback elseif nIdx = 5 then -- is background music playing if AudioEngine. isMusicPlaying () then -- the music is playing 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) -- play sound elseif nIdx = 7 then -- play effect m_nSoundId = AudioEngine. playEffect (EFFECT_FILE, true) -- play sound. The second parameter indicates whether to loop. true indicates loop elseif nIdx = 8 then -- stop effect AudioEngine. stopEffect (m_nSoundId) -- stop the sound effect elseif nIdx = 9 then -- unload effect AudioEngine. unloadEffect (EFFECT_FILE) -- Do not load the sound effect elseif nIdx = 10 then -- add bakcground music volume AudioEngine. setMusicVolume (AudioEngine. getMusicVolume () + 0.1) -- increase the volume of elseif nIdx = 11 then -- sub backgroud music volume AudioEngine. setMusicVolume (AudioEngine. getMusicVolume ()-0.1) -- reduces the volume of elseif nIdx = 12 then -- add effects volume AudioEngine. setEffectsVolume (AudioEngine. getEffectsVolume () + 0.1) -- increases the sound volume. elseif nIdx = 13 then -- sub effects volume AudioEngine. setEffectsVolume (AudioEngine. getEffectsVolume ()-0.1) -- reduces the sound volume. elseif nIdx = 14 then AudioEngine. pauseEffect (m_nSoundId) -- pause the sound effect elseif nIdx = 15 then AudioEngine. resumeEffect (m_nSoundId) -- restore the sound elseif nIdx = 16 then AudioEngine. pauseAllEffects () -- Pause all sound effects. elseif nIdx = 17 then AudioEngine. resumeAllEffects () -- restore all sound effects elseif nIdx = 18 then AudioEngine. stopAllEffects () -- stop all audio effects end -- add menu items for tests m_pItmeMenu = cc. menu: create () -- create Menu 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) -- menu label pMenuItem: registerScriptTapHandler (menuCallback) -- Registration menu callback method m_pItmeMenu: addChild (pMenuItem, I + 10000-1) pMenuItem: setPosition (cc. p (VisibleRect: center (). x, (VisibleRect: top (). y-I * LINE_SPACE) end -- set the menu content size 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) -- preload music AudioEngine. preloadEffect (EFFECT_FILE) -- preload sound -- set default volume AudioEngine. setEffectsVolume (0.5) -- set the audio volume AudioEngine. setMusicVolume (0.5) -- set the Music volume local function onNodeEvent (event) if event = "enter" then -- When elseif event = "exit" then -- AudioEngine is exited. destroyInstance () -- destroy object end -- Registration layer node event ret: registerScriptHandler (onNodeEvent) local prev = {x = 0, y = 0} local function onTouchEvent (eventType, x, x, y) if eventType = "began" then -- click prev. x = x prev. y = y m_tBeginPos = cc. p (x, y) -- start to click position return true elseif eventType = "moved" then -- Mobile event local touchLocation = cc. p (x, y) -- Obtain the position of the touch. local nMoveY = touchLocation. y-m_tBeginPos.y -- touch position minus start position equal to moving distance local curPosX, curPosY = m_pItmeMenu: getPosition () -- get the current menu position local curPos = cc. p (curPosX, curPosY) -- current location local nextPos = cc. p (curPos. x, curPos. y + nMoveY) -- next position 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 -- Record start position m_tBeginPos.y = touchLocation. y prev. x = x prev. y = y end -- touch start callback method 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 -- Single-touch local listener = cc. eventListenerTouchOneByOne: create () listener: setSwallowTouches (true) -- register the script listening event listener: registerScriptHandler (onTouchBegan, cc. handler. EVENT_TOUCH_BEGAN) listener: registerScriptHandler (onTouchMoved, cc. handler. EVENT_TOUCH_MOVED) local eventDispatcher = ret: getEventDispatcher () eventDispatcher: listener (listener, ret) return retendfunction CocosDenshionTestMain () cclog ("CocosDenshionTestMain") local scene =. scene: create () scene: addChild (CocosDenshionTest () scene: addChild (CreateBackMenuItem () return sceneend
How to combine cocos2d-x with Lua to use specific points
The cocos2d-x comes with the lua engine. Look at this. Although it is English, it is easy to understand.
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.
Check the two connections.
Www.cocos2d-x.org/..uments
Scripting and Translating between Programming ages
Who can provide cocos2d-x + lua how to compile the file into Android
Er, in fact, many things in oc need Java to complete. coos2d is mainly used in the game. Most of them are similar. All the methods in sit-down are hitting and calling strings @, do not add the prefix in oc at the beginning of the code without importing the header file. In Android, there are imported class files. Basically, there is no difference between other files. You can skip to the Forum and ask for advice. You will soon be able to learn it! Wish you success!