0

ここにオリジナルの投稿をしました

最初は、CGPoint**allPathsを埋めようとしました。

最初の次元は「パス」であり、2番目の次元は「ウェイポイント」です。そして、私はこれからCGPointを取得します。

例:allPaths [0] [2]は、最初のパス、3番目のウェイポイントのCGPointを提供します。

厄介なループのあるプレーンCで正常に実行しました。そして今、私はNSMutableArraysを使ってObj-Cで同じことをしようとしています。

これが私のコードです:

CCTMXObjectGroup *path;
NSMutableDictionary *waypoint;

int pathCounter = 0;
int waypointCounter = 0;

NSMutableArray *allPaths = [[NSMutableArray alloc] init];
NSMutableArray *allWaypointsForAPath = [[NSMutableArray alloc] init];

//Get all the Paths
while ((path = [self.tileMap objectGroupNamed:[NSString stringWithFormat:@"Path%d", pathCounter]]))
{
    waypointCounter = 0;
    //Empty all the data of the waypoints (so I can reuse it)
    [allWaypointsForAPath removeAllObjects];

    //Get all the waypoints from the path
    while ((waypoint = [path objectNamed:[NSString stringWithFormat:@"Wpt%d", waypointCounter]]))
    {
        int x = [[waypoint valueForKey:@"x"] intValue];
        int y = [[waypoint valueForKey:@"y"] intValue];

        [allWaypointsForAPath addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];
        //Get to the next waypoint
        waypointCounter++;
    }

    //Add the waypoints of the path to the list of paths
    [allPaths addObject:allWaypointsForAPath];

    //Get to the next path
    pathCounter++;
}

私の実際の問題は、allPathsのすべてのパスが最後のパスと等しいことです。(最初のパスはすべて最後のパスでオーバーライドされます)

この行[allPathsaddObject:allWaypointsForAPath]が原因であることを私は知っています。

それでも、どうすればいいですか?

4

1 に答える 1

0

ああ、私は何かを見つけたと思います!メモリの問題についてはわかりませんが、ガベージコレクターは機能するはずです。

実際には、次のようなループでNSMutableArray*allWaypointsForAPathを宣言する必要があります。

int pathCounter = 0;
int waypointCounter = 0;

NSMutableArray *allPaths = [[NSMutableArray alloc] init];

//Get all the PathZ
while ((path = [self.tileMap objectGroupNamed:[NSString stringWithFormat:@"Path%d", pathCounter]]))
{
    waypointCounter = 0;
    NSMutableArray *allWaypointsForAPath = [[NSMutableArray alloc] init];
    //Get all the waypoints from the path
    while ((waypoint = [path objectNamed:[NSString stringWithFormat:@"Wpt%d", waypointCounter]]))
    {
        int x = [[waypoint valueForKey:@"x"] intValue];
        int y = [[waypoint valueForKey:@"y"] intValue];
        [allWaypointsForAPath addObject:[NSValue valueWithCGPoint:CGPointMake(x, y)]];
        //Get to the next waypoint
        waypointCounter++;
    }

    [allPaths addObject:allWaypointsForAPath];
    //Get to the next path
    pathCounter++;
}
于 2012-07-28T07:14:07.780 に答える