objective-c 如何用 Cocos2d-iPhone 画一条线

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/691648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:18:30  来源:igfitidea点击:

How to draw a line with Cocos2d-iPhone

iphoneobjective-ccocoa-touchcocos2d-iphone

提问by gargantuan

I'm trying to get to grips with Cocos2d by trying to accomplish simple things. At this point, I have a scene, that scene has a background sprite, and a Layer. I'm trying to draw onto the Layer using drawLine. Here's my current attempt.

我试图通过尝试完成简单的事情来掌握 Cocos2d。此时,我有一个场景,该场景有一个背景精灵和一个图层。我正在尝试使用 drawLine 在图层上绘制。这是我目前的尝试。

@implementation MyLayer
-(id)init{
    self = [super init];
    if(self != nil){
        glColor4f(0.8, 1.0, 0.76, 1.0);  
        glLineWidth(2.0f);
        CocosNode *line = drawLine(10.0f, 100.0f,400.0f,27.0f);
        [self addChild:line z:1];
    }
    return self;
}
@end

Which generates the error "void value not ignored as it ought to be". So obviously I'm doing it wrong, but hopefully you can see my reasoning.

这会产生错误“无效值没有被忽略,因为它应该是”。所以很明显我做错了,但希望你能看到我的推理。

I've also tried this

我也试过这个

-(id)init{
    self = [super init];
    if(self != nil){
        glColor4f(0.8, 1.0, 0.76, 1.0);  
        glLineWidth(2.0f);
        drawLine(10.0f, 100.0f,400.0f,27.0f);
    }
    return self;
}

Which doesn't give me an error, but it doesn't work either. I realise I'm not understanding something fundamental, but can anyone steer me in the right direction?

这不会给我一个错误,但它也不起作用。我意识到我没有理解一些基本的东西,但是有人能引导我走向正确的方向吗?

回答by Travis

From the cocos2d drawPrimitivesTest.m:

来自 cocos2d drawPrimitivesTest.m

- (void)draw {
  // ...

  // draw a simple line
  // The default state is:
  // Line Width: 1
  // color: 255,255,255,255 (white, non-transparent)
  // Anti-Aliased
  glEnable(GL_LINE_SMOOTH);
  ccDrawLine( ccp(0, 0), ccp(s.width, s.height) );

  // ...
}

回答by gargantuan

Ok, I figured it out for anyone who is interested. Here's the code with the comment explaining what to do.

好的,我为任何有兴趣的人找到了它。这是带有注释的代码,解释了要做什么。

@implementation GameLayer
-(id)init{
    self = [super init];
    if(self != nil){
        // init stuff here      
    }
    return self;
}

// You have to over-ride this method
-(void)draw{
    glColor4f(0.8, 1.0, 0.76, 1.0);  
    glLineWidth(2.0f);
    drawLine(10,100,50,79);
}    
@end

So I assume, the draw method gets called at every frame.

所以我假设,draw 方法在每一帧都会被调用。

回答by huyleit

You can also use CCRibbon class to draw line with your texture. Here is an example:

您还可以使用 CCRibbon 类用您的纹理绘制线条。下面是一个例子:

First you create CCRibbon with width , image , length , color and fade parameters

首先,您使用宽度、图像、长度、颜色和淡入淡出参数创建 CCRibbon

ccColor4B myColor = ccc4(255, 255, 255, 150);

CCRibbon *ribbon = [CCRibbon ribbonWithWidth:10 image:@"green.png" length:10.0 color:myColor fade:0.7f];

Then we add it as a child:

然后我们将其添加为子项:

[self addChild:ribbon z:8];

If you run the application now you will not see anything because you didn't add any points yet to the CCRibbon so lets add 2 points

如果您现在运行该应用程序,您将看不到任何内容,因为您尚未向 CCRibbon 添加任何点,所以让我们添加 2 个点

[ribbon addPointAt:ccp(10,10) width:10];

[ribbon addPointAt:ccp(15,15) width:10];

You cannot remove individual points but you can remove the CCRibbon from its parent

您不能删除单个点,但可以从其父级中删除 CCRibbon

[self removeChild:ribbon cleanup:YES];

Source code from: http://www.ccsprite.com/cocos2d/using-ccribbon-example.html

源代码来自:http: //www.ccsprite.com/cocos2d/using-ccribbon-example.html