我正在尝试在迷宫中实现对象的随机移动,因此我使用可碰撞的墙制作了tilemap。我正在使用这样的代码检查对象可以移动的每个方向的碰撞:
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]]];
但是在极少数情况下,如果有一个死胡同,或者从正确的物体向下的路径正在穿过墙壁。有什么想法如何解决这样的环境? 感谢。
答案 0 :(得分: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;
}