如何创builddynamicCGPoint **数组
我有一些地图(用Tiled QT制作的地图),我想根据这些地图的对象组创build一个CGpoint **数组(我称之为Waypoints)。
每个地图可以有几个我称之为path的路标。
//Create the first dimension int nbrOfPaths = [[self.tileMap objectGroups] count]; CGPoint **pathArray = malloc(nbrOfPaths * sizeof(CGPoint *));
然后是第二个维度
//Create the second dimension int pathCounter = 0; while ((path = [self.tileMap objectGroupNamed:[NSString stringWithFormat:@"Path%d", pathCounter]])) { int nbrOfWpts = 0; while ((waypoint = [path objectNamed:[NSString stringWithFormat:@"Wpt%d", nbrOfWpts]])) { nbrOfWpts++; } pathArray[pathCounter] = malloc(nbrOfWpts * sizeof(CGPoint)); pathCounter++; }
现在我想填写pathArray
//Fill the array pathCounter = 0; while ((path = [self.tileMap objectGroupNamed:[NSString stringWithFormat:@"Path%d", pathCounter]])) { int waypointCounter = 0; //Get all the waypoints from the path while ((waypoint = [path objectNamed:[NSString stringWithFormat:@"Wpt%d", waypointCounter]])) { pathArray[pathCounter][waypointCounter].x = [[waypoint valueForKey:@"x"] intValue]; pathArray[pathCounter][waypointCounter].y = [[waypoint valueForKey:@"y"] intValue]; NSLog(@"x : %f & y : %f",pathArray[pathCounter][waypointCounter].x,pathArray[pathCounter][waypointCounter].y); waypointCounter++; } pathCounter++; }
当我NSLog(@“%@”,pathArray),它显示了我的整个patharrays将x和y。
但是有两个问题 :
-
y值永远不会正确(x值是正确的,我的tilemap.tmx也是正确的)
<object name="Wpt0" x="-18" y="304"/> <-- I get x : -18 and y :336 with NSLog <object name="Wpt1" x="111" y="304"/> <-- I get x : 111 and y :336 <object name="Wpt2" x="112" y="207"/> <-- I get x : 112 and y :433
-
我在NSLog的末尾得到一个EX_BAD_ACCESS
编辑谢谢关于CGPoint的NSLog(%@)。 但是,我得到这个行的y值(在丑陋的循环中):
NSLog(@"x : %f & y : %f",pathArray[pathCounter][waypointCounter].x,pathArray[pathCounter][waypointCounter].y);
首先,你不能这样NSLog CGPoint,因为它不是一个对象。 %@
需要一个客观的c对象来发送description
信息。
其次,你可以使用一个NSValue
包装,然后像使用任何其他对象一样使用NSMutableArray
。 有没有你不想这样做的理由? 你可以在其他数组内添加数组。
关于第一个问题:
y值永远不会正确(x值是正确的,我的tilemap.tmx也是正确的)
你有没有注意到,如果你添加瓷砖地图和NSLog的y值,他们总是加起来640? 那么你最好检查一下,tilemap的y坐标是否与CGPoint的bottom-to-top相反。 那么你总是可以做640-y转换两个坐标系之间的y坐标。