您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x手游技术分享(1)-【天天打蚊子】数据存储与音效篇

2013-09-10 11:45 441 查看
前言:

  手游项目《天天打蚊子》终于上线,特地写几篇技术分享文章,分享一下其中使用到的技术,其中使用cocos2d-x引擎,首选平台iOS,也请有iPhone或者iPad的朋友帮忙下载好评。十分感谢。

目前完美支持iPhone4、iPhone 4S、iPhone 5、iPad所有版本,iOS 5以上开始支持。

  目前开发团队3个人,本人客户端+服务端,另有1名客户端,1名美术,目前创业刚刚起步,请各位好友支持!

  《天天打蚊子》下载地址:https://itunes.apple.com/cn/app/id681203794

  

一、Cocos2d-x中的数据存储。

1、CCUserDefault数据存储

CCUserDefault头文件:

#import "Keychain.h"

@implementation Keychain

//keychain
+ (NSMutableDictionary *)getKeychainQuery:(NSString *)service
{
NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
(id)kSecClassGenericPassword,(id)kSecClass,
service, (id)kSecAttrService,
service, (id)kSecAttrAccount,
(id)kSecAttrAccessibleAfterFirstUnlock,(id)kSecAttrAccessible,
nil];
return dict;
}

+ (void)saveData:(NSString *)service data:(id)data
{
//Get search dictionary
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Delete old item before add new item
SecItemDelete((CFDictionaryRef)keychainQuery);
//Add new object to search dictionary(Attention:the data format)
[keychainQuery setObject:[NSKeyedArchiver archivedDataWithRootObject:data] forKey:(id)kSecValueData];
//Add item to keychain with the search dictionary
SecItemAdd((CFDictionaryRef)keychainQuery, NULL);
}

+ (id)loadData:(NSString *)service
{
id ret = nil;
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
//Configure the search setting
//Since in our simple case we are expecting only a single attribute to be returned (the password) we can set the attribute kSecReturnData to kCFBooleanTrue
[keychainQuery setObject:(id)kCFBooleanTrue forKey:(id)kSecReturnData];
[keychainQuery setObject:(id)kSecMatchLimitOne forKey:(id)kSecMatchLimit];
CFDataRef keyData = NULL;
if (SecItemCopyMatching((CFDictionaryRef)keychainQuery, (CFTypeRef *)&keyData) == noErr) {
@try {
ret = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)keyData];
} @catch (NSException *e) {
CLog(@"Unarchive of %@ failed: %@", service, e);
} @finally {
}
}
if (keyData)
CFRelease(keyData);
return ret;
}

+ (void)deleteData:(NSString *)service
{
NSMutableDictionary *keychainQuery = [self getKeychainQuery:service];
SecItemDelete((CFDictionaryRef)keychainQuery);
}

@end


keychain.m

  下面用keychain记录本地账号:

DBInfoAccount.h

#include "cocos2d.h"

using namespace cocos2d;

class DBInfoAccount {

public:

int m_iUserID;
CCString * m_pAccountID;
CCString * m_pPassword;

DBInfoAccount();
static DBInfoAccount * getInstance();
bool isHasAccount();

//数据存取
void loadData();
static void saveData();
static void reset();
};


DBInfoAccount.mm

#import "Keychain.h"
#include "DBInfoAccount.h"

//#define kAccount                @"Account"
#define kAccountAccountID        @"Account_Account_ID"
#define kAccountUserID            @"Account_UserID"
#define kAccountPassword        @"Account_Pwd"

static DBInfoAccount * s_pDBInfoAccount = NULL;

DBInfoAccount::DBInfoAccount():
m_iUserID(0)
{
m_pAccountID = new CCString("");
m_pPassword = new CCString("");
}

DBInfoAccount * DBInfoAccount::getInstance()
{
if (s_pDBInfoAccount == NULL) {
s_pDBInfoAccount -> loadData();
if (s_pDBInfoAccount == NULL) {
s_pDBInfoAccount = new DBInfoAccount;
}
}

return s_pDBInfoAccount;
}

