cocos2d动画用例

来源:网络 责任编辑:栏目编辑 发表时间:2013-07-01 10:31 点击:

// Animation.h
#import "Box2D.h"
#import "cocos2d.h"
#import "BYSingle.h"
@interface AnimSpriteFactory : NSObject {
    BYSingle *_single;
    BOOL _is4g;
    BOOL _isIpad;
}

- (id) init;
- (CCSprite*) genAnimSprite:(CGPoint)position
                   animName:(NSString*)animName
                 startIndex:(int)startIndex
                   endIndex:(int)endIndex
              repeatForever:(BOOL)repeatForever
                      delay:(float)delay;

- (void) dealloc;
@end

 

//  Animation.mm
#import "AnimSpriteFactory.h"
@implementation AnimSpriteFactory

- (id) init {
    if((self = [super init])) {
        _single = [BYSingle getInstance];
_is4g = _single.isRetinaSupported;
_isIpad = _single.isIpad;
    }
    return self;
}

/**
 * 用于获取 sprite 的宽度和高度,太他妈蛋疼了~
 * 解析 plist 文件
 * 弊端:会使动画第一帧偏低于正常情况(当初不知道是出于什么意图要计算出动画的 size)~
 */
-(CGSize) getAnimSpriteSize:(NSString*)animName start:(int)startIndex {
NSString *plistPath = [[NSBundle mainBundle] pathForResource:animName ofType:@"plist"];
NSDictionary *dictionary, *framesDic;
//
dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
   
dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];
framesDic = [dictionary objectForKey:@"frames"];
NSMutableDictionary *startFrameDic = [framesDic objectForKey:[NSString stringWithFormat:@"%@%d.png", animName, startIndex]];
NSString *rectDataString = [startFrameDic objectForKey:@"frame"];
NSRange range = NSMakeRange(1, [rectDataString length]-2);
NSString *withoutBorder = [rectDataString substringWithRange:range];
NSArray *array = [withoutBorder componentsSeparatedByString:@", "];
NSString *sizeString = [array objectAtIndex:1];
NSRange range2 = NSMakeRange(1, [sizeString length]-2);
NSString *sizeStringWithoutBorder = [sizeString substringWithRange:range2];
NSArray *size = [sizeStringWithoutBorder componentsSeparatedByString:@","];
return CGSizeMake([[size objectAtIndex:0] intValue], [[size objectAtIndex:1] intValue]);
}

// delay 默认值为 0。05~
- (CCSprite*) genAnimSprite:(CGPoint)position
                   animName:(NSString*)animName
                 startIndex:(int)startIndex
                   endIndex:(int)endIndex
              repeatForever:(BOOL)repeatForever
                      delay:(float)delay
{
    CCSpriteFrameCache *frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];
   
    // 1.缓冲sprite帧和纹理
    NSString *spriteFrameCacheName = [NSString stringWithFormat:@"%@.

    相关新闻>>

      发表评论
      请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
      用户名: 验证码:点击我更换图片
      最新评论 更多>>

      推荐热点

      • Lexical or Preprocessor Issue 'xxx.h
      • ios学习笔记(二)xcode 4.3.2下实现基本交互
      • ios版本的helloworld
      • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
      • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
      • IOS类似iphone通讯录TableView的完整demo【附源码】
      • UITableView一些方法
      • [iPhone中级]iPhone团购信息客户端的开发 (二)
      • iphone(object-c)内存管理(1)
      网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
      Copyright © 2008-2015 计算机技术学习交流网. 版权所有

      豫ICP备11007008号-1