cocos2dx 钢琴

#include "PianoScene.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

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

bool Piano::init()
{
	if (!Layer::init())
		return false;

	auto size = Director::getInstance()->getVisibleSize();

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("1.mp3");
	auto button1 = MenuItemImage::create("button1.png", "button2.png", this, SEL_MenuHandler(&Piano::note1));
	button1->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	button1->setScaleX(size.width * 0.8 / 80 / 7);
	button1->setScaleY(size.height / 360);
	auto menu1 = Menu::create();
	menu1->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	menu1->addChild(button1);
	menu1->setPosition((1-1)*size.width/7,0);
	addChild(menu1);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("2.mp3");
	auto button2 = MenuItemImage::create("button1.png", "button2.png", this, SEL_MenuHandler(&Piano::note2));
	button2->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	button2->setScaleX(size.width * 0.8 / 80 / 7);
	button2->setScaleY(size.height / 360);
	auto menu2 = Menu::create();
	menu2->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	menu2->addChild(button2);
	menu2->setPosition((2 - 1)*size.width / 7, 0);
	addChild(menu2);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("3.mp3");
	auto button3 = MenuItemImage::create("button1.png", "button2.png", this, SEL_MenuHandler(&Piano::note3));
	button3->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	button3->setScaleX(size.width * 0.8 / 80 / 7);
	button3->setScaleY(size.height / 360);
	auto menu3 = Menu::create();
	menu3->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	menu3->addChild(button3);
	menu3->setPosition((3 - 1)*size.width / 7, 0);
	addChild(menu3);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("4.mp3");
	auto button4 = MenuItemImage::create("button1.png", "button2.png", this, SEL_MenuHandler(&Piano::note4));
	button4->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	button4->setScaleX(size.width * 0.8 / 80 / 7);
	button4->setScaleY(size.height / 360);
	auto menu4 = Menu::create();
	menu4->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	menu4->addChild(button4);
	menu4->setPosition((4 - 1)*size.width / 7, 0);
	addChild(menu4);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("5.mp3");
	auto button5 = MenuItemImage::create("button1.png", "button2.png", this, SEL_MenuHandler(&Piano::note5));
	button5->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	button5->setScaleX(size.width * 0.8 / 80 / 7);
	button5->setScaleY(size.height / 360);
	auto menu5 = Menu::create();
	menu5->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	menu5->addChild(button5);
	menu5->setPosition((5 - 1)*size.width / 7, 0);
	addChild(menu5);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("6.mp3");
	auto button6 = MenuItemImage::create("button1.png", "button2.png", this, SEL_MenuHandler(&Piano::note6));
	button6->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	button6->setScaleX(size.width * 0.8 / 80 / 7);
	button6->setScaleY(size.height / 360);
	auto menu6 = Menu::create();
	menu6->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	menu6->addChild(button6);
	menu6->setPosition((6 - 1)*size.width / 7, 0);
	addChild(menu6);

	CocosDenshion::SimpleAudioEngine::sharedEngine()->preloadEffect("7.mp3");
	auto button7 = MenuItemImage::create("button1.png", "button2.png", this, SEL_MenuHandler(&Piano::note7));
	button7->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	button7->setScaleX(size.width * 0.8 / 80 / 7);
	button7->setScaleY(size.height / 360);
	auto menu7 = Menu::create();
	menu7->setAnchorPoint(Vec2(0, 0));//锚点改为左下角
	menu7->addChild(button7);
	menu7->setPosition((7 - 1)*size.width / 7, 0);
	addChild(menu7);

	return true;
}

void Piano::note1(cocos2d::Ref*)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("1.mp3");
}
void Piano::note2(cocos2d::Ref*)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("2.mp3");
}
void Piano::note3(cocos2d::Ref*)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("3.mp3");
}
void Piano::note4(cocos2d::Ref*)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("4.mp3");
}
void Piano::note5(cocos2d::Ref*)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("5.mp3");
}
void Piano::note6(cocos2d::Ref*)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("6.mp3");
}
void Piano::note7(cocos2d::Ref*)
{
	CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("7.mp3");
}

代码下载

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫描关注云+社区