前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >cocos2dx 钢琴

cocos2dx 钢琴

作者头像
_gongluck
发布2018-03-08 14:22:52
1.1K0
发布2018-03-08 14:22:52
举报
文章被收录于专栏:转载gongluck的CSDN博客
代码语言:javascript
复制
#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");
}

代码下载

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年07月03日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档