Porting Cocos2d 1.0 to Cocos2d 2.0 - cocos2d-iphone

I got one old game and I tied to update Cocos2d 2.0 SDK. I got some compilation error.
b2Vec2 gravity;
gravity.Set(0.0f, -10.0f);
self.world = new b2World(gravity, true);
Error: No matching constructor for initialization of 'b2World'
When I change this to below code then works but Box2D debug shapes are not drawn.
self.world = new b2World(gravity);
How to initialize Box2d world in right way that show debug shapes?

Replace the GLESDebugDraw files with those found in a newly created cocos2d 2.0 + Box2D project. Your version is still using GL ES 1.1 commands which don't work in cocos2d 2.x

Finally I got debug shape by replacing this draw function and GLESDebugDraw files.
-(void) draw
{
[super draw];
ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );
kmGLPushMatrix();
self.world->DrawDebugData();
kmGLPopMatrix();
}

Related

Tile map tutorial for cocos2d 3.x?

I wanted to follow a tile map game tutorial for Cocos2D:
http://www.raywenderlich.com/29458/how-to-make-a-tile-based-game-with-cocos2d-2-x
However, it seems I can't follow this with Cocos2D 3.0 as the new Cocos2D doesn't seem to even include CCTMXLayer and CCTMXTiledMap.
I suppose I could install an older version of Cocos2D, but that could open a whole new can of worms. Is there some up to date tutorial or system for tile map games?
Thanks
Found a similar question with up to date version of Cocos2D objects:
"How To Make a Tile-Based Game with Cocos2D 2.X" Make this tutorial with cocos2d V3
for example:
HelloWorldScene.h
cpp
// Inside the HelloWorld class declaration
cocos2d::Sprite *_player;
HelloWorldScene.cpp
cpp
// Inside the init method, after setting "_background ="
TMXObjectGroup *objects = _tileMap->getObjectGroup("Objects");
CCASSERT(NULL != objects, "'Objects' object group not found");
auto spawnPoint = objects->getObject("SpawnPoint");
CCASSERT(!spawnPoint.empty(), "SpawnPoint object not found");
int x = spawnPoint["x"].asInt();
int y = spawnPoint["y"].asInt();
_player = Sprite::create("Player.png");
_player->setPosition(x, y);
addChild(_player);
setViewPointCenter(_player->getPosition());

How to change the image of a CCSprite in cocos2d v3.x

