Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Cocos2d iphone 如何在cocos2d中使用openGL线描_Cocos2d Iphone_Line_Draw - Fatal编程技术网

Cocos2d iphone 如何在cocos2d中使用openGL线描

Cocos2d iphone 如何在cocos2d中使用openGL线描,cocos2d-iphone,line,draw,Cocos2d Iphone,Line,Draw,我喜欢画一条线,但是cocos2d里面的ccDrawLine锯齿,怎么画一条模糊的线,谁能帮我 ccDrawLine( ccp(StartP.x, StartP.y), ccp(EndP.x, EndP.y) ); 我没有使用ccDrawLine,但我用精灵创建了一条线,并设置了动画(见下面的代码)。通过这种方式,我可以使用定制的线条图像(但无论如何,我就是这样做的) 如果你想坚持使用基本体,我想你应该设置线条基本体的不透明度(参见解释如何设置的帖子),然后你可以创建一系列动作来设置不透明度级

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

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

我没有使用ccDrawLine,但我用精灵创建了一条线,并设置了动画(见下面的代码)。通过这种方式,我可以使用定制的线条图像(但无论如何,我就是这样做的)

如果你想坚持使用基本体,我想你应该设置线条基本体的不透明度(参见解释如何设置的帖子),然后你可以创建一系列动作来设置不透明度级别(例如,开始时不透明度为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];

希望这有帮助

我没有使用ccDrawLine,但我用精灵创建了一条线,并设置了动画(请参见下面的代码)。通过这种方式,我可以使用定制的线条图像(但无论如何,我就是这样做的)

#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
如果你想坚持使用基本体,我想你应该设置线条基本体的不透明度(参见解释如何设置的帖子),然后你可以创建一系列动作来设置不透明度级别(例如,开始时不透明度为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];
希望这有帮助

#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
要动态更新或扩展行,只需使用

[streak setLinePosition:ccp(x,y)];
希望这能给你更多的使用灵活性

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

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

希望这将为您提供更大的使用灵活性

非常感谢,此方法已在上使用。非常感谢,此方法已在上使用。用法:CClineSprite*streak=[CClineSprite node];[self addChild:streak z:999];[条纹设置线条位置:关联TurtleObject.position];[条纹设置线标度:0.4];要动态更新或扩展线,只需使用[streak setLinePosition:ccp(x,y)];希望这将为您的使用提供更大的灵活性用法:CClineSprite*streak=[CClineSprite节点];[self addChild:streak z:999];[条纹设置线条位置:关联TurtleObject.position];[条纹设置线标度:0.4];要动态更新或扩展线,只需使用[streak setLinePosition:ccp(x,y)];希望这能给你更多的使用灵活性