【说明】
本篇整理学习过程中用到的一些小技巧,以便之后使用,后续不断补充。
【1. Cocos2d-x 3.x截屏】
- utils::captureScreen(CC_CALLBACK_2(HelloWord::screenShareEx,this),"screen.png");
【2. 获取本地时间】
- 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;
- }
- {
- time_tt=time(NULL);
- tm*lt=localtime(&t);
- intyear=lt->tm_year+1900;
- intmonth=lt->tm_mon+1;
- intyday=lt->tm_yday;
- intmday=lt->tm_mday;
- intwday=lt->tm_wday;
- inthh=lt->tm_hour;
- intmm=lt->tm_min;
- intss=lt->tm_sec;
- printf("%d%dn",month);
- printf("%d%d%dn",yday,mday,wday);
- printf("%d%d%dn",hh,mm,ss);
- }
【3. Android横屏竖屏】
- android:screenOrientation="landscape"
- android:screenOrientation="portrait"
【4. Android获取机器码】
- Secure.getString(getContext().getContentResolver(),Secure.ANDROID_ID)
- 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();
【5. 创建文件夹】
- 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">
- 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;
- }
【8. 使用了scrollview出现白屏】
- importorg.cocos2dx.lib.Cocos2dxActivity;
- importorg.cocos2dx.lib.*;
- publicclassAppActivityextendsCocos2dxActivity
- {
- publicCocos2dxGLSurfaceViewonCreateView()
- {
- Cocos2dxGLSurfaceViewglSurfaceView=newCocos2dxGLSurfaceView(this);
- glSurfaceView.setEGLConfigChooser(8,8,8);
- returnglSurfaceView;
- }
- }
来自:http://blog.csdn.net/ldpjay/article/details/46986377
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|