-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
166 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters