Cocos2dx中零散知识点
cocos2dx中有三種定時器:schedule,scheduleUpdate,scheduleOnce。功能分別是 每隔幾秒調用自定義函數、調用系統默認的update()函數、只調用一次自定義函數
1、scheduleUpdate
加入當前節點后,程序會每幀都會自動執行一次默認的Update函數。(注:一定是Update函數哦,若想調用其他自己命名的函數則使用schedule)
看例子,走起。
首先在HelloWord類的頭文件中聲明Update函數:
void Update(float dt); //注意參數類型然后在HelloWorld類源文件中實現函數Update:
voidHelloWorld::Update(float dt)
{
CCLOG("baibai");}
現在我們可以調用了,在需要他不斷執行的地方加入調用的代碼就ok:
this->scheduleUpdate(); //this是當前節點,如layer,所以可以省略啦。運行之后你將會看到不斷有baibai被打印出來
停止方法:
this->unscheduleUpdate();
2、schedule
功能:每隔幾秒執行一次函數
首先還是在HelloWorld中聲明所要執行的函數:
void Move(float dt);然后在源文件實現:
void HelloWorld::Move(floatdt)
{
CCLOG("baibai");}
現在去執行他,注意參數哦
this->schedule(schedule_selector(HelloWorld::Move),1.0f); //每隔1.0f執行一次,省略參數則表示每幀都要執行運行之后,baibai每隔1.0f才會被打印一次。
停止方法:
this->unschedule(schedule_selector(HelloWorld::Move));
3、scheduleOnce
功能:在幾秒之后執行,并且只會執行一次。
我們就執行上面所寫的Move函數吧。
this->scheduleOnce(schedule_selector(HelloWorld::Move),1.0f); //在1.0f之后執行,并且只執行一次。
運行一下,baibai只是被打印了一次就完了。。。
ok,定時器的調用已經講完了,大家不妨自己寫一些函數體驗一下。
4、停止所有計時器
this->unscheduleAllSelectors()
CCNode類的setPosition,getPosition函數如果是一個Node的Child則獲取的坐標就是該Node的本地坐標
另一個關鍵問題就是在cocos2d-x里就是各種對象的大小問題。因為在cocos2d-x里CCNode對象有縮放的方法setScaleX和setScaleY。所以在獲取對象大小的時候必須根據情況明確指定獲取對象原始大小,還是縮放后的大小。當然cocos2d-x里提供了對應函數來完成這些操作:
getContentSize函數來獲得節點原始的大小。只是邏輯尺寸,不是像素
boundingBox函數來獲得經過縮放和旋轉之后的外框盒大小。
getContentSizeInPixels獲得的是像素點大小
像素點和邏輯點關系:邏輯點大小 = 像素大小
getVisibleSize:默示獲得視口(可視區域)的大小,若是DesignResolutionSize跟屏幕尺寸一樣大,則getVisibleSize便是getWinSize。
getVisibleOrigin:默示可視區域的出發點坐標,這在處理懲罰相對地位的時辰很是有效,確保節點在不合辨別率下的地位一致。
坐標轉換:
GL坐標系,cocos2d-x默認坐標系:
CCPoint CCDirector::convertToGL(const CCPoint& uiPoint)
{
CCSize s = m_obWinSizeInPoints;
float newY = s.height - uiPoint.y;
}
屏幕坐標系: 默認原點在左上角
CCPoint CCDirector::convertToUI(const CCPoint& glPoint)
{
CCSize winSize = m_obWinSizeInPoints;
float oppositeY = winSize.height - glPoint.y;
}
兩種坐標的X方向沒有變,只變了Y方向,cocos2d-x里默認的GL坐標系,即左下角為原點ccp(0.0f,0.0f)
// 創建精靈的五種方法 //方法一:直接創建精靈 //適合于要顯示的是這張圖片的全部區域, CCSprite * sprite = CCSprite::create("Icon.png"); //上面那句話也可以根據需要這樣來寫: //CCString* fileName = CCString::createWithFormat("Icon_%d.jpg", flag); //CCSprite* sprite = CCSprite::create(fileName->getCString()); sprite->setPosition(ccp(100, 100)); this->addChild(sprite); // 方法二:參數 圖片名稱 矩形區域 //適合于需要顯示此圖片的部分區域 CCSprite * sprite = CCSprite::create("Icon.png",CCRectMake(0, 0, 30, 30)); sprite->setPosition(ccp(100, 100)); this->addChild(sprite); //方法三: 利用幀緩存中的一幀的名稱聲稱一個對象 // 適合于plist打包好的文件 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("test_icon.plist"); CCSprite * sprite = CCSprite::createWithSpriteFrameName("Icon.png"); sprite->setPosition(ccp(100, 100)); this->addChild(sprite); //方法四: 利用另外一幀生成一個精靈對象 //適合于做幀動畫使用 CCSpriteFrame * frame = CCSpriteFrame::create("Icon.png", CCRectMake(0, 0, 40, 30)); CCSprite * sprite = CCSprite::createWithSpriteFrame(frame); sprite->setPosition(ccp(310, 150)); addChild(sprite); //方法五:利用紋理, //適合于需要頻繁使用的圖片 CCSpriteBatchNode* spriteTexture = CCSpriteBatchNode::create("iocn.png"); spriteTexture->setPosition(CCPointZero); addChild(spriteTexture); CCSprite* sprite = CCSprite::createWithTexture(spriteTexture->getTexture()); sprite->setPosition(ccp(visiblesize.width/2, 100)); spriteTexture->addChild(sprite, 2);常用的封裝方法
//返回場景 static CCScene* scene(CCLayer*layer){CCScene *scene=CCScene::create();scene->addChild(layer);//scene->autorelease();return scene;} //移動點static void moveNode(CCNode *node,CCPoint point){node->setPosition(node->getPosition()+point);} //格式化,串接字符串static char* format(int v, const char* prefix = "", const char* suffix = ""){static char buf[2048];sprintf(buf, "%s%d%s", prefix, v, suffix);return buf;} //創建動畫static CCAnimation* CreateAnimation(const char* filename,int start,int end,int width,float delay){CCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage(filename);CCArray *array=CCArray::create();for (int i=start;i<end;i++){CCSpriteFrame *frame=CCSpriteFrame::createWithTexture(texture,CCRectMake(i*width,0,width,texture->getContentSize().height));array->addObject(frame);}return CCAnimation::createWithSpriteFrames(array,delay);} //創建幀static CCSpriteFrame* getSpriteFrame(const char* filename, int pos, int width){CCTexture2D* texture = CCTextureCache::sharedTextureCache()->addImage(filename);CCSpriteFrame* frame = CCSpriteFrame::createWithTexture(texture, CCRectMake(pos*width, 0, width, texture->getContentSize().height));return frame;}//地圖坐標轉格子地圖static CCPoint Point2Tile(CCTMXTiledMap* map, CCPoint ptInMap){int dx = map->getTileSize().width;int dy = map->getTileSize().height;int x = ptInMap.x / dx;int y = ptInMap.y / dy;y = map->getMapSize().height - 1 - y;return ccp(x, y);} //格子地圖坐標轉地圖坐標static CCPoint Tile2PointLB(CCTMXTiledMap* map, CCPoint ptTile){ptTile.y = map->getMapSize().height - 1 - ptTile.y;return ccp(ptTile.x * map->getTileSize().width,ptTile.y * map->getTileSize().height);}總結
以上是生活随笔為你收集整理的Cocos2dx中零散知识点的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Volley学习总结
- 下一篇: 【每日SQL打卡】