HardBirch

【IOS】仿捕鱼达人的金币滚动显示

时间:12-04-09 栏目:iOS游戏开发技术 作者:张飞不张,文采横飞 评论:4 点击: 2,584 次

 

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

//
//  UiNumRoll.h
//  WheelScore
//
//  Created by 周海锋 on 12-4-8.
//  Copyright 2012年 CJLU. All rights reserved.
//

#import <Foundation/Foundation.h>
#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 <Foundation/Foundation.h>
#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的下载地址:http://download.csdn.net/detail/toss156/4209940

声明: 本文由( 张飞不张,文采横飞 )原创编译,转载请保留链接: 【IOS】仿捕鱼达人的金币滚动显示

【IOS】仿捕鱼达人的金币滚动显示:目前有4 条留言

  1. 4楼
    dongge_111:

    顶顶顶…

    2012-04-09 10:55 [回复]
  2. 地板
    x1135768777:

    写的好好,羡慕啊

    2012-04-09 17:02 [回复]
  3. 板凳
    toss156:

    [reply]x1135768777[/reply]
    汗。

    2012-04-09 22:19 [回复]
  4. 沙发
    wcc0623090:

    效果真心好好啊

    2012-04-11 13:18 [回复]

发表评论


QQ群互动

Linux系统与内核学习群:194051772

WP建站技术学习交流群:194062106

魔豆之路QR

魔豆的Linux内核之路

魔豆的Linux内核之路

优秀工程师当看优秀书籍

优秀程序员,要看优秀书!

赞助商广告

友荐云推荐