Moo Moo
Moo Moo

Reputation: 7

How do I use tile coordinates instead of the regular coordinates in cocos2d

I am still new to cocos2d and I have this problem that when I am using the ccp coordinates it stays at same position even when I move to the next screen. I came to the conclusion it must be that I am not using the tiles coordinates. So I need some help converting.

   - (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);
} 

that is what i use to translate regular coordinates into tile map coordinates.

now I am trying to spawn an enemy at a certain position specifically (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];

   }

How do I change the ccp of my addsaw so I can have it spawn at the tile (0,0) rather than the cocos2d regular (0,0)

Upvotes: 0

Views: 323

Answers (1)

Eric Yang
Eric Yang

Reputation: 1022

you should add your sprite to your cctmxtiledmap instead of the layer!

Upvotes: 3

Related Questions