bool DBInfoAccount::isHasAccount()
{
return m_pAccountID!= NULL && m_pAccountID -> length()>0;
}

void DBInfoAccount::loadData()
{
if (s_pDBInfoAccount == NULL) {
NSString * accountID = [Keychain loadData: kAccountAccountID];
NSString * userID = [Keychain loadData: kAccountUserID];
NSString * password = [Keychain loadData: kAccountPassword];
s_pDBInfoAccount = new DBInfoAccount;
const char * c_accountID = [accountID cStringUsingEncoding:NSUTF8StringEncoding];
DWORD c_userID = [userID  intValue];
const char * c_password = [password cStringUsingEncoding:NSUTF8StringEncoding];

if (c_accountID != NULL && c_password != NULL) {
s_pDBInfoAccount -> m_pAccountID -> m_sString = c_accountID;
s_pDBInfoAccount -> m_iUserID  = c_userID;
s_pDBInfoAccount -> m_pPassword -> m_sString = c_password;
}
}
}

void DBInfoAccount::saveData()
{
NSString * accountID = [NSString stringWithCString:s_pDBInfoAccount->m_pAccountID->getCString() encoding:NSUTF8StringEncoding];
NSString * userID = [NSString stringWithFormat:@"%d",s_pDBInfoAccount->m_iUserID];
NSString * password = [NSString stringWithCString:s_pDBInfoAccount->m_pPassword->getCString() encoding:NSUTF8StringEncoding];
[Keychain saveData:kAccountAccountID data: accountID];
[Keychain saveData:kAccountUserID data: userID];
[Keychain saveData:kAccountPassword data: password];
}

void DBInfoAccount::reset()
{
[Keychain deleteData:kAccountAccountID];
[Keychain deleteData:kAccountUserID];
[Keychain deleteData:kAccountPassword];
}


  具体用法一目了然,不再赘述。

二、音效管理。

  废话不说,直接上代码:

void DataHelper::initMusicAndEffect()
{
bool isEffectEnabel = DataHelper::getEffectEnable();
CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(isEffectEnabel);

bool isMusicEnable = DataHelper::getMusicEnable();
CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(isMusicEnable);
}
bool DataHelper::getEffectEnable()
{
return  CCUserDefault::sharedUserDefault()->getBoolForKey("Effect_Enable", true);
}
bool DataHelper::getMusicEnable()
{
return  CCUserDefault::sharedUserDefault()->getBoolForKey("Music_Enable", true);
}
void DataHelper::setEffectEnable(bool isEnable)
{
CCUserDefault::sharedUserDefault()->setBoolForKey("Effect_Enable", isEnable);
CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(isEnable);
}
void DataHelper::setMusicEnable(bool isEnable)
{
CCUserDefault::sharedUserDefault()->setBoolForKey("Music_Enable", isEnable);
CocosDenshion::SimpleAudioEngine::sharedEngine()->setBackgroundMusicVolume(isEnable);
}


  其中,CocosDenshion::SimpleAudioEngine::sharedEngine()->setEffectsVolume(isEffectEnabel),setEffectsVolume为设置的音效大小(0~1)的方法,这里使用false(0)和true(1)直接设置,实现是否设置为静音。

  至于播放背景音乐和音效,最好使用mp3格式的,iOS和安卓同时支持。

  具体:

CocosDenshion::SimpleAudioEngine::sharedEngine()->playBackgroundMusic("bg.mp3", true);//播放背景音乐,第二个参数为是否循环播放

CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("anjian.mp3");//播放音效,比如按键声


《天天打蚊子》下载地址:https://itunes.apple.com/cn/app/id681203794

请各位多多支持,给个好评呀!谢谢!或者扫描二维码下载:



游戏视频:

后续cocos2d-x技术文章陆续放出,敬请关注!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: