`
weiyinchao88
  • 浏览: 1234279 次
文章分类
社区版块
存档分类
最新评论

Cocos2d-x 3.2 Lua示例CocosDenshionTest(音频测试)

 
阅读更多
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卷_第2版》是一本专为游戏开发者准备的指南,主要聚焦于使用Cocos2d-x框架与Lua语言进行游戏开发。Cocos2d-x是一个开源的游戏开发框架,它允许开发者用C++、Lua或者JavaScript编写游戏,并且可以...

    cocos2d-x+lua游戏demo源码

    【cocos2d-x+lua游戏demo源码】是一个基于cocos2d-x游戏引擎和lua脚本语言的游戏示例项目。cocos2d-x是一个跨平台的2D游戏开发框架,广泛应用于移动设备,如iOS和Android。lua是一种轻量级的、面向过程的编程语言,...

    cocos2D-lua 核心编程内置代码

    在“Cocos2d-x之Lua核心编程(第二版)_配套代码”中,我们可以找到一系列的示例和代码,这些代码涵盖了Cocos2d-lua的核心功能和编程技巧。 一、Cocos2d-lua简介 Cocos2d-lua是Cocos2d-x的一个扩展,它允许开发者使用...

    关东升_Cocos2d-x实战 Lua卷

    根据提供的文件标题、描述、标签以及部分内容,我们可以总结出与“关东升_Cocos2d-x实战 Lua卷”相关的IT知识点。以下是对这些知识点的详细阐述: ### 关于《Cocos2d-x实战 Lua卷》 #### 1. Cocos2d-x简介 Cocos2d...

    cocos2d-x 动画工具 Flash2Cocos2d-x 1.3

    在压缩包文件"jyinkailej-Flash2Cocos2d-x-8c0deff"中,可能包含了Flash2Cocos2d-x工具的源码、文档、示例项目和其他相关资源,供开发者学习和使用。这些资源可以帮助开发者深入理解如何使用该工具,并进行实际的...

    Cocos2d-x实战C++卷关东升著完整版pdf

    同时,音效和音乐管理也是游戏体验的重要组成部分,书中会介绍如何使用Cocos2d-x的音频API来播放和控制音频资源。 在实际项目开发中,内存管理和性能优化是关键。本书可能会详细讲解Cocos2d-x中的内存管理机制,如...

    cocos2d-x-3.2.zip

    《cocos2d-x 3.2:经典游戏开发框架深度解析》 cocos2d-x 是一个跨平台的游戏开发框架,它基于C++,同时提供了Lua和JavaScript的绑定,让开发者可以方便地在多种操作系统上创建2D游戏、演示程序和其他图形/交互式...

    Cocos2d-x实战 JS卷 Cocos2d-JS开发

    Cocos2d-x框架基于C++,同时提供了Lua和JavaScript的绑定,使得开发者可以根据自己的喜好选择编程语言。JS卷特别针对JavaScript开发者,详细介绍了如何利用Cocos2d-JS进行游戏开发,涵盖了从基础概念到高级技术的...

    cocos2d-x与lua

    基本步骤包括下载cocos2d-x的源代码并编译出支持Lua脚本的可执行文件,然后在该环境下进行Lua脚本的编写、修改与测试。环境搭建包括修改配置文件、设置脚本搜索路径以及管理资源文件等关键操作。 知识点六:iOS平台...

    Cocos2d-x学习笔记

    - 学习脚本语言:虽然Cocos2d-x支持多语言开发,但了解JavaScript、Lua等脚本语言,可以快速实现游戏功能和逻辑。 Cocos2d-x框架除了Windows平台外,还支持iOS、Android、Mac OS X、Web等平台,初学者在掌握了...

    cocos2dx lua核心编程(第二版)配套代码

    《Cocos2d-x之Lua核心编程(第二版)》配套代码是一份极其重要的学习资源,旨在帮助开发者深入理解和熟练运用Cocos2d-x游戏引擎与Lua语言进行游戏开发。Cocos2d-x是一个跨平台的游戏开发框架,而Lua则是一种轻量级的...

    cocos2d-x 案例开发大全 第二章(源码)

    《cocos2d-x案例开发大全》第二章的源码主要涵盖了使用cocos2d-x进行游戏开发的各种实践技巧和示例。cocos2d-x是一个开源的游戏开发框架,基于C++,支持跨平台,包括iOS、Android、Windows等多个操作系统。本章的...

    Cocos2d-x demo程序

    Cocos2d-x 是一个广泛使用的开源游戏开发框架,它基于C++,并提供了Python、Lua等语言的绑定,使得开发者能够用多种编程语言来创建2D和3D游戏。这个" Cocos2d-x demo程序 "是作者使用Cocos2d-x编写的示例项目,用于...

    Cocos2d-x实战++Lua卷.pdf

    根据提供的文件信息,本文将重点围绕“Cocos2d-x实战++Lua卷”这一主题进行深入探讨,并结合描述部分给出的知识点,详细阐述Cocos2d-x与Lua在游戏开发中的应用。 ### Cocos2d-x简介 Cocos2d-x是一款开源的游戏引擎...

    cocos2d-1.0.1-x-0.10.0

    5. **资源管理**:Cocos2d-x 包含了图片、音频、字体等资源的加载和管理功能,简化了资源的生命周期管理。 6. **性能优化**:通过纹理 atlases 和批处理技术,提高了图像渲染的效率,减少了内存占用。 7. **脚本...

    cocos2d-lua整合到ios工程.zip

    在iOS平台上开发游戏时,有时候会使用到Cocos2d-x框架,而为了实现更高效、更便捷的编程,开发者可能会选择使用Lua作为脚本语言。"cocos2d-lua整合到ios工程.zip"这个压缩包文件提供了一个将Cocos2d-lua集成到原生...

    cocos2d-x 3.x游戏开发实战光盘

    《cocos2d-x 3.x游戏开发实战光盘》是一个深入探讨cocos2d-x 3.x游戏引擎开发的资源集合,旨在帮助开发者通过实际案例掌握这一强大的2D游戏开发工具。cocos2d-x是一款开源的游戏开发框架,基于C++,广泛应用于跨平台...

    瘸腿蛤蟆笔记39-cocos2d-x-3.2 Box2d物理引擎自由落体代码

    在本篇中,我们将深入探讨使用Cocos2d-x 3.2框架集成Box2D物理引擎实现自由落体效果的编程技术。Cocos2d-x是一个广泛使用的开源游戏开发框架,它支持多种平台,包括iOS、Android以及桌面系统。Box2D是一个强大的2D...

    Cocos2d-X游戏源码大合集.rar

    Cocos2d-X是一款强大的开源跨平台2D游戏开发框架,它基于C++,并提供了JavaScript和Lua等多种脚本语言接口。这个“Cocos2d-X游戏源码大合集.rar”文件显然包含了大约三十个使用Cocos2d-X开发的游戏实例源代码,对于...

    Cocos2d-x实战:Lua卷(第2版)书籍代码

    这是书籍 Cocos2d-x实战:Lua卷(第2版)的代码,关东升老师的,这里分享出来。除了章节的小示例代码外,还有一个《迷失航线》的游戏示例代码。这是一个下载地址。

Global site tag (gtag.js) - Google Analytics