Skip to content

Commit

Permalink
sound + FirstScene
Browse files Browse the repository at this point in the history
  • Loading branch information
D-W- committed Aug 3, 2014
1 parent c219b96 commit 8a2d346
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 96 deletions.
60 changes: 38 additions & 22 deletions AppDelegate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

USING_NS_CC;

#define userDefault UserDefault::getInstance()

AppDelegate::AppDelegate() {

}
Expand All @@ -12,41 +14,55 @@ AppDelegate::~AppDelegate()
}

bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLView::create("My Game");
director->setOpenGLView(glview);
}
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLView::create("My Game");
director->setOpenGLView(glview);
}

glview->setFrameSize(360,640);

//glview->setDesignResolutionSize(720 , 1280 , kResolutionShowAll);

glview->setDesignResolutionSize(320.0, 480.0, ResolutionPolicy::FIXED_WIDTH);
std::vector<std::string> searchPath;
searchPath.push_back("w640");
CCFileUtils::getInstance()->setSearchPaths(searchPath);
director->setContentScaleFactor(720.0 / 320.0);

// turn on display FPS
director->setDisplayStats(true);
// turn on display FPS
director->setDisplayStats(false);

// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / 60);
// set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0 / 60);

// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();
// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();

// run
director->runWithScene(scene);
// run
director->runWithScene(scene);

return true;
return true;
}

// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();
Director::getInstance()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
if(userDefault->getBoolForKey("Music"))
CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
Director::getInstance()->startAnimation();

// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
if(userDefault->getBoolForKey("Music"))
CocosDenshion::SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
175 changes: 111 additions & 64 deletions HelloWorldScene.cpp
Original file line number Diff line number Diff line change
@@ -1,93 +1,140 @@
#include "HelloWorldScene.h"

#define userDefault UserDefault::getInstance()

USING_NS_CC;

bool isFirstMusic = true;
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;
}
//¼ÓÁËÒ»ÐÐ×¢ÊÍ
//×¢ÊÍ commit
//commit
Size visibleSize = Director::getInstance()->getVisibleSize();
//初始化
touchedBack = 0;
exit = NULL;
Size screenSize = Director::getInstance()->getVisibleSize();
Point 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(Point(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(Point::ZERO);
this->addChild(menu, 1);

/////////////////////////////
// 3. add your codes below...
CCSprite* Background = CCSprite::create("menu/UI1.png");
Background->setPosition(ccp(screenSize.width/2,screenSize.height/2));
this->addChild(Background);
//触摸效果开启
this->setTouchEnabled(true);
//播放背景音乐
if(!userDefault->getBoolForKey("Music")){
userDefault->getBoolForKey("Music",true);
userDefault->flush();
}
int musBef = userDefault->getBoolForKey("Music");
userDefault->flush();
if(isFirstMusic & musBef)
{
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/Babkg_music.wav",true);
isFirstMusic = false;
}

// add a label shows "Hello World"
// create and initialize a label

auto label = LabelTTF::create("Hello World", "Arial", 24);

// position the label on the center of the screen
label->setPosition(Point(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
//声音开关
//打开关闭两个状态
auto on = MenuItemImage::create("menu/voice1.png","menu/voice1.png",NULL,NULL);
auto off = MenuItemImage::create("menu/voice3.png","menu/voice3.png",NULL,NULL);
//初始化状态为1
MenuItemToggle *item_Voice;
int selectId;
if (CocosDenshion::SimpleAudioEngine::sharedEngine()->isBackgroundMusicPlaying())
{
item_Voice = MenuItemToggle::createWithTarget(this, menu_selector(HelloWorld::setOptions),on,off, NULL);
AudioState = 1;
selectId = AudioState? 0 : 1;
}
else
{
item_Voice = MenuItemToggle::createWithTarget(this, menu_selector(HelloWorld::setOptions),off,on, NULL);
AudioState = 0;
selectId = AudioState? 1 : 0;
}
//int selectId = AudioState? 0 : 1;
item_Voice->setSelectedIndex(selectId);
item_Voice->setPosition(ccp(-120,-200));

// add the label as a child to this layer
this->addChild(label, 1);
CCMenu* menu = CCMenu::create(item_Voice,NULL);
this->addChild(menu);

// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
//响应键盘消息
auto keyListener = EventListenerKeyboard::create();
keyListener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(keyListener, this);

// position the sprite on the center of the screen
sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
// bind touch event实现触摸效果
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);

// add the sprite as a child to this layer
this->addChild(sprite, 0);

return true;
}


void HelloWorld::menuCloseCallback(Ref* pSender)
//声音选项,把用户的选择存储在后台
void HelloWorld::setOptions(cocos2d::Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif

Director::getInstance()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
bool temp = userDefault->getBoolForKey("Music");
userDefault->flush();
userDefault->setBoolForKey("Music",!temp);
userDefault->flush();

bool tmpSound = AudioState;
AudioState = !AudioState;

if (!tmpSound) {
CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("sound/Babkg_music.wav",true);
isFirstMusic = false;
}else{
CocosDenshion::SimpleAudioEngine::sharedEngine()->stopBackgroundMusic();
}
}
//响应安卓返回键
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keycode,Event* event)
{
exit = LabelTTF::create("Press back button again to exit", "fonts/fzmwt.ttf", 15);
if(keycode == EventKeyboard::KeyCode::KEY_BACKSPACE)
{
if(!touchedBack)
{
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
exit->runAction(CCFadeOut::create(2.0));
exit->setPosition(Point(origin.x + visibleSize.width/2,
origin.y + visibleSize.height/2));
this->addChild(exit);
touchedBack = true;
}
else
Director::getInstance()->end();
}
else
{
touchedBack = false;
if(exit)
{
exit->removeFromParentAndCleanup(true);
}
}
}
//响应触摸
bool HelloWorld::onTouchBegan(Touch *touch, Event *unused)
{
touchedBack = false;
if(exit)
{
exit->removeFromParentAndCleanup(true);
}
return touchedBack;
}
27 changes: 17 additions & 10 deletions HelloWorldScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
#include "SimpleAudioEngine.h"

USING_NS_CC;

class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
static cocos2d::Scene* createScene();
virtual bool init();
void menuCloseCallback(cocos2d::Ref* pSender);
CREATE_FUNC(HelloWorld);

// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();

// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);

// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
void setOptions(cocos2d::Ref* pSender);
//响应安卓返回键
virtual void onKeyReleased(EventKeyboard::KeyCode keycode,Event* event)override;
virtual bool onTouchBegan(Touch *touch, Event *unused) override;
private:
CCMenuItemImage *item_Voice;
bool AudioState;
LabelTTF * exit;
//记录是否按了别的地方
bool touchedBack;
};

#endif // __HELLOWORLD_SCENE_H__

0 comments on commit 8a2d346

Please sign in to comment.