Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1071327
  • 博文数量: 403
  • 博客积分: 10272
  • 博客等级: 上将
  • 技术积分: 4407
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:22
文章分类

全部博文(403)

文章存档

2012年(403)

分类: 嵌入式

2012-04-14 19:59:37

今天给大家带来一个模仿捕鱼达人中,金币的滚动显示。(部分代码参考了cocoachina上一个C++版的)

// // UiNumRoll.h // WheelScore // // Created by 周海锋 on 12-4-8. // Copyright 2012年 CJLU. All rights reserved. // #import #import "cocos2d.h" #define NUM_HEIGHT 20 #define NUM_WIDTH 20 typedef enum{ NumStyleNormal, NumStyleSameTime, }NumStyle; @interface UINumber : CCSprite { NumStyle m_style; //滚动样式 int m_num; //显示的数字 int m_nPosCur; //当前的位置 int m_nPosEnd; //结束的位置 int m_nMoveLen; //每次移动的位置 CCTexture2D *m_texture; //数字的texture } @property(nonatomic,retain) CCTexture2D *m_texture; -(id) initWithStyle:(NumStyle) style; -(void) setNumber:(int) num; -(void) onRollDown:(ccTime) dt; -(void) onRollUP:(ccTime) dt; -(void) setup; @end
// // UiNumRoll.m // WheelScore // // Created by 周海锋 on 12-4-8. // Copyright 2012年 CJLU. All rights reserved. // #import "UINumber.h" @implementation UINumber @synthesize m_texture; /* * init 初始化 */ -(id) init { if( (self=[super init])) { m_texture = NULL; m_style = NumStyleNormal; m_num = 0; m_nPosCur = 0; m_nPosEnd = 0; [self setup]; } return self; } /* * initWithStyle 初始化 */ -(id) initWithStyle:(NumStyle) style { if( (self=[super init])) { m_texture = NULL; m_style = style; m_num = 0; m_nPosCur = 0; m_nPosEnd = 0; [self setup]; } return self; } /* * setup 设置texture */ -(void)setup { 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)]; [self setDisplayFrame:frame]; } /* * setNumber 设置显示的数字 */ -(void) setNumber:(int) num { m_nPosCur = NUM_HEIGHT * m_num; m_nPosEnd = NUM_HEIGHT * num; if (NumStyleNormal == m_style) { m_nMoveLen = 4; } else if (NumStyleSameTime == m_style) { m_nMoveLen = (m_nPosEnd-m_nPosCur)/20; } if (m_num > num) { [self schedule:@selector(onRollUP:) interval:0.03]; } else { [self schedule:@selector(onRollDown:) interval:0.03]; } m_num = 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)]; [self setDisplayFrame:frame]; } /* * onRollUP 向上滚动 */ -(void) onRollUP:(ccTime) dt { m_nPosCur -= 4; 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, NUM_HEIGHT)]; [self setDisplayFrame:frame]; } -(void)dealloc { [self unscheduleAllSelectors]; [m_texture release]; [super dealloc]; } @end
// // UIRollNum.h // WheelScore // // Created by 周海锋 on 12-4-8. // Copyright 2012年 CJLU. All rights reserved. // #import #import "cocos2d.h" #import "UINumber.h" @interface UIRollNum : CCSprite { int m_nNumber; //显示的数字 int m_maxCol; //最大显示位数 NSMutableArray *numArray; //存放每个数字的数组 CGPoint m_point; //坐标 bool zeroFill; //是否开启0填充 NumStyle style; //滚动样式 } @property (nonatomic,retain) NSMutableArray *numArray; @property (nonatomic) CGPoint m_point; @property (nonatomic) NumStyle style; -(void) rebuildEffect; -(void) clearEffect; -(int) getNumber; -(void) setNumber:(int)num; @end
// // UIRollNum.m // WheelScore // // Created by 周海锋 on 12-4-8. // Copyright 2012年 CJLU. All rights reserved. // #import "UIRollNum.h" @implementation UIRollNum @synthesize numArray,m_point,style; /* * init 初始化 */ -(id) init { if (self = [super init]) { m_nNumber = 0; m_maxCol = 6; numArray =[[NSMutableArray alloc] init]; zeroFill = YES; style = NumStyleNormal; } return self; } /* * getNumber 获取显示的数字 */ -(int) getNumber { return m_nNumber; } /* * setNumber 设置显示的数字 * num int 设置的数字 */ -(void) setNumber:(int)num { if (m_nNumber != num) { m_nNumber = num; [self rebuildEffect]; } } /* * rebuildEffect 重新设置每位数字 */ -(void) rebuildEffect { [self clearEffect]; 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 alloc]initWithStyle:style]; [numArray addObject:pNumber]; [pNumber setNumber:showNum]; [pNumber setPosition:CGPointMake(m_point.x - i*NUM_WIDTH, m_point.y)]; [pNumber setAnchorPoint:CGPointMake(1, 0.5)]; [self addChild:pNumber z:100]; i++; num = num/10; } } /* * rebuildEffect 清楚每位数字 */ -(void) clearEffect { for(int i=0;i<[numArray count];i++) { UINumber* pNumber = (UINumber *)[numArray objectAtIndex:i]; [self removeChild:pNumber cleanup:YES]; } [numArray removeAllObjects]; } -(void)dealloc { [numArray release]; [super dealloc]; } @end

demo的下载地址:

阅读(1307) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~