1

我喜欢画线,但是cocos2d里面的ccDrawLine有锯齿,怎么画出模糊的线,谁能帮帮我?

ccDrawLine( ccp(StartP.x, StartP.y), ccp(EndP.x, EndP.y) );
4

2 回答 2

1

我没有使用 ccDrawLine,但我用 Sprite 创建了一条线,并为它设置了动画(参见下面的代码)。通过这种方式,我能够使用制作的自定义线条图像(但没关系。我就是这样做的)。

如果你想坚持原语我想你应该设置线原语的不透明度(参见这篇解释如何的帖子)然后你可以创建一个设置不透明度级别的动作序列(例如从 100% 的不透明度开始,然后75%,然后像我对图像所做的那样回到 100%,但使用上面链接中的方法)以获得模糊效果。

使用图像的代码:

CCSprite * string = [self getChildByTag:tag];
[string setOpacity:100]; 
NSMutableArray* frames = [[NSMutableArray alloc]initWithCapacity:3];
NSString*lineFrame = [NSString stringWithString:@"line0.png"];            

CCSpriteFrame* frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:lineFrame];
[frames addObject:frame];

lineFrame = [NSString stringWithString:@"line1.png"];
frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:lineFrame];
[frames addObject:frame];

lineFrame = [NSString stringWithString:@"line0.png"];
frame = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:lineFrame];
[frames addObject:frame];


CCAnimation* anim = [CCAnimation animationWithFrames:frames delay:0.1f];
CCAnimate* animate = [CCAnimate actionWithAnimation:anim];
//CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
[string runAction:animate];

[string setOpacity:75];

希望这会有所帮助..

于 2012-06-19T15:32:37.050 回答
0
#import "cocos2d.h"

@interface CClineSprite : CCLayer
{
    CCRenderTexture * renderTarget;
    NSMutableArray  * pathArray;
    CCSprite        * pathBrush;
    CGPoint           prePosition;
}

-(void)setLinePosition:(CGPoint)position;

-(void)setLineOpacity:(GLubyte) anOpacity;

-(void)setLineScale:(float) scale;

-(void)setLineColor:(ccColor3B) color;

@end








#import "CClineSprite.h"

@implementation CClineSprite

- (id) init
{
    self = [super init];

    if (self)
    {
        CGSize s = [[CCDirector sharedDirector] winSize];
        renderTarget = [CCRenderTexture renderTextureWithWidth:s.width height:s.height];
        [renderTarget setPosition:ccp(s.width/2, s.height/2)];
        [self addChild:renderTarget z:1];
        pathBrush = [CCSprite spriteWithFile:@"dots.png"];
        pathBrush.color = ccWHITE;
        [pathBrush setOpacity:100];
        [pathBrush setScale:0.5];
        pathArray = [[NSMutableArray alloc]init];
    }
    return self;
}

-(void)setLineOpacity:(GLubyte) anOpacity
{
    [pathBrush setOpacity:anOpacity];
}

-(void)setLineScale:(float) scale
{
    [pathBrush setScale:scale];
}

-(void)setLineColor:(ccColor3B) color
{
    [pathBrush setColor:color];
}

-(void)setLinePosition:(CGPoint)position
{
    [pathArray addObject:[NSValue valueWithCGPoint:position]];
    [self renderPath];
}

- (void) renderPath
{
    [renderTarget clear:0 g:0 b:0 a:0];
    [renderTarget begin];

    for (int i = 0; i < pathArray.count-1;i++)
    {
        CGPoint pt1;
        CGPoint pt2;

        [[pathArray objectAtIndex:i] getValue: &pt1];
        [[pathArray objectAtIndex:i + 1] getValue: &pt2];

        float distance = ccpDistance(pt1, pt2);

        if (distance > 1)
        {
            int d = (int)distance;
            for (int i = 0; i < d; i += 10)
            {
                float difx = pt2.x - pt1.x;
                float dify = pt2.y - pt1.y;
                float delta = (float)i / distance;

                [pathBrush setPosition:ccp(pt1.x + (difx * delta), pt1.y + (dify * delta))];
                [pathBrush visit];
            }
        }
    }
    [renderTarget end];
}

@end

用法:

CClineSprite* streak = [CClineSprite node];
[self addChild:streak z:999];
[streak setLinePosition:associatedTurtleObject.position];
[streak setLineScale:0.4];

要动态更新或扩展行,只需使用

[streak setLinePosition:ccp(x,y)];

希望这会给您更多的使用灵活性

于 2013-05-07T08:01:34.113 回答