我試圖用ccDrawPoly來創建一些隨機看起來像小行星的多邊形。要做到這一點,我寫了一個簡單的函數,它在段中繪製一個圓圈(隨機化每次迭代的圓弧半徑)以創建一種「顛簸」的多邊形。頂點被存儲在一個NSMutableArray,然後提供給ccDrawPolygon(代碼如下):cocos2d ccDrawPoly崩潰
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崩潰之前的內容。爲了使它更容易可視化,我添加了一個我創建的圖形(藍色像素代表圓心,白色像素是頂點,紅色線是連接它們的直線)。
(
(
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"
)
)