1

我正在尝试在 box2d 环境中设置用于路径查找的网格。是我的绘图方法导致没有绘制它们之间的节点和链接吗?这是我绘制世界的主要课程:

@interface HelloWorldLayer()
@end

@implementation HelloWorldLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    HelloWorldLayer *layer = [HelloWorldLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(id) init
{
    if( (self=[super init]))
    {
        [self createGrid];
        [self drawGrid];
    }
    return self;
}

-(void) dealloc
{   
    [super dealloc];
}

-(void) createGrid
{    
    //Create 2D array (grid)
    nodeSpace = 50.0f;
    gridSizeX = 500;
    gridSizeY = 500;
    grid = [[NSMutableArray alloc] initWithCapacity:(gridSizeX)];

    for(int x=0; x<gridSizeX; x++){
        [grid addObject:[[NSMutableArray alloc] initWithCapacity:(gridSizeY)]];
    }

    //Create AStar nodes
    for(int x=0; x<gridSizeX; x++){
        for(int y=0; y<gridSizeY; y++){
            //Add a node
            AStarNode *node = [[AStarNode alloc] init];
            node.position = ccp(x*nodeSpace + nodeSpace/2, y*nodeSpace + nodeSpace/2);
            [[grid objectAtIndex:x] addObject:node];
        }
    }

    //Add neighbors
    for(int x=0; x<gridSizeX; x++){
        for(int y=0; y<gridSizeY; y++){
            //Add a node
            AStarNode *node = [[grid objectAtIndex:x] objectAtIndex:y];

            //Add self as neighbor to neighboring nodes
            [self addNeighbor:node toGridNodeX:x-1 Y:y-1]; //Top-Left
            [self addNeighbor:node toGridNodeX:x-1 Y:y]; //Left
            [self addNeighbor:node toGridNodeX:x-1 Y:y+1]; //Bottom-Left
            [self addNeighbor:node toGridNodeX:x Y:y-1]; //Top

            [self addNeighbor:node toGridNodeX:x Y:y+1]; //Bottom
            [self addNeighbor:node toGridNodeX:x+1 Y:y-1]; //Top-Right
            [self addNeighbor:node toGridNodeX:x+1 Y:y]; //Right
            [self addNeighbor:node toGridNodeX:x+1 Y:y+1]; //Bottom-Right
        }
    }
}

/* Add neighbor helper method */
-(void) addNeighbor:(AStarNode*)node toGridNodeX:(int)x Y:(int)y {
    if(x >= 0 && y >= 0 && x < gridSizeX && y < gridSizeY){
        AStarNode *neighbor = [[grid objectAtIndex:x] objectAtIndex:y];
        if(![AStarNode isNode:neighbor inList:node.neighbors]){
            [node.neighbors addObject:neighbor];
        }
    }
}

-(void) drawGrid
{
        for(int x=0; x<gridSizeX; x++){
            for(int y=0; y<gridSizeY; y++){
                //Draw node
                AStarNode *node = [[grid objectAtIndex:x] objectAtIndex:y];
                ccDrawColor4F(16, 16, 16, 8);
                ccDrawPoint(node.position);

                //Draw neighbor lines (there is going to be a lot of them)
                for(int i=0; i<node.neighbors.count; i++){
                    AStarNode *neighbor = [node.neighbors objectAtIndex:i];
                    ccDrawColor4F(16, 16, 16, 8);
                    ccDrawLine(node.position, neighbor.position);
                }
            }
        }
}

@end
4

2 回答 2

2

您只能在 draw 或 visit 方法中使用 OpenGL 进行绘制。

于 2013-03-16T17:21:36.347 回答
1

我不认为绘图会在 init 中起作用。尝试将其放入 onEnter

-(void) onEnter {
    // at this point, the node has been added, is running
    // and will be visited as appropriate (i think)
    [super onEnter];   // dont forget to super any 'onMethod' override of cocos2d
    [self drawGrid];
}
于 2013-03-16T17:14:35.400 回答