了解Cocos2d-x中的声音引擎 掌握SimpleAudioEngine类 掌握音效的控制 Cocos2d-x中的事件分发机制 了解监听器 了解优先级 掌握触摸事件及其应用 其他事件 Cocos2d-x提供了一个SimpleAudioEngine类支持游戏内的音乐和音效。 它可以被用来增加背景音乐,控制游戏音效。 SimpleAudioEngine类 SimpleAudioEngine单例对象
auto audioEngine = SimpleAudioEngine->getInstance();
播放背景音乐和音效 背景音乐和音效都是异步播放的, 声音引擎做了单独的处理,因此需要分别播放
audioEngine->playBackgroundMusic("音乐文件Resources的相对路径",true);
audioEngine->playEffect("音乐文件Resources的相对路径",false,1.0f,1.0f,1.0f);
SimpleAudioEngine支持的音频格式 支持的音乐格式
平台 | 音乐文件格式 | 备注 |
---|
Android | mp3,mid,ogg,wav | 可以播放android、media、MediaPlayer所支持的所有格式 | iOS | aac,caf,map3,m4a | 可以播放android、media、MediaPlayer所支持的所有格式 | Android | mp3,mid,wav | mp3官方文档说支持的是AudioEngine在SimpleAudioEngine引擎底层做了一系列的封装,但是实际cocos2d中SimpleAudioEngine不支持的 |
支持的音效格式
平台 | 音效文件格式 | 备注 |
---|
Android | ogg,wav | 对wav的支持不完美 | iOS | caf,m4a | 可以播放Cocos2d-iphone CocosDesion所支持的所有格式 | Android | mid,wav | 无 |
声音的控制 注意: 1、暂停与恢复相对应,指的是播放中的音频文件暂时停止, 可以通过恢复的方式从暂停位置继续播放。 2、处于暂停中的声音可以直接被停止,停止后不可以恢复播放,只能 只能重新开始播放。
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
audio->pauseBackgroundMusic();
audio->pauseEffect();
audio->pauseAllEffects();
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
audio->stopBackgroundMusic();
audio->stopEffect();
audio->stopAllEffects();
恢复与暂停相对应,指的是从暂停位置继续播放音频文件。
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
auto audio = SimpleAudioEngine::getInstance();
audio->resumeBackgroundMusic();
audio->resumeEffect();
audio->resumeAllEffects();
实现背景音乐的控制,要求: 点击按钮播放背景音乐 点击按钮暂停播放背景音乐 点击按钮恢复播放背景音乐 点击按钮停止播放背景音乐
MusicDemo.h
#pragma once
#include "cocos2d.h"
class MusicScene:public Scene
{
public:
CREATE_FUNC(MusicScene);
virtual bool init();
};
MusicDemo.cpp #include "ui/CocosGUI.h"包含的头文件
#include "ui/UIWidget.h"
#include "ui/UILayout.h"
#include "ui/UIButton.h"
#include "ui/UICheckBox.h"
#include "ui/UIRadioButton.h"
#include "ui/UIImageView.h"
#include "ui/UIText.h"
#include "ui/UITextAtlas.h"
#include "ui/UILoadingBar.h"
#include "ui/UIScrollView.h"
#include "ui/UIListView.h"
#include "ui/UISlider.h"
#include "ui/UITextField.h"
#include "ui/UITextBMFont.h"
#include "ui/UIPageView.h"
#include "ui/UIHelper.h"
#include "ui/UIRichText.h"
#include "ui/UIHBox.h"
#include "ui/UIVBox.h"
#include "ui/UIRelativeBox.h"
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
#include "ui/UIVideoPlayer.h"
#include "ui/UIWebView/UIWebView.h"
#endif
#include "ui/GUIExport.h"
#include "ui/UIScale9Sprite.h"
#include "ui/UIEditBox/UIEditBox.h"
#include "ui/UILayoutComponent.h"
#include "ui/UITabControl.h"
#include "editor-support/cocostudio/CocosStudioExtension.h"
#include "MusicDemo.h"
#include "ui/CocosGUI.h"
using namespace ui;
#include "SimpleAudioEngine.h"
using namespace CocosDenshion;
bool MusicScene::init()
{
if(!Scene::init())return false;
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 base = Vec2(visibleSize / 2);
Button* _button = Button::create();
_button ->setTitleText("Play");
_button ->setTitleFontSize(36);
_button ->setPositionX(base.x);
_button ->setPositionY(base.y + _button->getContentSize().height * 1.5 + 15);
_button->addClickEventListener([](Ref* sender)
{
SimpleAudioEngine::getInstance()->playBackgroundMusic("音乐文件相对路径",true);
SimpleAudioEngine::getInstance()->setBackgroundMusicVolume(1.2f);
});
this->addChild(_button);
_button = Button::create();
_button ->setTitleText("Pause");
_button ->setTitleFontSize(36);
_button ->setPositionX(base.x);
_button ->setPositionY(base.y + _button->getContentSize().height * 0.5 + 5);
_button->addClickEventListener([](Ref* sender)
{
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
});
this->addChild(_button);
_button = Button::create();
_button ->setTitleText("Resume");
_button ->setTitleFontSize(36);
_button ->setPositionX(base.x);
_button ->setPositionY(base.y + _button->getContentSize().height * 0.5 - 5);
_button->addClickEventListener([](Ref* sender)
{
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
});
this->addChild(_button);
_button = Button::create();
_button ->setTitleText("Stop");
_button ->setTitleFontSize(36);
_button ->setPositionX(base.x);
_button ->setPositionY(base.y + _button->getContentSize().height * 0.5 - 15);
_button->addClickEventListener([](Ref* sender)
{
SimpleAudioEngine::getInstance()->stopBackgroundMusic();
});
this->addChild(_button);
return true;
}
事件分发机制 Cocos2d-x通过事件分发机制响应用户事件 内置支持常见的事件如触摸事件,键盘事件等。 提供了创建自定义事件的方法,满足我们在游戏的开发过程中,特殊的事件响应需求。
什么是事件分发? 当事件发生(例如,用户触摸屏幕,或者敲键盘);EventDispatcher会发布 (Event objects)事件对象到合适的EventListeners,并调用你的回调。 基本元素: 事件监听器:EventListeners 事件分发器:EventDispatcher 事件对象:Event 优先级 使用一个整形的数值,数值较低的监听器比数值较高的监听器,先接收到事件。 场景图优先级 是指向节点对象的指针,z-order较高的节点中的监听器比z-order 较低的节点中的,先接收到事件。
事件的吞没 监听器接收到期望的事件 这时事件应该被吞没 触摸事件 什么是触摸事件 当你触摸移动设备的屏幕时,设备感受到被触摸,获取被触摸的位置和信息,然后反馈给你。 提示:如果需要触摸控制屏幕下层节点,可以通过优先级达成这种需求,优先级高的能先处理事件。
auto listener1 = EventListenerTouchOneByOne::create();
listener1 ->onTouchBegan = [](Touch* touch,Event* event)
{
return true;
};
listener1 ->onTouchMoved = [](Touch* touch,Event* event){};
listener1 ->onTouchMoved = [=](Touch* touch,Event* event){};
_eventDispatcher ->addEventListenerWithSceneGraphpriority(listenere1,this);
演示事件的触摸: TouchLayer.h
#pragma once
#include "cocos2d.h"
class TouchLayer:public Scene
{
public:
CREATE_FUNC(TouchLayer);
virtual bool init();
};
TouchLayer.cpp
bool TouchLayer::init()
{
this->setContentSize(Director::getInstance()->getVisibleSize());
auto listener = EventListenerTouchOneByOne::create();
listener ->onTouchBegan = [](Touch* touch,Event* event)
{
log("touch began");
return true;
};
listener ->onTouchEnded = [](Touch* touch,Event* event)
{
log("touch end");
};
listener ->onTouchMoved = [](Touch* touch,Event* event)
{
log("touch moved");
};
_eventDispatcher ->addEventListenerWithSceneGraphpriority(listenere,this);
return false;
}
键盘事件
auto listener = EventListenerboard::create();
listener ->onKeyPressed = CC_CALLBACK_2(KeyBoardTest::onKeyPressed,this);
listener ->onKeyReleased = CC_CALLBACK_2(KeyBoardTest::onKeyReleased,this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener,this);
void keyboardTest::onKeyPressed(EventKeyboard::KeyCode KeyCode,Event* event)
{
log("key with keycode %d pressed",KeyCode);
}
void keyboardTest::onKeyReleased(EventKeyboard::KeyCode KeyCode,Event* event)
{
log("key with keycode %d released",KeyCode);
}
加速度传感器事件 一些移动设备备有加速度传感器,通过监听它的事件获取各方向的加速度。
Device::setAccelerometerEnabled(true);
auto listener = EventListenerAcceleration::create(
CC_CALLBACK_2(AccelerometerTest::onAcceleration,
this));
_eventDispatcher ->addEventListenerWithSceneGraphpriority(listenere,this);
void AccelerometerTest::onAcceleration(Acceleration* acc,Event* event){};
鼠标事件 创建鼠标事件
_mouseListener = EventListenerMouse::create();
_mouseListener ->onMouseMove = CC_CALLBACK_1(MouseTest::omMouseMove,this);
_mouseListener ->onMouseUp = CC_CALLBACK_1(MouseTest::onMouseUp ,this);
_mouseListener ->onMouseDown = CC_CALLBACK_1(MouseTest::onMouseDown ,this);
_mouseListener ->onMouseScroll = CC_CALLBACK_1(MouseTest::onMouseScroll,this);
_eventDispatcher ->addEventListenerWithSceneGraphpriority(_mouseListener ,this);
事件类型
void MouseTest::omMouseDown(Event* event){}
void MouseTest::omMouseUp(Event* event){}
void MouseTest::omMousemove(Event* event){}
void MouseTest::omMouseScroll(Event* event){}
触摸事件传递到下层,控制上层的精灵移动,要求: 上层触摸事件控制该层的左右移动 事件传递到下层 下层事件控制精灵的竖直上下移动
EventDemo.h
#pragma once
#include "cocos2d.h"
class EventScene:public Scene
{
public:
CREATE_FUNC(EventScene);
virtual bool init();
};
EventDemo.cpp
#include "TopLayer.h"
#include "BottomLayer.h"
bool EventScene::init()
{
auto topLayer = TopLayer::create();
this->addChild(topLayer);
auto bottomLayer = BottomLayer::create(topLayer->getSprite());
this->addChild(bottomLayer);
return true;
}
TopLayer.h
#pragma once
#include "cocos2d.h"
class TopLayer:public Layer
{
public:
CREATE_FUNC(TopLayer);
virtual bool init();
Sprite* getSprite();
private:
Sprite* _sprite;
Vec2 _position;
};
TopLayer.cpp
bool TopLayer::init()
{
Size visible = Director::getInstance()->getVisibleSize();
Vec2 center = Vec2(visibleSize / 2);
Sprite* bg = Sprite::create("图片相对路径");
bg->setPosition(bg);
this->addChild(bg);
_sprite = Sprite::create("图片相对路径");
_sprite ->setPosition(center);
this->addChild(_sprite);
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [this](Touch* touch,Event* event){
_position = touch->getLocation();
return true;
};
listener->onTouchMoved = [](Touch* touch,Event* event)
{
float distance = touch->getLocation().x - _position.x;
auto node = event->getCurrentTarget();
node->setPositionX(node->getPositionX() + distance);
_position.x = touch->getLocation().x;
};
_eventDispatcher ->addEventListenerWithSceneGraphpriority(listener,this);
return true;
}
Sprite* TopLayer::getSprite()
{
return _sprite ;
}
BottomLayer.h
#pragma once
#include "cocos2d.h"
class BottomLayer:public Layer
{
public:
static BottomLayer*create(Sprite * sprite);
CREATE_FUNC(BottomLayer);
virtual bool init();
private:
Sprite* _sprite;
Vec2 _position;
};
BottomLayer.cpp
bool BottomLayer::init()
{
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [this](Touch* touch,Event* event){
_position = touch->getLocation();
return true;
};
listener->onTouchMoved = [](Touch* touch,Event* event)
{
float distance = touch->getLocation().y - _position.y;
_sprite->setPositionY(_sprite->getPositionY() + distance);
_position.y = touch->getLocation().y;
};
_eventDispatcher ->addEventListenerWithSceneGraphpriority(listener,this);
return true;
}
BottomLayer * BottomLayer::create(Sprite * sprite)
{
auto layer = BottomLayer::create();
layer->_sprite = sprite;
return layer;
}
边学边写,如果有哪里不正确的地方还请小伙伴们及时指出来,码字不易还请谅解!
|