In cocos2d 2.x we change the image of a CCSprite using CCTexture. But in cocos2d 3.x CCTextureCache seems to be deprecated as Xcode warns me : "undeclared identifier 'CCTextureCache'". Or may be am I miss something as I'm new to cocos.
So how can we change the image of a CCSprite in v3 ??
Thank you.
I think I know how to do.
We have to use a spriteSheet built with TexturePacker [note : may be it's wrong to speak about external resources like it on SO] for example (let's say we have 2 images : monster_01.png and monster_02.png).
We add the .plist and the .png into xCode
We put the spritesheet in cache
and then we can create a CCSprite with a frame using a item of the spritesheet.
This image can be changed.
Some code :
3) We put in cache
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:#"monsterSpriteSheet.plist"];
4) We create the sprite
CCSprite * mySprite = [CCSprite initWithSpriteFrame: [CCSpriteFrame frameWithImageNamed: #"monster_01.png"]];
5) To change image :
[mySprite setSpriteFrame:[CCSpriteFrame frameWithImageNamed: #"monster_02.png"]];
This works perfectly with cocos2d v3.
I spent 6 hours to have this process. Sometimes I feel stupid.
You can do it by using this
CCSpriteFrameCache and after that you can change you sprite by using function setSpriteFrame of ccsprite.

cpCCSprite moving out of Window Containment in cocos 2d Spacemanager framework

I'm new to Cocos2D - spacemanager framework, and stuck at a problem.
I have created a window containment for the gravity using below code..
[spaceManager addWindowContainmentWithFriction:1.0 elasticity:1.0 inset:cpvzero];
Now I created a cpCCSprite object and added it to the CCScene.
I am trying to applyImpulse to this cpCCSprite object when a touch is detected near the sprite.
[sprite applyImpulse:cpvmult(vector, magnitude)];
This works fine when the magnitude is less, but if it is a high value say 1000 or 1500,
the sprite moves out of the window containment added and is disappearing from the screen for the detected touch.
I am using cocos v2.x with spacemanager framework v0.3.0.
Please help me in fixing this.

Cocos2d CCLayerColor and CCLayerGradient alpha not working under iOS6

I just upgraded to Xcode 4.5 / iOS6, and my Cocos2d game now has an issue with transparency on CCLayerColor and CCLayerGradient. Layers created with these subclasses appear to be all-white and opaque, when in fact they should be white with transparency.
ccColor4B topStartColor = ccc4(255, 255, 255, 150);
ccColor4B topEndColor = ccc4(255, 255, 255, 100);
CGPoint topVector = ccp(0, 1);
_topGradient = [CCLayerGradient layerWithColor:topStartColor
fadingTo:topEndColor
alongVector:topVector];
I am on Cocos2d 2.0 Beta2. I did have to rework my AppDelegate to deal with the screen rotation issues caused by iOS6, so it is possible I may have inadvertently forgotten to set something up correctly - though I have combed through it pretty carefully. I should add that sprites with alpha are working fine - it seems to be CCLayerColor and CCLayerGradient only. I tested CCLayerColor in a stock Cocos2d 2.0 project, and it seems to work correctly there, so it's something in my app - but I cannot figure out what's different about my project.
This was in fact a bug in Cocos2d 2.0 Beta 2, and has been fixed in the development branch.

reveal the contents of a layer through a rectangle "cut-out" with cocos2d?

I am looking for a way to mask out a layer and reveal the contents of the layer through a rectangle (actually I want multiple rectangles to reveal the underlying layer's content).
I came across this similar question:
Cocos2d iPhone - Sprite cliping/mask/frame
Which had a solution:
http://www.learn-cocos2d.com/2011/01/cocos2d-gem-clippingnode/
However, when trying to use this class, I get the warning: "implicit declaration of function 'glPushMatrix' is invalid in C99"... I also get the error: "Property 'deviceOrientation' not found on object of type 'CCDirector *'"
What do I need to do to get this to work with the latest version of cocos2d?
... In any event, I commented out the deviceOrientation stuff, just to test if it will even work, and it doesn't seem to be.
I've got a CCBatchNode:
sheet = [CCSpriteBatchNode batchNodeWithFile:#"bg.png" capacity:500];
Then I add many sprites to that
[sheet addChild:sprite1];
[sheet addChild:sprite2];
[sheet addChild:sprite3];
Then I make the clipping node layer
ClippingNode *clipNode = [ClippingNode node];
clipNode.clippingRegion = CGRectMake(50, 50, 200, 200);
Then I add the sprite sheet and the clipNode:
[layer addChild:sheet];
[layer addChild:clipNode];
Then I add that to the CCSprite object
[self addChild:layer];
...
The result is, I see my many sprites from the sheet, but there is no clipping mask.. And my console shows a million: "OpenGL error 0x0502 in -[CCTextureAtlas drawNumberOfQuads:fromIndex:] 556"
so..... I am not sure what I am doing wrong-- or if this all has to do with the openGL warnings and device orientation errors... ?
UPDATE: I added #include <OpenGLES/ES1/gl.h> to the ClippingNode.m, and it got rid of the glpush/pop warnings.. But still results in the same OpenGL error once I add the clipNode child to the layer...
You can do it with shaders in cocos2d 2.0. Have a look at this tutorial.
http://www.raywenderlich.com/4428/how-to-mask-a-sprite-with-cocos2d-2-0