iOS cocos2d 롤 넘 버 (디지털 스크롤 효과) 컨트롤 효과 원본 구현

개발 자: 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

좋은 웹페이지 즐겨찾기