【说明】
本篇整理学习过程中用到的一些小技巧,以便之后使用,后续不断补充。
【1. Cocos2d-x 3.x截屏】
- utils::captureScreen(CC_CALLBACK_2(HelloWord::screenShareEx,this),"screen.png");
【2. 获取本地时间】
- //获取UNIX时间(时间戳)
- std::stringgetUnixTime()
- {
- //获取时间
- time_ttimep;
- time(&timep);
- longlTime=timep;
- charsTime[16]={0};
- sprintf(sTime,"%ld",lTime);
- returnsTime;
- }
- //获取本地时间
- std::stringgetStandardTime()
- {
- //获取时间
- structtm*tm;
- time_ttimep;
- #if(CC_TARGET_PLATFORM==CC_PLATFORM_WIN32)
- time(&timep);
- #else
- structtimevaltv;
- gettimeofday(&tv,NULL);
- timep=tv.tv_sec;
- #endif
- tm=localtime(&timep);
- intyear=tm->tm_year+1900;
- intmonth=tm->tm_mon+1;
- intday=tm->tm_mday;
- inthour=tm->tm_hour;
- intminute=tm->tm_min;
- intsecond=tm->tm_sec;
- charsTime[16]={0};
- sprintf(sTime,"%04d%02d%02d%02d%02d%02d",year,month,day,hour,minute,second);
- returnsTime;
- }
- //获取本地时间戳
- intgetTimeStamp()
- {
- timevaltm;
- gettimeofday(&tm,NULL);
- returntm.tv_sec;//单位:秒
- //returntm.tv_usec;//单位:毫秒
- }
- //获取本地时间
- {
- time_tt=time(NULL);
- tm*lt=localtime(&t);
- intyear=lt->tm_year+1900;//相对1900年的过去的年数
- intmonth=lt->tm_mon+1;//1月份:为0
- intyday=lt->tm_yday;//年第几天:从1开始
- intmday=lt->tm_mday;//月第几天:从1开始
- intwday=lt->tm_wday;//周第几天:从1开始
- inthh=lt->tm_hour;//时
- intmm=lt->tm_min;//分
- intss=lt->tm_sec;//秒
- printf("%d%d\n",month);
- printf("%d%d%d\n",yday,mday,wday);
- printf("%d%d%d\n",hh,mm,ss);
- }
- //横屏
- android:screenOrientation="landscape"
- //竖屏
- android:screenOrientation="portrait"
- //做应用时很多时候都得获取到每个设备的机器码
- Secure.getString(getContext().getContentResolver(),Secure.ANDROID_ID)
- //正常情况下,你想得到设备的唯一序号,TelephonyManager.getDeviceId()就足够了。
- //但会暴露DeviceID,最好把这些id加密。加密后的序号仍然可以唯一的识别该设备,
- //例如,使用String.hashCode(),结合UUID:
- finalTelephonyManagertm=(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
- finalStringtmDevice,tmSerial,tmPhone,androidId;
- tmDevice=""+tm.getDeviceId();
- tmSerial=""+tm.getSimSerialNumber();
- androidId=""+android.provider.Settings.Secure.getString(getContentResolver(),android.provider.Settings.Secure.ANDROID_ID);
- UUIDdeviceUuid=newUUID(androidId.hashCode(),((long)tmDevice.hashCode()<<32)|tmSerial.hashCode());
- StringuniqueId=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>
- boolcreateDirectory(conststd::string&dirpath)
- {
- #if(CC_TARGET_PLATFORM!=CC_PLATFORM_WIN32)
- DIR*pDir=opendir(dirpath.c_str());//打开目录
- if(!pDir)
- {
- //如果目录不存在,则创建目录
- intret=mkdir(dirpath.c_str(),S_IRWXU|S_IRWXG|S_IRWXO);
- if(ret!=0&&errno!=EEXIST)
- {
- returnfalse;
- }
- }
- returntrue;
- #else
- if((GetFileAttributesA(dirpath.c_str()))==INVALID_FILE_ATTRIBUTES)
- {
- BOOLret=CreateDirectoryA(dirpath.c_str(),NULL);
- if(!ret&&ERROR_ALREADY_EXISTS!=GetLastError())
- {
- returnfalse;
- }
- }
- returntrue;
- #endif
- }
- </pre><strong><spanstyle="font-size:18px">【6.屏幕旋转】</span></strong><prename="code"class="cpp">//屏幕旋转
- //可以通过getDeviceOrientation和setDeviceOrientation来分别获得屏幕的朝向和设置相应的屏幕朝向。
- //这个例子就是根据屏幕的朝向再通过setDeviceOrientation设置屏幕的朝向
- switch(s_currentOrientation)
- {
- caseCCDeviceOrientationLandscapeLeft:
- s_currentOrientation=CCDeviceOrientationPortrait;
- break;
- caseCCDeviceOrientationPortrait:
- s_currentOrientation=CCDeviceOrientationPortraitUpsideDown;
- break;
- caseCCDeviceOrientationPortraitUpsideDown:
- s_currentOrientation=CCDeviceOrientationLandscapeLeft;
- break;
- }
- CCDirector::shareDirector()->setDeviceOrientation(s_currentOrientation);
【7. 随机浮点数】
- #defineRAND_LIMIT32767
- floatRandomFloat()
- {
- floatr=(float)(std::rand()&(RAND_LIMIT));
- r/=RAND_LIMIT;
- r=2.0f*r-1.0f;
- returnr;
- }
- //用了scrollview和pageview的场景在小米,华为等手机跑显示不正常,会大部分白屏。
- //把这个文件AppActivity.java里的内容改成这样
- importorg.cocos2dx.lib.Cocos2dxActivity;
- importorg.cocos2dx.lib.*;
- publicclassAppActivityextendsCocos2dxActivity
- {
- publicCocos2dxGLSurfaceViewonCreateView()
- {
- Cocos2dxGLSurfaceViewglSurfaceView=newCocos2dxGLSurfaceView(this);
- //glSurfaceView.setEGLConfigChooser(5,6,5,16,8);
- glSurfaceView.setEGLConfigChooser(8,8,8);
- returnglSurfaceView;
- }
- }
来自:http://blog.csdn.net/ldpjay/article/details/46986377