如何使用tile坐标而不是cocos2d中的常规坐标

时间:2012-08-18 22:51:04

标签: iphone objective-c ios xcode cocos2d-iphone

我还是cocos2d的新手,我有这个问题,当我使用ccp坐标时,即使我移动到下一个屏幕,它也会保持在同一位置。我得出的结论是,我不是在使用瓷砖坐标。所以我需要一些帮助转换。

   - (CGPoint)tileCoordForPosition:(CGPoint)position {
float x = floor(position.x / map.tileSize.width);
float levelHeightInPixels = map.mapSize.height * map.tileSize.height;
float y = floor((levelHeightInPixels - position.y) / map.tileSize.height);
return ccp(x, y);
  }

-(CGRect)tileRectFromTileCoords:(CGPoint)tileCoords {
float levelHeightInPixels = map.mapSize.height * map.tileSize.height;
CGPoint origin = ccp(tileCoords.x * map.tileSize.width, levelHeightInPixels -    ((tileCoords.y + 1) * map.tileSize.height));
return CGRectMake(origin.x, origin.y, map.tileSize.width, map.tileSize.height);
} 

这就是我用来将常规坐标转换为平铺贴图坐标的方法。

现在我试图在特定位置(0,0)

产生一个敌人
     -(void)addsaw{

CCSprite *saw = [CCSprite spriteWithFile:@"saw.png"]; 
// Determine where to spawn the target along the Y axis
CGSize winSize = [[CCDirector sharedDirector] winSize];
int minY =saw.contentSize.height/2;
int maxY = winSize.height - saw.contentSize.height/2;
int rangeY = maxY - minY;
int actualY =  ( rangeY) + minY;

//THIS IS THE PROBLEM RIGHT HERE
saw.position = ccp(0,0);
[self addChild:saw];
// Create the actions
id actionMove = [CCMoveTo actionWithDuration:actualDuration 
                                    position:ccp(-saw.contentSize.width/2, actualY)];
id actionMoveDone = [CCCallFuncN actionWithTarget:self 
                                         selector:@selector(spriteMoveFinished2:)];
[saw runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
saw.tag = 1;
[_saws addObject:saw];

   }

如何更改我的addaw的ccp,以便我可以在tile(0,0)而不是cocos2d regular(0,0)

中生成它

1 个答案:

答案 0 :(得分:3)

你应该将你的精灵添加到cctmxtiledmap而不是图层!