【问题标题】:cocos2d ccDrawPoly crashcocos2d ccDrawPoly 崩溃
【发布时间】:2011-04-29 15:20:19
【问题描述】:

我正在尝试使用 ccDrawPoly 创建一些看起来像小行星的随机多边形。为此,我编写了一个简单的函数,它分段绘制一个圆(在每次迭代中随机化圆半径)以创建一种“凹凸不平”的多边形。顶点存储在 NSMutableArray 中,然后馈送到 ccDrawPolygon(代码如下):

float maxRadiusVariation = self.radius * 0.2; // 20% of radius
float cx = self.radius, cy = self.radius;
int minSegmentAngle = 5;
int maxSegmentAngle = 45;
int angle = 0;

while(angle < 360)
{
    float newRadius = self.radius/2 + rand()%(int)maxRadiusVariation;

    float x = cx + (cos(CC_DEGREES_TO_RADIANS(angle)) * newRadius);
    float y = cy + (sin(CC_DEGREES_TO_RADIANS(angle)) * newRadius);

    [vertices addObject:[NSArray arrayWithObjects:[NSNumber numberWithFloat:x], [NSNumber numberWithFloat:y], nil]];

    int angleIncrement = minSegmentAngle + rand()%maxSegmentAngle;
    angle += angleIncrement;
}

cocos2d 在调用绘图时内部崩溃。我相信我的问题可能在于我如何从 NSMutableArray 缓存中抓取多边形顶点并尝试绘制它们:

CGPoint cgVertices[[vertices count]];

    for(int i = 0; i < [vertices count]; i++)
    {
        NSArray *vertex = [vertices objectAtIndex:i];
        float x = [[vertex objectAtIndex:0] floatValue];
        float y = [[vertex objectAtIndex:1] floatValue];
        cgVertices[i] = ccp(x, y);
    }
    ccDrawPoly(cgVertices, [vertices count], YES);

关于更多信息,我在 cocos2d 崩溃之前包含了一个顶点数组内容的示例。为了使其更易于可视化,我添加了一个我创建的图形(其中蓝色像素代表圆心,白色像素是顶点,红线是连接它们的线)。

http://nial.me/output.png

(
        (
        93,
        60
    ),
        (
        "96.25231",
        "76.90473"
    ),
        (
        "91.17692",
        78
    ),
        (
        "83.78063",
        "81.41218"
    ),
        (
        "78.77886",
        "95.3179"
    ),
        (
        "68.77309",
        "98.00043"
    ),
        (
        44,
        "87.71281"
    ),
        (
        "34.08406",
        "87.79144"
    ),
        (
        "26.45318",
        "81.78556"
    ),
        (
        "22.51079",
        "70.74986"
    ),
        (
        "23.02254",
        "61.29128"
    ),
        (
        "27.64341",
        "44.21864"
    ),
        (
        "30.8436",
        "37.22053"
    ),
        (
        "52.57661",
        "27.84579"
    ),
        (
        "62.44148",
        "25.08526"
    ),
        (
        "73.42231",
        "29.853"
    ),
        (
        "84.13467",
        "37.49406"
    ),
        (
        "89.13047",
        "49.39737"
    )
)

【问题讨论】:

    标签: iphone cocos2d-iphone drawing


    【解决方案1】:

    你是在draw方法之外画的吗?如果你是,下面的解释可能会有所帮助。

    您不能在 CCNode 绘制函数之外使用 ccDrawPoly。 请注意它在大写中声明“不要在此方法之外绘制你的东西”。下面是CCNode的draw方法的定义。

    // This is CCNode's draw method

    -(void) draw
    {
            // override me
            // Only use this function to draw your stuff.
            // DON'T draw your stuff outside this method
    }
    

    cocos2d-ios中ccDrawPoly的例子:ActionsTest.m

    -(void) draw
    {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
    
        float x = winSize.width*2 - 100;
        float y = winSize.height;
    
        CGPoint vertices[] = { ccp(5,5), ccp(x-5,5), ccp(x-5,y-5), ccp(5,y-5) };
        ccDrawPoly(vertices, 4, YES);
    }
    

    来源:Draw and Update Reference

    【讨论】:

      猜你喜欢
      • 2011-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多