我有几张地图(使用Tiled QT制作的地图),我想根据这些地图的对象组(我叫它Waypoints)创建一个CGpoint **数组。如何创建一个动态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),它为我整个路径阵列将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
我得到一个EX_BAD_ACCESS在NSLog末尾
编辑 谢谢关于CGPoint的NSLog(%@)。 不过,我得到这条线(在丑陋的循环)的y值:
所有的NSLog(@"x : %f & y : %f",pathArray[pathCounter][waypointCounter].x,pathArray[pathCounter][waypointCounter].y);
你的代码看起来不错,你可以显示到'NSLog'的确切调用吗?你不打算用'%@'说明符打印'pathArray',对吗?因为该说明符是用于对象的,而'pathArray'是一个C数组。您需要使用与填充它相同的两个嵌套循环排列来完成它。 – dasblinkenlight 2012-07-27 10:14:31