#ifndef __MUSICLAYER_H__ #define __MUSICLAYER_H__ #include "cocos2d.h" using namespace std; using namespace cocos2d; class MusicLayer:public LayerColor { public: void inits(); void free(); MusicLayer(); ~MusicLayer(); private: void OnCallback(cocos2d::Ref* pSender); virtual bool onTouchBegan(Touch *touch, Event *unused_event); }; #endif // !__MUSICLAYER_H__
#include "MusicLayer.h" #include "cocos/ui/UIButton.h" #include "SimpleAudioEngine.h" #define DISPLAY Director::getInstance()->getVisibleSize() #define displayex Director::getInstance()->getVisibleSize() MusicLayer::MusicLayer() { this->inits(); } MusicLayer::~MusicLayer() { this->free(); } void MusicLayer::free() { } void MusicLayer::inits() { log("MusicLayer init"); this->setTouchEnabled(true); auto ELTOBO = EventListenerTouchOneByOne::create(); ELTOBO->setSwallowTouches(true); ELTOBO->onTouchBegan = std::move(std::bind(&MusicLayer::onTouchBegan, this, std::placeholders::_1, std::placeholders::_2)); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(ELTOBO, this); //this->getEventDispatcher()->addEventListenerWithFixedPriority(); //this->getEventDispatcher()->addCustomEventListener(); string file("res/Button.png"); auto b = cocos2d::ui::Button::create(file, file, file); b->setColor(Color3B(24, 48, 64)); b->setPosition(displayex / 2); b->setPressedActionEnabled(true); b->setContentSize(Size(200, 100)); b->setTitleColor(Color3B::BLUE); b->addClickEventListener(std::bind(&MusicLayer::OnCallback, this, std::placeholders::_1)); this->addChild(b); } void MusicLayer::OnCallback(cocos2d::Ref* pSender) { string name = ((Node*)(pSender))->getName(); if (name == "") { } } bool MusicLayer::onTouchBegan(Touch *pTouch, Event *pEvent) { log("touch MusicLayer"); return true; }
auto musicLayer = new (std::nothrow) MusicLayer(); if (musicLayer && musicLayer->initWithColor(Color4B(12, 24, 32, 255))) { musicLayer->setPosition(0, DISPLAY.height); musicLayer->runAction(MoveTo::create(0.1f, Vec3(0, 0, 0))); musicLayer->autorelease(); this->addChild(musicLayer); }