Cocos2d和UIKit的結合使用

2022-08-21 22:18:06 字數 1466 閱讀 8949

額,題目寫的有點大,其實這篇隨筆只是想mark下cocos2d中對uiview的使用。

cocos2d中想使用uiview等uikit系的控制項,最常用常見的方法,就是通過openglview來做,雖然這個非常簡單,還是簡述下吧。

比如,現在我想在cocos2d中使用uiimageview這個控制項,非常簡單,直接上**。

cgrect frame = [[[ccdirector shareddirector] openglview] frame];

_animateimageview = [[uiimageview alloc] initwithframe:frame];

[[[ccdirector shareddirector] openglview] addsubview:_animateimageview];

不用了的時候。

[_animateimageview release];

[_animateimageview removefromsuperview];

諾,這樣呢,有乙個問題,使用的uiimageview若不remove掉的話總是顯示在最上面,再加個sprite啥的也加不上去。恰好,這次需要的圖比較大(320*480),還想借助uiimageview的動畫功能,這下鬱悶了。

怎麼樣才能即能讓他順利展示又能再上面新增sprite或者ccmenu等呢。

ok, 直接給出解決方案吧。因為一旦採用openglview addsubview的話肯定是不行的,那乙個解決思路是在openglview下插入一層view, 同時把openglview設定成透明的,這樣不就行了。

//

set glview by wordsworth mar.26

[glview setmultipletouchenabled:yes];

glview.opaque = no;

glclearcolor(0.0f, 0.0f, 0.0f, 0.0f);

glclear(gl_color_buffer_bit);

//add a view by wordsworth mar.26, in order to insert another view in mainboardlayer

overview = [[uiview alloc] initwithframe:[[uiscreen mainscreen] bounds]];

overview.opaque = no;

overview.backgroundcolor = [uicolor clearcolor];

[overview addsubview:glview];

[window addsubview:overview];

最後,把我們的imageview盡情的寫入openglview下面吧,這樣在上面加上各種各樣的ccsprite、ccmenu等不用擔心不顯示啦。

其他的uikit控制項也差不多可以按這個套路來了。

cocos2d實現語音 Cocos2d 聲音API

param url 聲音路徑 cc.audioengine.playmusic url loop 停止背景 param releasedata 是否釋放聲音資料,預設為false cc.audioengine.stopmusic releasedata 暫停背景 cc.audioengine.pau...

cocos2d筆記 cocos2d的單例模式

cocos2d充分使用了單例設計模式,這裡有必要提及它是因為它是經常是討論熱點。大體上說,乙個singleton是乙個類,它在應用使用期中僅僅例項化一次。為了確保如此,就要使用乙個既建立又訪問此物件例項的靜態方法。因此,你不使用alloc init或者靜態自動釋放初始化器 static autore...

Cocos2D研究院之初識Cocos2d(二)

在正式介紹cocos2d之前,我先囉嗦一下本人對這套引擎的看法。我認為coos2d最大的作用就是降低開發成本 貌似所有引擎都是這樣 對於研發部門來說,最大的成本就是時間,所以節省時間是降低成本最有效的方式,cocos2d幾乎封裝了所有開發ios遊戲需要的底層 使剛剛接觸oc的開發者也能很快的製作出效...