zoukankan      html  css  js  c++  java
  • 【Cocos2dx】资源目录,播放背景音乐,导入外部库

    在Cocos2dx中播放背景音乐是一件非常easy的事情,就一行代码,可是首先要导入Cocos2dx的音频引擎cocosDenshion。

    cocosDenshion对cocos2dproject提供了非常方便的声音素材的调用和管理。

    同一时候要把要播放的音乐放到project的资源目录才干正确播放

    与《【Cocos2dx】Windows平台下Cocos2dx 2.x的下载、安装、配置。打造自己的Helloworld》(点击打开链接)相同通过例如以下的Python命令。创建一个BGMproject之后:

    python create_project.py -project BGM -package test.bgm -language cpp

    你能够在此project目录中找到Resources目录,你能够发现当中的内容就是早已被我们玩烂的HelloWorld.png与关闭button。

    Cocos2dx用到全部图片、音乐资源都在必须在目录中才可以被调用。


    我们再拷贝一个Windows自带的实例音乐Kalimba.mp3进去,让我们的Cocos2dx程序在启动时,自己主动循环播放这个Kalimba.mp3音乐,也就是游戏的BGM。

    直接打开proj.win32中的HelloCpp.sln。

    对HelloCpp或仅仅在HelloWorld这个场景播放BGM,则对其点击属性,例如以下图加入Cocos2dx的音频引擎cocosDenshion所需的包。就是把.(Cocos2dx)CososDenshioninclude这个目录全员引入到此project或此cpp。


    点击确定之后。我们仅仅须要在HelloWorldScene.cpp中引入头文件#include "SimpleAudioEngine.h"同一时候在bool HelloWorld::init(){}中增加一行播放背景音乐的代码就能够了。

    终于HelloWorldScene.cpp的代码改动例如以下,事实上没改什么。

    #include "HelloWorldScene.h"
    #include "SimpleAudioEngine.h"//引入头文件
    
    USING_NS_CC;
    
    CCScene* HelloWorld::scene()
    {
        // 'scene' is an autorelease object
        CCScene *scene = CCScene::create();
        
        // 'layer' is an autorelease object
        HelloWorld *layer = HelloWorld::create();
    
        // add layer as a child to scene
        scene->addChild(layer);
    
        // return the scene
        return scene;
    }
    
    // on "init" you need to initialize your instance
    bool HelloWorld::init()
    {
    	CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Kalimba.mp3",true);//播放音乐
    	//差别于CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("xx.wav");专门用来播放简短的音效
        //////////////////////////////
        // 1. super init first
        if ( !CCLayer::init() )
        {
            return false;
        }
        
        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
        CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    
        /////////////////////////////
        // 2. add a menu item with "X" image, which is clicked to quit the program
        //    you may modify it.
    
        // add a "close" icon to exit the progress. it's an autorelease object
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                            "CloseNormal.png",
                                            "CloseSelected.png",
                                            this,
                                            menu_selector(HelloWorld::menuCloseCallback));
        
    	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
                                    origin.y + pCloseItem->getContentSize().height/2));
    
        // create menu, it's an autorelease object
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        this->addChild(pMenu, 1);
    
        /////////////////////////////
        // 3. add your codes below...
    
        // add a label shows "Hello World"
        // create and initialize a label
        
        CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
        
        // position the label on the center of the screen
        pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
                                origin.y + visibleSize.height - pLabel->getContentSize().height));
    
        // add the label as a child to this layer
        this->addChild(pLabel, 1);
    
        // add "HelloWorld" splash screen"
        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
    
        // position the sprite on the center of the screen
        pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    
        // add the sprite as a child to this layer
        this->addChild(pSprite, 0);
        
        return true;
    }
    
    
    void HelloWorld::menuCloseCallback(CCObject* pSender)
    {
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
    	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
    #else
        CCDirector::sharedDirector()->end();
    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
        exit(0);
    #endif
    #endif
    }
    

    执行此程序,就能够听到Kalimba.mp3这个难听得要死的Windows7演示样例音乐了。当中CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("Kalimba.mp3",true);中第一个參数就是此音乐在Resource目录的路径。true代表循环播放。

  • 相关阅读:
    C++模板实战6:迭代器
    Hacking up an armv7s library
    Android之ListView分页数据加载
    Android 命令行打包和签名
    django 自定模板标签的注册
    [置顶] 高效能人士的七个习惯读书笔记(二)
    价格战拉上了Android平板电脑
    Synergy 多系统共享鼠标键盘 Windows 和 Mac 完全配置教程
    global planner源码阅读
    源码安装eigen
  • 原文地址:https://www.cnblogs.com/yangykaifa/p/6952078.html
Copyright © 2011-2022 走看看