cocos2d-iphone中兼容iphone/ipad的問題

咱們知道在IOS開發中,要兼容iphone/ipad,只要把開發的版本設置爲universal版就能夠了,可是設置是比較簡單,而裏面的素材的大小和位置倒是比較難控制的。緣由是iphone與ipad的比例是不統一的,iphone是(480*320/960*640)3:2的屏,而ipad的屏幕是(1024*768,New ipad是2048*1536)4:3的屏。
在cocos2d-iphone中有一個很重要的方法ccp,這個方法是用於建立CGPoint。爲了讓IOS遊戲的資源最小,咱們開發了Universal版本,讓其共用一套資源。因爲在IOS中的高清屏(Retina960*640)比普通版本(480*320)大兩倍。爲了更加接近真實,咱們在Ipad中採用iphone中的Retina版本素材。如今介紹一下咱們的思路:
一、對ccp方法的擴展
    因爲在前期開發的過程當中並無考慮Universal版本,在2.0的時候要擴展,因此之前的位置不少採用的是ccp,位置用的是iphone版本,有些是寫的數字,因此要對這個方法進行擴展。咱們定義了另一個方法fcccf,這個方法能夠根據系統的設備返回真實位置,傳入的參數爲在iphone上的X,Y軸。
#define IsPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)//定義是不是Ipad的宏
#define fcccf(__x__,__y__) GetPointbyPhoneXandY(__x__,__y__)//爲了與ccp用法一致
/*
     * 傳入Iphone位置
     */
    CG_INLINE CGPoint GetPointbyPhoneXandY(CGFloat x,CGFloat y){
        CGPoint p;
        if (IsPad) {
            //p=ccp((x+16)*2, (y+32)*2);
            p=ccp(x/0.46875, y/0.41667);
        }
        else{
            p=ccp(x, y);
        }
        return p;
    }
二、重寫cocos2d-iphone中的一些基類方法
    咱們知道在cocos2d-iphone中若是傳入普通素材路徑而又支持Retina版本的話,cocos2d-iphone會自動識別爲Retina版本素材,其原則是(1)png,jpg圖片在後綴前加@2x,例如icon.png和icon@2x.png,(2)對於plist文件和pvr.ccz文件(圖片壓縮處理)的文件則加「-hd」,例如KeyButton.plist與KeyButton-hd.plist。跟蹤到咱們能夠發現其實在組織資源實,cocos2d-iphone把資源的文件名已經再處理一遍,基於這種思路。咱們也能夠在ipad中對文件名進行處理。
cocos2d-iphone中主要的基類有CCSprite和CCLayer,咱們重寫一個基類命名爲FCBaseSprite:CCSprite,重寫+(id)spriteWithFile:(NSString *)filename、+(id)spriteWithSpriteFrameName:(NSString*)spriteFrameName等方法。另外再擴展一個方法就是對於一些背景性質的精靈可能須要拉昇,再對以上兩個方法重載。則FCBaseSprite定義以下:
@interface FCBaseSprite : CCSprite {
    
}
+(id)spriteWithFile:(NSString *)filename stretch:(bool)isstretch;
+ (id)spriteWithSpriteFrameName:(NSString *)spriteFrameName stretch:(bool)isstretch;
@end
實現分別是:
/*
*stretch是否按照Ipad版進行變形
*/
+(id)spriteWithFile:(NSString *)filename stretch:(bool)isstretch {
    filename=[filename getDoubleImageFilename];
    CCNode* n= [super spriteWithFile:filename];//[[[self alloc] initWithFile:filename] autorelease];
    if (isstretch&&IsPad) {
        n.scaleX=1024.0/960;
        n.scaleY=768.0/640;
    }
    return n;
}
/*
*stretch是否按照Ipad版進行變形
*/
+ (id)spriteWithSpriteFrameName:(NSString *)spriteFrameName stretch:(bool)isstretch
{
    CCSpriteFrame *frame = [[FCBaseCCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:spriteFrameName];
    
    NSAssert1(frame!=nil, @"Invalid spriteFrameName: %@", spriteFrameName);
    
    CCNode *n=[super spriteWithSpriteFrame:frame];
    if (IsPad && isstretch) {
        n.scaleX=1024.0/960;
        n.scaleY=768.0/640;
    }
    return n;
}
getDoubleImageFilename方法是咱們對NSString類型的方法一個擴展,即爲了獲取真實的資源文件名
方法以下:
#define Retina2x @"@2x"
#define Retinahd @"-hd"

- (NSString*)getDoubleImageFilename {
    if (IsPad) {
        NSString* filenamenoExt= [self stringByDeletingPathExtension];
        NSString* extension = [self pathExtension];
        
        //NSString *extension = [path pathExtension];

        if (![filenamenoExt hasSuffix:Retina2x]) {
//            if ([self retainCount]>0) {
//                [self release];
//            }
            self = [NSString stringWithFormat:@"%@%@.%@",filenamenoExt,Retina2x,extension];
            //NSLOG(@"new plist=%@",*plist_p);
        }
    }
    return self;
}

以上是我開發過程當中的一部分,請各位大蝦批評指出。iphone