我有一些地图(使用 Tiled QT 制作的瓷砖地图),我想根据这些地图的对象组创建一个 CGpoint **数组(我称它们为航点)。
每张地图都可以有几组我称之为路径的航路点。
//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) 时,它向我显示整个 pathArray 将 x 和 y。
但是2 个问题:
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);