【问题标题】:How to draw a line with Cocos2d-iPhone如何用 Cocos2d-iPhone 画线
【发布时间】:2010-10-16 01:16:53
【问题描述】:

我试图通过完成简单的事情来掌握 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

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

我也试过了

-(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;
}

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

【问题讨论】:

    标签: iphone objective-c cocoa-touch cocos2d-iphone


    【解决方案1】:

    好的,我想出来给任何有兴趣的人。这是带有注释的代码,解释了要做什么。

    @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
    

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

    【讨论】:

    • 对我不起作用。画线(10,100,50,79);在 CCLayer 中未定义,不会链接应用程序有什么想法吗?谢谢。
    • 仅供参考 - 这个例子对于 Cocos2D 的 v1.0 已经过时了。 “drawLine”不再存在。使用具有相同参数的 CCDrawingPrimitives 中的“ccDrawLine”。
    • 添加到Shakakai的评论:如果你想绘制多条线段,也可以看看cocos2d中的ccDrawPoly
    【解决方案2】:

    来自cocos2ddrawPrimitivesTest.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) );
    
      // ...
    }
    

    【讨论】:

    【解决方案3】:

    您也可以使用 CCRibbon 类来绘制纹理与线条。这是一个例子:

    首先你创建带有 width 、 image 、 length 、 color 和 fade 参数的 CCRibbon

    ccColor4B myColor = ccc4(255, 255, 255, 150);
    
    CCRibbon *ribbon = [CCRibbon ribbonWithWidth:10 image:@"green.png" length:10.0 color:myColor fade:0.7f];
    

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

    [self addChild:ribbon z:8];
    

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

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

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

    [self removeChild:ribbon cleanup:YES];
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多