0

我试图在迷宫中实现物体的随机移动,所以我用可碰撞的墙壁制作了瓷砖地图。我正在使用这样的代码检查对象可以移动的每个方向的碰撞:

int directionsCount = 4;
CGPoint position = sprite.position;
CCArray *newPositions = [CCArray arrayWithCapacity:directionsCount];
CGFloat tileSize = _tileMap.tileSize.width;
[newPositions addObject:[NSValue valueWithCGPoint:CGPointMake(position.x, position.y + tileSize)]];
[newPositions addObject:[NSValue valueWithCGPoint:CGPointMake(position.x + tileSize, position.y)]];
[newPositions addObject:[NSValue valueWithCGPoint:CGPointMake(position.x, position.y - tileSize)]];
[newPositions addObject:[NSValue valueWithCGPoint:CGPointMake(position.x - tileSize, position.y)]];
for (NSValue *value in newPositions) {
    int tileGid = [_meta tileGIDAt:[self tileCoordForPosition:[value CGPointValue]]];
    if (tileGid) {
        NSDictionary *properties = [_tileMap propertiesForGID:tileGid];
        if (properties) {
            NSString *collision = [properties valueForKey:@"Collidable"];
            if (collision && [collision compare:@"True"] == NSOrderedSame) {
                [newPositions removeObject:value];
            }
        }
    }
}
[sprite runAction:[CCMoveTo actionWithDuration:1.0f position:[[newPositions objectAtIndex:(CCRANDOM_0_1() * [newPositions count])] CGPointValue]]];

但在少数情况下,当有死胡同或路径从正确的物体转向穿过墙壁时。有没有办法解决这样的环境?谢谢。

4

1 回答 1

0

这是我的瓷砖地图代码:

- (void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    UITouch *myTouch = [touches anyObject];
    CGPoint location = [myTouch locationInView:[myTouch view]];
    location = [[CCDirector sharedDirector] convertToGL:location];


    [self MoveToTilePosition:location];
}

//这里 cat = 你的主要移动精灵

-(void)MoveToTilePosition:(CGPoint)inPoint
{
     CCTMXLayer *layer = [_tileMap layerNamed:@"Background"];

     CGPoint point= [self MyTileCoordForPosition:inPoint];
     CCSprite *tileN = [layer tileAt:point];

     CGPoint point2= [self MyActerTileForPosition:cat.position];

     CCSprite *catTile = [layer tileAt:point2];

    if( ( (point.x==point2.x) || (point.y==point2.y) )
       &&
       !( (point.x==point2.x) && (point.y==point2.y) )
       )
    {
        CGPoint newPos = ccp(tileN.position.x+TILE_SIZE/2,tileN.position.y+(TILE_SIZE/2) );

        if([self checkForMovement:point2 To:point ])
        {
            [cat runAction:[CCMoveTo actionWithDuration:1 position:newPos]];
        }
    }
}


- (CGPoint)MyActerTileForPosition:(CGPoint)position 
{
    int maxTileCol = _tileMap.mapSize.height; 

    int x = ( (position.x)/TILE_SIZE);
    int y = maxTileCol - ( ((position.y))/TILE_SIZE);
    return ccp(x, y);
}

typedef enum GRID_TYPE{
        kGrideCollidable,
        kGrideCollectable,
        kGrideNormal,

}GridType;

-(bool)checkForMovement:(CGPoint)start To:(CGPoint)end
{

    bool isFreePath = true;

    bool isXDif = (start.x!=end.x) ? true :false;

    if(isXDif)
    {
        int diff = end.x-start.x ;

        for (int i=1; i<=abs(diff); i++) {
            CGPoint pt = start;

            if(diff>0)
                pt.x+= i;
            else
                pt.x-= i;

        //    printf("toMove(%f,%f)  \n",pt.x,pt.y);

            GridType type = [self getTileType:pt ];

            if(type==kGrideCollidable)
                isFreePath = false;   
        }
    }
    else
    {
        int diff = end.y-start.y ;

        for (int i=1; i<=abs(diff); i++) {
            CGPoint pt = start;

            if(diff>0)
                pt.y+= i;
            else
                pt.y-= i;

            GridType type = [self getTileType:pt ];

            if(type==kGrideCollidable)
                isFreePath = false;   
        }
    }

    return isFreePath;
}


-(GridType)getTileType:(CGPoint)position {    
     GridType type = kGrideNormal;

    CGPoint tileCoord = position;//[self tileCoordForPosition:position];
    unsigned int tileGid = [_meta tileGIDAt:tileCoord];
    if (tileGid) {
        NSDictionary *properties = [_tileMap propertiesForGID:tileGid];
        if (properties)
        {
            NSString *collision = [properties valueForKey:@"Collidable"];
            if (collision && [collision compare:@"True"] == NSOrderedSame) {
                 type = kGrideCollidable ;
            }
            NSString *collectable = [properties valueForKey:@"Collectable"];
            if (collectable && [collectable compare:@"True"] == NSOrderedSame) {
                type = kGrideCollectable ;
                 // [[SimpleAudioEngine sharedEngine] playEffect:@"pickup.caf"];
            }
        }
    }
    return type;    
}
于 2012-07-26T17:56:48.737 回答