Hey all,
Okay, I've been having a problem. I'm making a simple tiled RPG-like game using Tiled and Cocos2D. Following one of Ray Wenderlich's tutorials (found here: http://www.raywenderlich.com/1186/collisions-and-collectables-how-to-make-a-tile-based-game-with-cocos2d-part-2 ), I managed to add collision to my tiled map. However, I didn't like the way he implemented movement in the game and instead opted for a directional pad. I found a very nice bit of code called EasyArrows (found here: http://www.cocos2d-iphone.org/forum/topic/7692 ) that accomplished exactly what I was hoping to do. The problem I'm facing is that I can get the two to work separately but not together. Before I add EasyArrows, the collision and movement work just fine. After I add EasyArrows, my character sprite completely ignores the collision, but he moves how I want him to.
As you might have guessed, I'm a bit of a newbie still, but I'm starting to get the hang of things. One of the best ways to learn is to make mistakes, eh? :P
If you'd like to see any specific segments of code, let me know. I'll post them here.
Thanks guys!
EDIT:
I've decided to go ahead and put my code in anyway.
"stLayer" is the collision tile layer in my Tiled map (see Ray's tutorial for details)
"mage" is my player sprite
-----
HelloWorldLayer.m
// This is the part of the EasyArrows code that you place within the HelloWorldLayer
-(void)update:(ccTime)dt{
if ([[menu getButton:2]isSelected])
{
mage.position = ccp(mage.position.x,mage.position.y + 3);
}
if ([[menu getButton:8]isSelected])
{
mage.position = ccp(mage.position.x,mage.position.y - 3);
}
if ([[menu getButton:4]isSelected])
{
mage.position = ccp(mage.position.x + 3,mage.position.y);
}
if ([[menu getButton:6]isSelected])
{
mage.position = ccp(mage.position.x - 4,mage.position.y);
}
}
// This is Rey's code for the collision
-(void) setPlayerPosition:(CGPoint)position{
CGPoint tileCoord = [self tileCoordForPosition:position];
int tileGid = [stLayer tileGIDAt:tileCoord];
if(tileGid){
NSDictionary *properties = [theMap propertiesForGID:tileGid];
if(properties){
NSString *collision = [properties valueForKey:@"Collidable"];
if (collision && [collision compare:@"True"] ==NSOrderedSame) {
return;
}
}
}
mage.position = position;
}
// This converts tiles to coordinates
-(CGPoint)tileCoordForPosition:(CGPoint)position{
int x = position.x/theMap.tileSize.width;
int y = ((theMap.mapSize.height * theMap.tileSize.height)-position.y)/theMap.tileSize.height;
return ccp(x,y);
}