IOS Cocos2d Gen CCSpriteFrame from Cache

/* Get Frame from Cache  */
+ (CCSpriteFrame*) getFrameFromImage:(NSString*) imagefile x:(int) x y:(int) y w:(int) w h:(int)h {
   
    CCSpriteFrame *frame ;
    CCSpriteBatchNode *spriteSheet;
    BOOL check = [self checkFileExistInResource:imagefile];
   
    if(check) {
        spriteSheet = [CCSpriteBatchNode batchNodeWithFile:imagefile];          
    } else {
        spriteSheet = [CCSpriteBatchNode batchNodeWithTexture:[self getTextureFromImage:imagefile]];       
    }
   
    frame = [CCSpriteFrame frameWithTexture:spriteSheet.texture rect:CGRectMake(x*w,y*h,w,h)];
   
    return frame;
}

/* Get Frame from Cache  */
+ (CCSpriteFrame*) getFrameFromImage:(NSString*) imagefile {
   
    CCSpriteFrame *frame ;
    CCSpriteBatchNode *spriteSheet;
    BOOL check = [self checkFileExistInResource:imagefile];
    CGSize size;
    if(check) {
        spriteSheet = [CCSpriteBatchNode batchNodeWithFile:imagefile];
    } else {
        spriteSheet = [CCSpriteBatchNode batchNodeWithTexture:[self getTextureFromImage:imagefile]];
    }
    size = [Global getImageSize:imagefile];
    frame = [CCSpriteFrame frameWithTexture:spriteSheet.texture rect:CGRectMake(0,0,size.width,size.height)];
   
    return frame;
}

/* Check file exist in Resource folder  */
+ (BOOL) checkFileExistInResource:(NSString*) filename {
   
    NSString *basename = [[[filename lastPathComponent] componentsSeparatedByString: @"."] objectAtIndex: 0];   
    NSString *pathAndFileName = [[NSBundle mainBundle] pathForResource:basename ofType:@"png"];
    BOOL check = [[NSFileManager defaultManager] fileExistsAtPath:pathAndFileName];
   
    return check;
   
}

Comments

Popular posts from this blog

Java : Variables Declaring

Install DNF in RHEL/CentOS 7

SQL Self JOIN