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

6 篇文章 0 订阅
6 篇文章 0 订阅

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

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


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

#import <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>
#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<m_maxCol; ++i)
    {
        UINumber* pNumber = [UINumber numberWithSprite:style];
        [numArray addObject:pNumber];
        [pNumber setNumber:0];
        [pNumber setPosition:CGPointMake(m_point.x - i*(NUM_WIDTH+NUM_SPACE), m_point.y)];
        [self addChild:pNumber];
    }
    
}


/*
 * getNumber 获取显示的数字
 */
-(int) getNumber
{
    return m_nNumber;
}

/*
 * setNumber 设置显示的数字
 * num int 设置的数字
 */
-(void) setNumber:(int)num
{
    if(num<0)
        num=0;
    
    if (m_nNumber != num)
    {
        m_nNumber=num;
        [self rebuildEffect];
    }
}

/*
 * rebuildEffect 重新设置每位数字
 */
-(void) rebuildEffect
{    
    int i=0;
    int num = m_nNumber;
    while (1) {
        if (num<=0) {
            if(m_maxCol<=i && zeroFill)
            break;
        }
        int showNum = num%10;
        
        UINumber* pNumber = (UINumber *)[numArray objectAtIndex:i];
        [pNumber setNumber:showNum];
        
        i++;
        num = num/10;
    }
}


-(void)setPosition:(CGPoint)position
{
    position.x+=width*0.5f;//让位置居中
    [super setPosition:position];
}

-(void)dealloc
{
    NSLog(@"~UIRollNum");
    [self removeAllChildrenWithCleanup:YES];
    [numArray release];
    [super dealloc];
}

@end


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Cocos Creator 中可以使用动作(Action)来创建数字滚动效果。下面是一个简单的示例代码: 1. 创建一个空节点,将它命名为“NumberRoller”。 2. 在“NumberRoller”节点下创建三个 Label 节点,分别命名为“Num1”、“Num2”、“Num3”。 3. 在“NumberRoller”节点上添加一个脚本组件,将以下代码添加到脚本中: ```javascript cc.Class({ extends: cc.Component, properties: { startValue: 0, endValue: 999, duration: 2, interval: 0.05, num1: cc.Label, num2: cc.Label, num3: cc.Label }, start () { this.num1.string = this.startValue; this.num2.string = this.startValue; this.num3.string = this.startValue; this.schedule(this.updateValue, this.interval); }, updateValue () { let value1 = Math.floor(Math.random() * 10); let value2 = Math.floor(Math.random() * 10); let value3 = Math.floor(Math.random() * 10); let valueSum = value1 * 100 + value2 * 10 + value3; let currentValue = parseInt(this.num3.string) + valueSum; if (currentValue > this.endValue) { currentValue = this.endValue; this.unschedule(this.updateValue); } this.num1.string = this.num2.string; this.num2.string = this.num3.string; this.num3.string = currentValue; } }); ``` 4. 在“NumberRoller”节点上配置脚本组件的属性,包括起始值(startValue)、结束值(endValue)、动画时间(duration)和数字变化的时间间隔(interval)。 5. 运行游戏,可以看到数字滚动效果。 这只是一个简单的数字滚动效果示例,你可以根据你的实际需求进行修改和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值