coco2dx開發的小總結篇章

1:didAccelerate(CCAcceleration *pAccelerationValue) 得到重力改變的函數 就是你想做一個搖晃類遊戲得到z的變換就是這個接口。setIsAccelerometerEnabled(true);

2:voidvisit()

{

CCSize winSize = CCDirector::sharedDirector()->getWinSize();

CCSize winSizeInPixels = CCDirector::sharedDirector()->getWinSizeInPixels();

float scaleX = winSizeInPixels.width / ORIGIN_WIDTH;

float scaleY = winSizeInPixels.height / ORIGIN_HEIGHT;

glEnable(GL_SCISSOR_TEST);

glScissor(0 , 100, winSize.width,300);

CCLayer::visit();

glDisable(GL_SCISSOR_TEST);

} //這個就是j2me裏面的setclip啦。設置當前層的c裁剪區域。

3:隨機數 arc4random(); 或者 srand((unsigned)time(NULL));//必須要 不然種子不會變得到的隨機數永遠都是一樣的 rand()%100;

4:刪除文件 c語言自帶:int r = remove(“path”); //刪除文件

5:文件以及數據庫操作需要做線程安全處理;

6:圖層layer出錯 可以使用函數設置成2d模式再試試:CCDirector::sharedDirector()->setProjection(CCDirectorProjection2D);

7: 二進制png生成sprite:

CCImage image;

unsigned long nSize = pngLength;

unsigned char* pBuffer = pngDatas;

image.initWithImageData((void*)pBuffer, nSize, CCImage::kFmtPng); CCTexture2D* texture = new CCTexture2D();

texture->initWithImage(&image);

if (!texture)

{

return false;

}

CCSprite* sprite = CCSprite::spriteWithTexture(texture);

texture->release();

8:c語言下判斷文件時候存在

用函數access,頭文件是stdio.h,原型:

int access(const char *filename, int amode);

amode參數爲0時表示檢查文件的存在性,如果文件存在,返回0,不存在,返回-1。

這個函數還可以檢查其它文件屬性:

06 檢查讀寫權限
04 檢查讀權限
02 檢查寫權限
01 檢查執行權限
00 檢查文件的存在性
在UNIX和VC下實驗成功。
好處是 fopen(..,"r")不好,當無讀權限時一不行了。
而這個就算這個文件沒有讀權限,也可以判斷這個文件存在於否
存在返回0,不存在返回-1
#include <stdio.h>
int main()
{
printf ("%d",access("111",0));
}

9:char c[10]; sprintf(c,"123456789012");

printf("%s",c);

居然能全部打印出來,這個隱形錯誤讓我鬱悶了2個小時,哎呀呀呀。bt

10:閃屏問題 看中文網站http://cn.cocos2d-x.org/resource/show?nid=80 第二個方法

11:日期相關 得到日期 並計算相差的天數 以日爲單位

structcc_timeval now;

if( CCTime::gettimeofdayCocos2d(&now,NULL) == 0)

{

structtm *t;

t = localtime(&now.tv_sec);

int nowYear = t->tm_year+1900 ,nowMonth = t->tm_mon+1,nowDay = t->tm_mday;

char time[100];

sprintf(time,"%d-%d-%d",nowYear,nowMonth,nowDay);

if (time.length() > 0)

{

char* token = strtok((char*)timeStr.c_str(),"-");

int lastYear,lastMonth,lastDay;

int j = 0;

while( token != NULL )

{

if (j == 0)

{

lastYear = atoi(token);

}

elseif (j == 1)

{

lastMonth = atoi(token);

}

elseif ( j == 2)

{

lastDay = atoi(token);

}

j++;

token = strtok( NULL,"-");

}

CCLog("lastYear:%d lastMonth:%d lastDay:%d",lastYear,lastMonth,lastDay);

int disDays = getDays(nowYear, nowMonth, nowDay, lastYear, lastMonth, lastDay);

CCLog("disDay:%d",disDays);

}

int getDays(int year,int month,int day,int lastYear,int lastMonth,int lastDay)

{

int iTotalDays = 0;

int iMonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; // Initiate month

if (isLeapYear(lastYear))

{

iMonth[2] = 29;

}

else

{

iMonth[2] = 28;

}

while (!(year == lastYear && month == lastMonth && day == lastDay)) {

lastDay++;

if ( lastDay == iMonth[lastMonth]) {

lastDay = 0;

lastMonth++;

}

if (lastMonth == 13) {

lastMonth = 1;

lastYear ++;

if (isLeapYear(lastYear))

{

iMonth[2] = 29; // Leap year February is 29 days.

}

else

{

iMonth[2] = 28;

}

}

iTotalDays++;

}

return iTotalDays;

}

bool isLeapYear(int year)

{

bool isLeapYear = false;

if ((year%4==0 && year%100) || year%400 ==0 )

{

isLeapYear = true;

}

return isLeapYear;

}

12:uiview 旋轉這隻位置

// CGAffineTransform transform = adView.transform; //旋轉

// transform = CGAffineTransformRotate(transform, 3.14F/2);

// adView.transform = transform;

// adView.center = CGPointMake(200, 500); //中心點設置座標

// 設置位置

CGRect frame = adView.frame;

13:c/c++ 隨機數使用前只需要初始化一次隨機數種子 srand((unsigned)time(NULL)); //初始化隨機數種子

不然多次初始化太消費cpu了。

frame.origin.x = 0;

frame.origin.y = 0;

adView.frame = frame;

13:os下的定時器

// [NSTimer scheduledTimerWithTimeInterval: 90

// target: self

// selector: @selector(handleTimer)

// userInfo: nil

// repeats: NO];

 

 

轉載地址:http://hi.baidu.com/baby_66_/blog/item/d6e680f26a9dd1969f51463d.html


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章