Cocos2d-x 3.2 Lua示例CocosDenshionTest(音频测试)
本篇博客介绍Cocos2d-x 3.2中Lua示例的音频测试,Cocos2d-x使用SimpleAudioEngine这个类来实现音频的控制,比如播放、暂停、停止等操作。
Lua代码中,使用的是AudioEngine,具体实现可以参考AudioEngine.lua文件,只是把SimpleAudioEngin进行了封装。
示例代码:
--[[
CocosDenshionTest.lua
Cocos2d-x 音频支持
]]--
require "AudioEngine"
local EFFECT_FILE = "effect1.wav"
local MUSIC_FILE = nil
-- 获取目标平台
local targetPlatform = cc.Application:getInstance():getTargetPlatform()
-- iphone或者ipad
if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) then
MUSIC_FILE = "background.caf" -- caf格式
else
MUSIC_FILE = "background.mp3" -- mp3格式
end
local 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 ret
end
function CocosDenshionTestMain()
cclog("CocosDenshionTestMain")
local scene = cc.Scene:create()
scene:addChild(CocosDenshionTest())
scene:addChild(CreateBackMenuItem())
return scene
end
分享到:
相关推荐
这是书籍 Cocos2d-x实战:Lua卷(第2版)的代码,关东升老师的,这里分享出来。除了章节的小示例代码外,还有一个《迷失航线》的游戏示例代码。这是一个下载地址。
cocos2d-x-lua测试案例修复了cocos2d-x LUA示例代码,并使其可以在 Ko, Cocos Code IDE更新中
lua_c++ 交互原理 cocos2d手动绑定示例 cocos2d自动绑定工具:环境配置,使用详细说明,使用见解 试用于想要了解lua绑定的新人
第1章 Cocos2D-X引擎的介绍 1 1.1 何为游戏引擎 1 1.1.1 游戏的核心—引擎 1 1.1.2 引擎的特点 2 1.1.3 知名的引擎介绍 4 1.1.4 引擎的分类 5 1.2 Cocos2D-X引擎的来历 8 1.3 引擎的版本 9 1.4 下载与安装 10 1.5 ...
cocos2d-Lua示例demo源码踩虫子-MOD美国大兵
快速界面控制 快速 cocos2d-x uicontrol 示例
Cocos2d-lua示例(一)排行榜之TableView-附件资源
MoonWarriors-lua是为了学习Cocos2d-x Lua开发而移植的Quick-Cocos2d-x版本。移植过程中,体验了一回JS代码到Lua代码的各自语法特性。 1、增加了屏幕分辨率适配。 2、修复了原版本的偶尔屏幕卡顿问题。 界面载图: ...
cocos2dx-lua 自定义事件类
入门 运行安装程序脚本,它将自动下载其依赖项 ...SDKBOX示例使用共享的cocos2d-x二进制文件来减少下载 文献资料 有关更多信息,请查看 跑步 在发布模式下编译: cocos run -p android -m release
#cocos2dx 播放视频插件 ##停止维护 貌似看到cocos2dx v3 版已经自带了一个videoplayer, 所以这个就停止维护了! ##特性: lua绑定 播放完成事件回调 支持android, ios平台 ##使用示例: -- 播放完之后的回调函数 local...