cocos2d-x 模拟钢琴

素材:https://pan.baidu.com/s/1QNZBWrstBuuaWZ4P7trxRQ

在HelloWorldScene.h中

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"

class HelloWorld : public cocos2d::Layer
{
public:
	bool is_paused;
    static cocos2d::Scene* createScene();
    virtual bool init();

	void HelloWorld::note1(cocos2d::Object* pSender);
	void HelloWorld::note2(cocos2d::Object* pSender);
	void HelloWorld::note3(cocos2d::Object* pSender);
	void HelloWorld::note4(cocos2d::Object* pSender);
	void HelloWorld::note5(cocos2d::Object* pSender);
    CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

在HelloWorldScene.cpp中

#include "HelloWorldScene.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include"SimpleAudioEngine.h"		//播放音乐的头文件

USING_NS_CC;

using namespace cocostudio::timeline;

Scene* HelloWorld::createScene()
{
    auto scene = Scene::create();
    auto layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
	//预加载5个音符的MP3文件
	for (int i = 0; i < 5; i++)
	{
		//preload 预加载   Effect  效果		Format  格式
		CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect(String::createWithFormat("%d.mp3", i)->getCString());
	}
	//通过按钮,创建5个琴键
	auto *pButon1 = MenuItemImage::create("button1.jpg", "button2.jpg", this, menu_selector(HelloWorld::note1));
	auto *button1 = Menu::create(pButon1, NULL);
	button1->setPosition(130, 320);
	addChild(button1);

	auto *pButon2 = MenuItemImage::create("button1.jpg", "button2.jpg", this, menu_selector(HelloWorld::note2));
	auto *button2 = Menu::create(pButon2, NULL);
	button2->setPosition(130*2+10, 320);
	addChild(button2);

	auto *pButon3 = MenuItemImage::create("button1.jpg", "button2.jpg", this, menu_selector(HelloWorld::note3));
	auto *button3 = Menu::create(pButon3, NULL);
	button3->setPosition(130 * 3 + 20, 320);
	addChild(button3);

	auto *pButon4 = MenuItemImage::create("button1.jpg", "button2.jpg", this, menu_selector(HelloWorld::note4));
	auto *button4 = Menu::create(pButon4, NULL);
	button4->setPosition(130 * 4 + 30, 320);
	addChild(button4);

	auto *pButon5 = MenuItemImage::create("button1.jpg", "button2.jpg", this, menu_selector(HelloWorld::note5));
	auto *button5 = Menu::create(pButon5, NULL);
	button5->setPosition(130 * 5 + 40, 320);
	addChild(button5);
	
	return true;
}
//5个按键的响应操作
void HelloWorld::note1(cocos2d::Object* pSender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("11.mp3");
}
void HelloWorld::note2(cocos2d::Object* pSender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("12.mp3");
}
void HelloWorld::note3(cocos2d::Object* pSender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("52.mp3");
}
void HelloWorld::note4(cocos2d::Object* pSender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("53.mp3");
}
void HelloWorld::note5(cocos2d::Object* pSender)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("54.mp3");
}


点击运行

cocos2d-x 模拟钢琴_第1张图片

了解:播放这类比较短的音乐的方法与playBackgroundMusic方法是完全相同分的,只不过要将方法明修改就行。另外,因为这类音乐是要反复播放的,如果每次调用playEffect时都会重新对音乐文件进行加载,因此可以在场景初始化时,对这些文件进行统一加载,就可

你可能感兴趣的:(cocos2d-x 模拟钢琴)