iOS cocos2d 实现RollNumber(数字滚动效果)控件效果源码

开发人员:Jason's.Alex   QQ:531401335

csdn博客:http://blog.csdn.net/RuShrooM


//
//  UiNumRoll.h
//  WheelScore
//
//  自定义数字精灵
//
//开发人员:Jason's.Alex
//QQ:531401335

#import 
#import "cocos2d.h"
#import "ResourceLoad.h"

#define NUM_HEIGHT 20
#define NUM_WIDTH  12

typedef enum{
    NumStyleNormal,
    NumStyleSameTime,
}NumStyle;


@interface UINumber : CCSprite {
    NumStyle m_style;       //滚动样式
    int m_number;//当前的数字
    int m_nPosCur;          //当前的位置
    int m_nPosEnd;          //结束的位置
    int m_nMoveLen;         //每次移动的位置
    CCTexture2D *m_texture; //数字的texture
    float moveFactor;//移动因子,当使用retina技术,每个坐标点占用2个像素
}

@property(nonatomic,retain) CCTexture2D *m_texture;

+(id)numberWithSprite:(NumStyle) style;

-(id)initWithStyle:(NumStyle) style ;

-(void) setNumber:(int) num;
-(void) onRollDown:(ccTime) dt;
-(void) onRollUP:(ccTime) dt;
-(void) setup;
@end

//
//  UiNumRoll.m
//  WheelScore
//
//开发人员:Jason's.Alex
//QQ:531401335


#import "UINumber.h"

@implementation UINumber
@synthesize m_texture;

+(id)numberWithSprite:(NumStyle) style
{
    return [[[self alloc]initWithStyle:style]autorelease];
}

/*
 * init 初始化
 */
-(id) init
{
	if( (self=[super init])) {
        m_texture = NULL;
        m_style = NumStyleNormal;
        m_nPosCur = 0;
        m_nPosEnd = 0;
        m_number=0;
        moveFactor=1.0f;
        [self setup];
    }
	return self;
}

/*
 * initWithStyle 初始化
 */
-(id) initWithStyle:(NumStyle) style
{
    if( (self=[super init])) 
    {
        m_texture = NULL;
        m_style = style;
        m_nPosCur = 0;
        m_nPosEnd = 0;
        m_number=0;
        moveFactor=1.0f;
        [self setup];
    }
    return self;
}

/*
 * setup 设置texture
 */
-(void)setup
{
    ResourceLoad* rl=[ResourceLoad shardResourceLoad];
    
    if(rl.isRetina)//如果 启动高清那么所有截取图像缩小一倍,
    {
        moveFactor=0.5f;
        [self setScaleX:rl.retinaScaleX*2.0f];
        [self setScaleY:rl.retinaScaleY*2.0f];
    }
    
    UIImage *image = [UIImage imageNamed:@"number.png"];
    m_texture = [[CCTexture2D alloc]initWithImage:image];
    
    CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:m_texture rect:CGRectMake(0, 0, NUM_WIDTH, NUM_HEIGHT*moveFactor)];
    
    [self setDisplayFrame:frame];
}

/*
 * setNumber 设置显示的数字
 */
-(void) setNumber:(int) num
{
    [self unscheduleAllSelectors];
    m_nPosCur =NUM_HEIGHT*m_number*moveFactor;
    m_nPosEnd = NUM_HEIGHT * num*moveFactor;
    
    if (NumStyleNormal == m_style) {
        m_nMoveLen = 4*moveFactor;
    }
    else if (NumStyleSameTime == m_style) {
        m_nMoveLen = (m_nPosEnd-m_nPosCur)/20*moveFactor;
    }
    
    if (m_number>num) {
        [self schedule:@selector(onRollUP:) interval:0.01];
    }
    else {
        [self schedule:@selector(onRollDown:) interval:0.01];
    }
    
    m_number=num;
}

/*
 * onRollDown 向下滚动
 */
-(void) onRollDown:(ccTime) dt
{
    m_nPosCur += m_nMoveLen;
    if (m_nPosCur >= m_nPosEnd) {
        m_nPosCur = m_nPosEnd;
        [self unschedule:@selector(onRollDown:)];
    }
    
    CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:m_texture rect:CGRectMake(0, m_nPosCur, NUM_WIDTH, NUM_HEIGHT*moveFactor)];
    [self setDisplayFrame:frame];
}


/*
 * onRollUP 向上滚动
 */
-(void) onRollUP:(ccTime) dt
{
    m_nPosCur -= m_nMoveLen;
    if (m_nPosCur <= m_nPosEnd) {
        m_nPosCur = m_nPosEnd;
        [self unschedule:@selector(onRollUP:)];
    }
    
    CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:m_texture rect:CGRectMake(0, m_nPosCur, NUM_WIDTH, moveFactor*NUM_HEIGHT)];
    [self setDisplayFrame:frame];
}

-(void)dealloc
{
    NSLog(@"~UINumber");
    [self unscheduleAllSelectors];
    [m_texture release];
    [super dealloc];
}
@end

//
//  UIRollNum.h
//  WheelScore
//
//数字滚动
//开发人员:Jason's.Alex
//QQ:531401335

#import 
#import "cocos2d.h"
#import "UINumber.h"

#define NUM_SPACE 0 //数字之间的间隔

@interface UIRollNum : CCLayer {
    int m_nNumber;              //显示的数字
    int m_maxCol;               //最大显示位数
    NSMutableArray *numArray;   //存放每个数字的数组
    CGPoint m_point;            //坐标
    bool  zeroFill;             //是否开启0填充
    NumStyle style;             //滚动样式
    
    float width;
    float height;//高和 宽
}

@property (nonatomic,retain) NSMutableArray *numArray;
@property (nonatomic) CGPoint m_point;
@property (nonatomic) NumStyle style;  

@property(readonly) float width;
@property(readonly) float height;

+(id)numberWithRollNum;

-(void)initWithNumberSprite;//初始化数字精灵

-(void) rebuildEffect;
-(int) getNumber;
-(void) setNumber:(int)num;
@end

//
//  UIRollNum.m
//  WheelScore
//
//开发人员:Jason's.Alex
//QQ:531401335

#import "UIRollNum.h"
@implementation UIRollNum

@synthesize numArray,m_point,style;
@synthesize width;
@synthesize height;

+(id)numberWithRollNum
{
    return [[[self alloc]init]autorelease];
}

/*
 * init 初始化
 */
-(id) init
{
    if (self = [super init]) {
        m_nNumber = -1;
        m_maxCol = 6;
        numArray =[[NSMutableArray alloc] init];
        zeroFill = YES;
        style = NumStyleNormal;
        width=(NUM_WIDTH+NUM_SPACE)*m_maxCol;
        height=NUM_HEIGHT;
        
        [self initWithNumberSprite];
    }   
    return self;
}

-(void)initWithNumberSprite//初始化数字精灵
{    
    
    for (int i=0; i


你可能感兴趣的:(iOS,游戏,cocos2d,object-c)