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

【总结】Cocos2d-x实用方法整理

2015-07-21 15:04 447 查看
【说明】

本篇整理学习过程中用到的一些小技巧,以便之后使用,后续不断补充。

【1. Cocos2d-x 3.x截屏】

utils::captureScreen(CC_CALLBACK_2(HelloWord::screenShareEx, this), "screen.png");


【2. 获取本地时间】

// 获取UNIX时间(时间戳)
std::string getUnixTime()
{
// 获取时间
time_t timep;
time(&timep);

long lTime = timep;
char sTime[16] = {0};
sprintf(sTime, "%ld", lTime);

return sTime;
}

// 获取本地时间
std::string getStandardTime()
{
// 获取时间
struct tm *tm;
time_t timep;
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
time(&timep);
#else
struct timeval tv;
gettimeofday(&tv,NULL);
timep = tv.tv_sec;
#endif
tm = localtime(&timep);
int year = tm->tm_year + 1900;
int month = tm->tm_mon + 1;
int day = tm->tm_mday;
int hour=tm->tm_hour;
int minute=tm->tm_min;
int second=tm->tm_sec;

char sTime[16] = {0};
sprintf(sTime, "%04d%02d%02d%02d%02d%02d", year, month, day, hour, minute, second);

return sTime;
}
也可以这样写:

// 获取本地时间戳
int getTimeStamp()
{
timeval tm;
gettimeofday(&tm, NULL);
return tm.tv_sec;    // 单位:秒
//    return tm.tv_usec; // 单位:毫秒
}
//获取本地时间
{
time_t t = time(NULL);
tm* lt = localtime(&t);

int year = lt->tm_year + 1900;  // 相对1900年的过去的年数
int month = lt->tm_mon + 1;     // 1月份:为0

int yday = lt->tm_yday;         // 年第几天:从1开始
int mday = lt->tm_mday;         // 月第几天:从1开始
int wday = lt->tm_wday;         // 周第几天:从1开始

int hh = lt->tm_hour;           // 时
int mm = lt->tm_min;            // 分
int ss = lt->tm_sec;            // 秒

printf("%d %d\n", year, month);
printf("%d %d %d\n", yday, mday, wday);
printf("%d %d %d\n", hh, mm, ss);
}
【3. Android横屏竖屏】

// 横屏
android:screenOrientation="landscape"
// 竖屏
android:screenOrientation="portrait"
【4. Android获取机器码】

//做应用时很多时候都得获取到每个设备的机器码
Secure.getString(getContext().getContentResolver(), Secure.ANDROID_ID)

//正常情况下,你想得到设备的唯一序号, TelephonyManager.getDeviceId() 就足够了。
//但会暴露DeviceID,最好把这些id加密。加密后的序号仍然可以唯一的识别该设备,
//例如,使用 String.hashCode() ,结合UUID:
final TelephonyManager tm = (TelephonyManager) getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
final String tmDevice, tmSerial, tmPhone, androidId;
tmDevice = "" + tm.getDeviceId();
tmSerial = "" + tm.getSimSerialNumber();
androidId = "" + android.provider.Settings.Secure.getString(getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
UUID deviceUuid = new UUID(androidId.hashCode(), ((long)tmDevice.hashCode() << 32) | tmSerial.hashCode());
String uniqueId = deviceUuid.toString();

//最后的deviceID可能是这样的结果: 00000000-54b3-e7c7-0000-000046bffd97

【5. 创建文件夹】

// 创建文件夹,在AssertManager里面有源码
mkdir(pszPath.c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
// 具体实现代码:
#include <sys/stat.h>
#include <dirent.h>
bool createDirectory(const std::string& dirpath)
{
#if (CC_TARGET_PLATFORM != CC_PLATFORM_WIN32)
DIR *pDir = opendir(dirpath.c_str()); // 打开目录
if (!pDir)
{
// 如果目录不存在,则创建目录
int ret = mkdir(dirpath.c_str(), S_IRWXU|S_IRWXG|S_IRWXO);
if (ret != 0 && errno != EEXIST)
{
return false;
}
}
return true;
#else
if ((GetFileAttributesA(dirpath.c_str())) == INVALID_FILE_ATTRIBUTES)
{
BOOL ret = CreateDirectoryA(dirpath.c_str(), NULL);
if (!ret && ERROR_ALREADY_EXISTS != GetLastError())
{
return false;
}
}
return true;
#endif
}


</pre><strong><span style="font-size:18px">【6. 屏幕旋转】</span></strong><pre name="code" class="cpp">//屏幕旋转
//可以通过getDeviceOrientation和setDeviceOrientation来分别获得屏幕的朝向和设置相应的屏幕朝向。
//这个例子就是根据屏幕的朝向再通过setDeviceOrientation设置屏幕的朝向
switch (s_currentOrientation)
{
case CCDeviceOrientationLandscapeLeft:
s_currentOrientation = CCDeviceOrientationPortrait;
break;
case CCDeviceOrientationPortrait:
s_currentOrientation = CCDeviceOrientationPortraitUpsideDown;
break;
case CCDeviceOrientationPortraitUpsideDown:
s_currentOrientation = CCDeviceOrientationLandscapeLeft;
break;
}
CCDirector::shareDirector()->setDeviceOrientation(s_currentOrientation);

【7. 随机浮点数】

#define    RAND_LIMIT    32767
float RandomFloat()
{
float r = (float)(std::rand() & (RAND_LIMIT));
r /= RAND_LIMIT;
r = 2.0f * r - 1.0f;
return r;
}
【8. 使用了scrollview出现白屏】

//用了scrollview和pageview的场景在小米,华为等手机跑显示不正常,会大部分白屏。
//把这个文件AppActivity.java里的内容改成这样
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.*;
public class AppActivity extends Cocos2dxActivity
{
public Cocos2dxGLSurfaceView onCreateView()
{
Cocos2dxGLSurfaceView glSurfaceView = new Cocos2dxGLSurfaceView(this);
//glSurfaceView.setEGLConfigChooser(5, 6, 5, 0, 16, 8);
glSurfaceView.setEGLConfigChooser(8 , 8, 8, 8, 16, 8);
return glSurfaceView;
}
}


TODO...
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: