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