我有幾張地圖(使用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