cocos2d分数滚动效果!

本文介绍了一种使用Cocos2d实现的分数滚动显示效果。通过自定义ScoreBoard类,可以设置字体、颜色、阴影等参数,并平滑地更新显示的分数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

cocos2d,做了一个分数滚动的效果,cocos2d分数滚动效果!


#import <UIKit/UIKit.h>
#import "cocos2d.h"
//#import "AWTextureFilter.h"

@interface ScoreBoard :CCSprite
{
	CCLabelTTF *label_;
	CCSprite *shadowLa_;
	long long number_;
    NSString* format_;
    long target_;
    ccTime interval_;
    NSString* fontName_;
    CGFloat fontSize_;
}

-(id)initWithScore:(NSString*)format Number:(long long)number shadowColor:(ccColor3B)shadowColor shadowPosition:(CGPoint)shadowposition Interval:(ccTime)interval FontName:(NSString*)fontName FontSize:(CGFloat)fontSize;
-(id)initWithScore:(NSString*)format Number:(long long)number;
-(void)setScore:(long long)score;

@end

#import "ScoreBoard.h"

@implementation ScoreBoard
- (void) setText:(NSString*)text
{	
  //  NSLog(@"TODO");
	//Create mutable texture
    //	CCTexture2DMutable *shadowTexture = [[[CCTexture2DMutable alloc] initWithString:text fontName:fontName_ fontSize:fontSize_] autorelease];
    //	
    //	//Copy the mutable texture as non mutable texture
    //	CCTexture2D *labelTexture = [[shadowTexture copyMutable:NO] autorelease];
    //	
    //	[label_ setTexture:labelTexture];
    //	[label_ setTextureRect:CGRectMake(0, 0, shadowTexture.contentSize.width, shadowTexture.contentSize.height)];
    //	
    //	//Apply blur to the mutable texture
    ////	[AWTextureFilter blur:shadowTexture radius:4];
    //	
    //	[shadowLa_ setTexture:shadowTexture];
    //	[shadowLa_ setTextureRect:CGRectMake(0, 0, shadowTexture.contentSize.width, shadowTexture.contentSize.height)];
    [label_ setString:text];
}

-(id)initWithScore:(NSString*)format Number:(long long)number shadowColor:(ccColor3B)shadowColor shadowPosition:(CGPoint)shadowposition Interval:(ccTime)interval FontName:(NSString*)fontName FontSize:(CGFloat)fontSize
{
    self=[super init];
    if (self!=nil) {
        label_ = [CCSprite node];
        [label_ setPosition:ccp(0, 0)];
    	shadowLa_ = [CCSprite node];
    	[shadowLa_ setPosition:shadowposition];
    	[shadowLa_ setColor:shadowColor];
        [shadowLa_ setOpacity:128];
	

        number_ = number;
        format_=format;
        interval_=interval;
        fontName_=fontName;
        fontSize_=fontSize;
        label_=[[CCLabelTTF alloc]initWithString:[NSString stringWithFormat:format_, number_] fontName:fontName fontSize:fontSize];
//        [self setText:[NSString stringWithFormat:format_, number_]];
        
        [self addChild:shadowLa_ z:0];
        [self addChild:label_ z:1];
    }
        return self;
}
-(id)initWithScore:(NSString *)format Number:(long long)number
{
    return [self initWithScore:format Number:number shadowColor:ccBLACK shadowPosition:ccp(0, -2) Interval:0.000001f FontName:@"Eurostile LT ExtendedTwo" FontSize:16];
}

- (void)updateLabel:(ccTime)delta
{
    if (target_==number_) {
        [self unschedule:@selector(updateLabel:)];
        return;
    }
    
    NSString *str = [NSString stringWithFormat:@"%ld",labs(target_ - number_)];
    int len = str.length;
    if (target_>number_)
    {
        number_ += pow(10, len-1);
    }
    else
    {
        number_ -= pow(10, len-1);
    }
        
    [self setText:[NSString stringWithFormat:format_, number_]];
}

-(void)setScore:(long long)score
{
    if (score==number_) {
        return;
    }
    target_=score;
    
    [self schedule:@selector(updateLabel:) interval:interval_];
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值