iphone - 使用cocos2d的等距平铺游戏中的问题

标签 iphone ios cocos2d-iphone isometric

我一直在努力理解 Knight FightTiled Map Editor 制作的等距游戏

当我打开一个新项目并加载同一张 map 时,这个特殊函数会给我不同的结果。

-(CGPoint) locationFromTilePos:(CGPoint)tilePos;
{
    CCTMXLayer *grass = [self.tileMap layerNamed:@"Grass"];
    CCSprite *tile = [grass tileAt:tilePos];
    float x = -tile.position.x - self.tileMap.tileSize.width + 32;
    float y = -tile.position.y - self.tileMap.tileSize.height;
    return CGPointMake(x, y);
}

在 Knight Fight 中将 tilePos 作为 (0,0) 输入时

  1. 草地 block 位置:(1248, 1248)
  2. 函数返回的最终位置:(-1280,-1280)

在我的新项目中将 tilePos 作为 (0,0) 输入时

  1. 草地 block 位置:(624, 624)
  2. 函数返回的最终位置:(-656,-656)

我无法在 Cocos2d 上使用 Tiled 在线找到任何等距 map 资源。我需要在图 block 坐标和实际屏幕坐标之间进行转换。谁能帮忙。

最佳答案

试试这个: //从瓦片坐标计算屏幕坐标

- (CGPoint)positionForTileCoord:(CGPoint)pos {

float halfMapWidth = _tileMap.mapSize.width*0.5;
float halfMapHeight = _tileMap.mapSize.height*0.5;
float tileWidth = _tileMap.tileSize.width;
float tileHeight = _tileMap.tileSize.height;

int convertedY = _tileMap.mapSize.height-(pos.y-1);

int x = halfMapWidth*tileWidth + tileWidth*pos.x*0.5-tileWidth*pos.y*0.5;
int y = halfMapHeight*tileHeight +tileHeight*convertedY*0.5 - tileHeight*pos.x*0.5-tileHeight*0.5;


return ccp(x, y);

}

反之则试试这个:

// calculating the tile coordinates from screen location

-(CGPoint) tilePosFromLocation:(CGPoint)location
{
CGPoint pos = location;
float halfMapWidth = _tileMap.mapSize.width*0.5;
float mapHeight = _tileMap.mapSize.height;
float tileWidth = _tileMap.tileSize.width;
float tileHeight = _tileMap.tileSize.height;

CGPoint tilePosDiv = CGPointMake(pos.x/tileWidth, pos.y/tileHeight);
float invereseTileY = mapHeight - tilePosDiv.y;

// Cast int to make sure that result is in whole numbers

float posX = (int)(invereseTileY + tilePosDiv.x - halfMapWidth);
float posY = (int)(invereseTileY - tilePosDiv.x + halfMapWidth);

return CGPointMake(posX, posY);
}

关于iphone - 使用cocos2d的等距平铺游戏中的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18175622/

相关文章:

iphone - NSSetUncaughtExceptionHandler 无法捕获 iPhone 上的所有错误

iphone - 我可以通过 Spotlight 搜索我的 iPhone 应用程序的内容吗?

ios - 如果应用程序未安装在 iOS 设备上,通用链接将不起作用

ios - 如何读取一个IOS App项目

ios - Crashlytics 不显示崩溃报告

c++ - box2d:在 ResetMassData 上获取 SIGABRT,b2Assert(m_I > 0.0f) - 中心惯性、质心

ios - cocos2d : graphics tool

iphone - PhoneGap : download & save binary files (mp3s) for iPhone & Android

objective-c - 解析 Twitter 获取状态 Objective C

ios - 使用 UIAccelerometer 重绘 CCSprite 的速度不够快?