Frage

I have a few maps (tilemaps made with Tiled QT) and I would like to create a CGpoint **array based on the objects groups of those maps (I call them Waypoints).

Each maps can have a few set of waypoints that I call path.

//Create the first dimension
int nbrOfPaths = [[self.tileMap objectGroups] count];
CGPoint **pathArray = malloc(nbrOfPaths * sizeof(CGPoint *));

Then for the second dimension

//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++;
}

Now I want to fill up the 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++;
}

When I NSLog(@"%@",pathArray), it shows me the entire pathArray will the x and y.

HOWEVER 2 problems :

  • The y value is never correct (the x value is correct and my tilemap.tmx is correct too)

    <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
    
  • I get a EX_BAD_ACCESS at the end of the NSLog

EDIT Thank you about the NSLog(%@) concerning CGPoint. However, I get the y value with this line (in the ugly loop):

NSLog(@"x : %f & y : %f",pathArray[pathCounter][waypointCounter].x,pathArray[pathCounter][waypointCounter].y);
War es hilfreich?

Lösung

First of all, you can't NSLog CGPoint like that because it is not an object. %@ expects an objective c object to send a description message to.

Secondly, you can use an NSValue wrapper and then use NSMutableArray as you would with any other object. Is there a reason you don't want to do that? You can add arrays inside other arrays as you are doing.

Andere Tipps

Regarding the first problem:

The y value is never correct (the x value is correct and my tilemap.tmx is correct too)

Have you noticed that if you add the y values from tile map and from NSLog they always add up to 640? Then you better check if tilemap y-coordinate is top-to-bottom as oppose to CGPoint's bottom-to-top. Then you can always do 640 - y to convert the y-coordinate between the two coordinate systems.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top