iphone - cocos2d ccDrawPoly crash -


i'm trying use ccdrawpoly create random looking asteroid-like polygons. this, i've written simple function draws circle in segments (randomizing circles radius on each iteration) create sort of 'bumpy' polygon. vertices stored in nsmutablearray , fed ccdrawpolygon (code below):

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 crashing internally on call draw. believe problem may in how i'm grabbing polygon vertices out of nsmutablearray cache , attempting plot them:

cgpoint cgvertices[[vertices count]];      for(int = 0; < [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); 

for bit of further information, i've included example of vertices array contents before cocos2d crashes. make little easier visualise, i've included graphic created (where blue pixel represents circles centre, white pixels vertexes , red lines lines connecting them).

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"     ) ) 

are drawing outside draw method? if are, following explanation might help.

you cannot use ccdrawpoly outside ccnode draw function. notice states in caps 'don't draw stuff outside method'. in following definition of ccnode's draw method.

// ccnode's draw method

-(void) draw {         // override me         // use function draw stuff.         // don't draw stuff outside method } 

example of ccdrawpoly in cocos2d-ios: 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); } 

source:draw , update reference


Comments