Multiple CCTMXTiledMaps for iPhone Game - iphone

So I want to divide my game into chunks by using several different CCTMXTiledMaps.
I am able to load the maps into my main 'HelloWorldLayer'. I am also able to detect whether the player sprite collides with a tile with the property of 'collectable'.
My problem occurs when I add several CCTMXTiledMap nodes to the game, as it doesn't do the collectible tile detection on all of them, just the first one.
Here is my working code that does the check, but only for the first added CCTMXTledMap:
CGPoint point = [self getTileCoordForPosition:position :map];
CCTMXLayer *metaLayer = [map layerNamed:#"Meta"];
CCTMXLayer *foregroundLayer = [map layerNamed:#"Foreground"];
CCSprite *metaTile = [metaLayer tileAt:point];
CCSprite *foregroundTile = [foregroundLayer tileAt:point];
if (foregroundTile)
{
NSLog(#"HIT!");
// Remove the meta tile and the foreground tile
[metaLayer removeTileAt:point];
[foregroundLayer removeTileAt:point];
}
How can I make this code do the check for every CCTMXTiledMap node that has been added?

The problem was that I was calculating the tile map positions wrong, in a tile map co-ordinates to map position function.
I was multiplying by the CC_SCALE_RATIO() function, or something like that (going off the top of my head), and it was mis-calculating the pixel positioning.
Just thought I'd write in an answer since I found the solution. Hope it helps somebody!

Related

Coordinate system in Cocos2D

I'm having problems detecting collisions in Cocos2D, because the coordinates I use to see if an object collided are always wrong.
I have the following objects:
All the sprites from a tiled map's layer, containing all the obstacles;
A sprite which is child of another sprite, which is child of the hello world layer (the main layer I'm using, which returns the scene). This sprites continuously moves in the map, and it may collide with the obstacles.
To detect collision I just see look at the distance between 2 sprite's bounding boxes:
inline BOOL collision(CGRect r1, CGRect r2)
{
CGPoint c1= RectCenter(r1);
CGPoint c2= RectCenter(r2);
BOOL result= (fabs(c1.x-c2.x)<MAX(r1.size.width/2.0,r2.size.width/2.0)) && (fabs(c1.y-c2.y)<MAX(r1.size.height/2.0,r2.size.height/2.0));
return result;
}
I use the boundingBox property to get the coordinate and the size of every sprite. But the coordinates are wrong, and it doesn't detect collisions correctly.
I'm pretty sure that I'm doing something wrong, could someone tell me how is the way to manage all sprites to have the same coordinate system? I also tried with convertToWordSpace, but with no luck.
You can do away with collision method in favor of the builtin function for seeing if two CGRects intersect:
CGRectIntersectsRect(r1, r2);

iphone - Cocos2d puzzle game ,how to solve piece position?

i need help with my first app with cocos2d,
I'm trying to develop puzzle game,
the game has image pieces and it's transparent background , and i need to match thees peices to the correct place/position !
my problem is that i don't know how to find the exact position/ place of the piece on it's place on the image ?
can any one help me please ?
trying to make game like this :
Animal Puzzle
and puzzle image and pieces like this :
Puzzle Image & pieces
If I'm understanding correctly, you want to detect when the user has dragged a puzzle piece into the correct slot.
If that's the case, and correct me if I'm misunderstanding, you'll need at least two pieces of information to make it happen: the position of the puzzle piece's correct location (i.e. the position the piece should be in when the puzzle is put together), and the same puzzle piece's current location as it's being moved by the user. Once you have this information, perhaps as two CGPoints (correctPosition and currentPosition), you can get the distance between them with ccpDistance():
CGPoint currentPosition = selectedPuzzlePiece.position;
float distanceFromCorrectPos = ccpDistance(correctPosition, currentPosition);
In the above example, correctPosition could be a CGPoint stored before the pieces are mixed up, and selectedPuzzlePiece could be the CCSprite the user is currently touching. You could then use the distanceFromCorrectPos value to determine when the user has moved the piece close enough to snap it into place:
float snapIntoPlaceThreshold = 10.0f;
if(distanceFromCorrectPos <= snapIntoPlaceThreshold) {
selectedPuzzlePiece.position = correctPosition;
}

How to Repeat (Continuse) TileMap in Cocos 2d?

I made a game using cocos 2d.
I made a Tile based map, in which I want to repeat the map when it is complete.
My problem is in the game, the map moves completely for the first time but I want to continues repeat it.
I am using the following code to move the map.
-(void) moveMap
{
// Create the actions
id actionMove = [CCMoveTo actionWithDuration:10.0 position:ccp(-1,(-self.tileMap.contentSize.height))];
id actionMoveDone = [CCCallFuncN actionWithTarget:self selector:#selector(setViewpointCenter:)];
[self.tileMap runAction:[CCSequence actions:actionMove, actionMoveDone, nil]];
}
This works properly and the map moves downward till map height, after that the map will disappear. Now I want to continue repeating the map.
Separate your tile map into two smaller tile maps and put them one next to each other.
Then in your update method check their positions and when first one goes offscreen, move it after second one. When second one goes offscreen, move it after first one.
You can use a single map that's larger that the screen, then every 32px in any given direction, shift the map back 32px. This is useful for infinite maps. It requires one a single object and texture, plus is works both for vertical and horizontal motion. It can be a pain to time correctly, but so can the multiple map scenario.
(I upvoted fichek's answer because it's the most common.)

Static and moving shapes in spacemanager

Dear all,
I have an application that uses cocos2d spacemanager with gravity set to a specific value.
If i want to make a shape in the middle of the screen it will fall down to the floor, if i set the gravity to zero all other object will not move as supposed, if i use a second spacemanager and set its gravity to 0 i cant detect collision between objects from different spacemanagers. how can i add a shape that wont fall down in the middle of the screen and detect its collision while other objects behave correctly according to the gravity set.
Also a question is, should i use shapes (Circle, rectangle, ... etc) with spacemanager and if i want to use a ccsprite (image) i should put it in a shape or i can use the sprite alone (e.g. a tree is not a rectangle or circle collision and reflection wont be natural how can i do this).
regards
Every shape has a property called mass. If you want a shape to be static and respond to collisions just set mass to STATIC_MASS like this:
cpShape *ball = [smgr addCircleAt:cpv(440,70) mass:STATIC_MASS radius:10];
to ad an image, do that:
cpShape *ball = [smgr addCircleAt:cpv(440, 70) mass:STATIC_MASS radius:10];
[super initWithShape:playerShape file:#"ball.png"];
If this doesn't work, set up a cpCCSprite in it with a shape.
You can search for cpCCSprite on google, im sure you'l find something :)

Combining two textures in one

I'm developing a game for iOS. I'm using cocos2d libs. I want to have an object, that have 3 parts - beginning, ending and the middle. I've got the image with these components. And the object can be stretched, when created. But only the middle part should be stretched, the beginning and endings should have no scaling. Because this operation is done only once i decided it's a good idea to create a new CCSprite for this object, and not to keep three (for increasing performance).
I'm using CCSPriteBatchNode for rendering, and i don't know if i really need to combine the object's parts (maybe rendering 3 parts using batch will be as fast as rendering one pre-combined object).
So there are two quastions:
Do i need to combine parts in one object?
If, yes - how can i do that?
Instead of combining the textures you could create a node and add the three sprites as children to it. You can then work with the parent node as a single entity.
Something along the lines of:
CCNode *sprites = [CCNode node];
CCSprite *spriteA = [CCSprite spriteWithSpriteFrameName:#"spriteA.png"];
spriteA.position = ccp(-10, 0);
[sprites addChild:spriteA];
CCSprite *spriteB = [CCSprite spriteWithSpriteFrameName:#"spriteB.png"];
spriteB.position = ccp(0, 0);
[sprites addChild:spriteB];
CCSprite *spriteC = [CCSprite spriteWithSpriteFrameName:#"spriteC.png"];
spriteC.position = ccp(10, 0);
[sprites addChild:spriteC];
You can scale and position each individual sprite depending on your parameters then work with the sprites object to position/scale them as a whole.
There might be a small performance hit so I would think twice before using this for a large amount of sprites, but I've been using this method in a few situations and in my case I didn't notice any issues with performance.
Look at the RenderTexture demo.
Instead of using the brush, you can use put your 3 parts onto it using those images instead of the brush.