还有播放背景音乐只能放一次,第二次放就没反映,重放,暂停按钮也没有用
//这是CPP文件:
#if(CC_TARGET_PLATFORM==CC_PLATFOR_ANDROID)
#define EFFECT_FILE “effect2.ogg”#else
#define EFFECT_FILE “effect1.wav”
#endif
#if (CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
#define MUSIC_FILE “music.mid”
#elif(CC_TARGET_PLATFORM==CC_PLATFOR_BLACKBERRY||CC_TARGET_PLATFORM==CC_PLATFOR_LINUX)
#define MUSIC_FILE “background.ogg”
#else
#define MUSIC_FILE “background.mp3”
#endif
#include “HelloWorldScene.h”
USING_NS_CC;
//Button类
class Button :public Node
{
public:
static ButtoncreateWithSprite(const charfilePath)
{
auto b = new Button();
if (b&&!b->initSpriteButton(filePath)) {
delete b;
b = NULL;
}
return b;
}
static ButtoncreateWithText(const chartext)
{
auto b = new Button();
if (b&&!b->initTextButton(text)) {
delete b;
b = NULL;
}
return b;
}
~Button()
{
}
void onTriggered(const std::function<void(void)>&onTriggered)
{
_onTriggered = onTriggered;
}
private:
Button() :_child(NULL)
{
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = =](Touch touch, Event event)
{
const bool hits = touchHits(touch);
if (hits)
scaleButtonTo(0.7f);
return hits;
};
listener->onTouchEnded = =](Touchtouch, Eventevent)
{
const bool hits = touchHits(touch);
if (hits&&_onTriggered)
_onTriggered();
scaleButtonTo(1);
};
listener->onTouchCancelled = =](Touchtouch, Event event)
{
scaleButtonTo(1);
#elif(CC_TARGET_PLATFORM==CC_PLATFORM_MARMALADE)
#define EFFECT_FILE “effect1.raw”
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
}
bool initSpriteButton(const char*filePath)
{
_child = Sprite::create(filePath);
addChild(_child);
return true;
}
bool initTextButton(const char*text)
{
_child = Label::createWithSystemFont(text, "Arial", 32);
addChild(_child);
return true;
}
bool touchHits(Touch *touch)
{
const Rect area(0, 0, _child->getContentSize().width, _child->getContentSize().height);
return area.containsPoint(_child->convertToNodeSpace(touch->getLocation()));
}
void scaleButtonTo(float scale)
{
auto action = ScaleTo::create(0.1f, scale);
action->setTag(900);
stopActionByTag(900);
runAction(action);
}
Node *_child;
std::function<void(void)> _onTriggered;
};
//AudioSlider类
class AudioSlider :public Node
{
public:
static AudioSlidercreate()
{
auto ret = new AudioSlider();
if (ret&&!ret->init()) {
delete ret;
ret = NULL;
}
return ret;
}
float getValue()const
{
return _slider->getValue();
}
void setValue(float minValue, float maxValue, float value)
{
_slider->setMinimumValue(minValue);
_slider->setMaximumValue(maxValue);
_slider->setValue(value);
char buffer;
sprintf(buffer, “.2f", minValue);
_lblMinValue = Label::createWithSystemFont(buffer, "Arial", 18);
addChild(_lblMinValue);
sprintf(buffer, ".2f”, maxValue);
_lblMaxValue = Label::createWithSystemFont(buffer, “Arial”, 18);
addChild(_lblMaxValue);
}
void setSliderPosition(float x, float y) {
Size visibleSize = Director::getInstance()->getVisibleSize();
_slider->setPosition(visibleSize.widthx, visibleSize.heighty);
_lblMinValue->setPosition(visibleSize.width(x - 0.1), visibleSize.height*(y + 0.5));
_lblMaxValue->setPosition(visibleSize.width*(x + 0.1), visibleSize.height*(y + 0.5));
}
ControlSlider *getSlider() {
return _slider;
}
private:
bool init()
{
_slider = ControlSlider::create(“sliderTrack.png”,“sliderProgress.png”, “sliderThumb.png”);
addChild(_slider);
return true;
}
ControlSlider *_slider;
Label *_lblMinValue;
Label _lblMaxValue;
};
Scene HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto 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()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->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
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
SimpleAudioEngine::getInstance()->preloadBackgroundMusic(MUSIC_FILE);
SimpleAudioEngine::getInstance()->preloadEffect(EFFECT_FILE);
auto lblMusic = Label::createWithSystemFont("backgroundcotrol", "Arial", 32);
lblMusic->setPosition(visibleSize.width*0.25, visibleSize.height*0.9);
this->addChild(lblMusic);
auto playMusic = Button::createWithText("PLAY");
playMusic->onTriggered(] {
SimpleAudioEngine::getInstance()->playBackgroundMusic(MUSIC_FILE); });
playMusic->setPosition(visibleSize.width*0.1, visibleSize.height*0.8);
this->addChild(playMusic);
auto stopMusic = Button::createWithText("EXIT");
stopMusic->onTriggered(] {
SimpleAudioEngine::getInstance()->stopBackgroundMusic(); });
stopMusic->setPosition(visibleSize.width*0.25, visibleSize.height*0.8);
this->addChild(stopMusic);
auto rewindMusic = Button::createWithText("REPLAY");
rewindMusic->onTriggered(] {
SimpleAudioEngine::getInstance()->rewindBackgroundMusic(); });
rewindMusic->setPosition(visibleSize.width*0.40, visibleSize.height*0.8);
this->addChild(rewindMusic);
auto pauseMusic = Button::createWithText("STOP");
pauseMusic->onTriggered(] {
SimpleAudioEngine::getInstance()->pauseBackgroundMusic(); });
pauseMusic->setPosition(visibleSize.width*0.1, visibleSize.height*0.65);
this->addChild(pauseMusic);
auto resumeMusic = Button::createWithText("connitine");
resumeMusic->onTriggered(] {
SimpleAudioEngine::getInstance()->resumeBackgroundMusic(); });
resumeMusic->setPosition(visibleSize.width*0.25, visibleSize.height*0.65);
this->addChild(resumeMusic);
auto lblMusicVolume = Label::createWithSystemFont("yingliang", "Arial", 24);
lblMusicVolume->setPosition(visibleSize.width*0.08, visibleSize.height*0.45);
this->addChild(lblMusicVolume);
_sliderMusicVolume - AudioSlider::create();
_sliderMusicVolume->setValue(0.00f, 1.00f, 1.0f);
_sliderMusicVolume->setSliderPosition(0.28, 0.45);
_sliderMusicVolume->getSlider()->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::musicValueChanged), Control::EventType::VALUE_CHANGED);
this->addChild(_sliderMusicVolume);
return true;
}
void HelloWorld::musicValueChanged(Ref pSender, Control::EventType controlEvent)
{
ControlSliderpSlider = dynamic_cast<ControlSlider*>(pSender);
const float musicVolume = pSlider->getValue();
SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(musicVolume);
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
//这是头文件:
#ifndef HELLOWORLD_SCENE_H
#define HELLOWORLD_SCENE_H
#include “cocos2d.h”
#include “SimpleAudioEngine.h”
#include"cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
using namespace CocosDenshion;
class AudioSlider;
class HelloWorld : public cocos2d::Layer
{
private:
unsigned int _soundId;
AudioSlider *_sliderMusicVolume;
AudioSlider *_sliderEffectsVolume;
AudioSlider *_sliderPitch;
AudioSlider *_sliderPan;
AudioSlider _sliderGain;
public:
static cocos2d::Scene createScene();
virtual bool init();
void musicValueChanged(Ref* pSender, Control::EventType controlEvent);
void effectsValueChanged(Ref* pSender, Control::EventType controlEvent);
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // HELLOWORLD_SCENE_H