Archive for the ‘Games Development’ Category

Using LevelHelper without Box2d for Cocos2d-X

Thursday, November 8th, 2012

I have been trying out LevelHelper recently and it has been very useful when making games that have a fair bit of physics.

I was looking at making a really simple game that did not require physics. Thats where LevelHelper seemed to fall over. Since the editor generates code for you and that code has reference to Box2D.

Fear not for all is not lost, you can still use LevelHelper without implementing the physics. You still need use the Cocos2D-X with Box2D template in xCode. You just don’t attach the physics properties.

So follow the tutorials on the LevelHelper site. When you get to the bellow line of code in the second video:

[cpp]
//attaches the physics properties and the sprites to the view.
loader->addObjectsToWorld(world, this);[/cpp]

The first parameter “world” is used to attach the physics properties. The second parameter “this” is what all the image assets in LevelHelper level are attached to. All you need to do is change the above line to:

[cpp]
//only attaches the sprites to the view.
loader->addObjectsToWorld(NULL, this);[/cpp]

Now just ignore any code to do with physics and Box2D.

Implementing CCCallFunc Cocos2d-X 2.0

Thursday, August 2nd, 2012

This is a simple way of implementing CCCallFunc.

ClassA wants to pass a function reference to ClassB.

CCCallFunc calls a function without any parameters. If you want to get the CCObject that called it then use CCCallFuncN and if you want to pass your own data use CCCallFuncND

In ClassB header create a variable to hold the callFunction.

[cpp]
//ClassB.h
public:
CCCallFunc callFuncHolder;
[/cpp]

In ClassB implementation call the passed in function.
[cpp]
//ClassB.cpp

void someMethod()
{
//this calls the passed in method if there is no set method it will not error
callFuncHolder.execute();
}

[/cpp]

In class ClassA implementation.

[cpp]
//ClassA.cpp
void init()
{
ClassB *cb = new ClassB();
cb->callFuncHolder = *CCCallFunc::create(this, callfunc_selector(methodPassed));

//when ClassB is ready it calls methodPassed
}

//this is the method passed into the CCCallFunc
void methodPassed()
{
CCLog(“methodPassed call form else where”);
}
[/cpp]

Creating Frame Animation in Cocos2d-X 2.0 Using a Sprite Sheet

Saturday, July 28th, 2012

Since the Cocos2d-X 2.0 rc2 a lot of the initialisation has changed so this code works in Cocos2d-X 2.0.

First create a sprite sheet so we can load it in.

Once you have created the Sprite Sheet you will end up with a image file and plist.

mySprites.png
mySprites.plist

I like to name my sprites like this:

jumpAnimation_00
jumpAnimation_01
jumpAnimation_02
jumpAnimation_03
jumpAnimation_04
jumpAnimation_05

walkAnimations_00
walkAnimations_01
walkAnimations_02
walkAnimations_03
walkAnimations_04
walkAnimations_05

It makes creating the animations easier. My code below is depended on the above naming convention.

[cpp]

void creatAnimation()
{
//load plist and it will load in the png with the same name
//the frames will be stored in the CCSpriteFrameCache so you can get the frames at you leisure
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(“mySprites.plist”);

//the frames will be gathered in the animation object
CCAnimation* jumpAnim = animationName(“jumpAnimations”);
jumpAnim->setDelayPerUnit(0.2);

//create sprite first frame from animation first frame
CCSprite *jumpSprite = CCSprite::create((CCSpriteFrame*) jumpAnim->getFrames()->objectAtIndex(0));

CCAction *jumpAct = CCRepeatForever::create(jumpAnim);
jumpSprite->runAction(jumpAct);
this->addChild(spriteAnim);

}
//this method loops through the CCSpriteFrameCache to get the
CCAnimation *animationName(char const *name)
{
//creates a temp animation object
CCAnimation* holderAnim = CCAnimation::create();

//variables used for the loop
string fName;
string tName = name;
CCSpriteFrame* pFrame;
int frameCount = 1;

//loops till it can’t find any more frames
do
{

//adds the underscore and a zero if the image is less than 10
fName = (frameCount < 10) ? tName + "_0" : tName + "_"; //adds the image number---Utils::num2str() converts an integer to a string fName = fName + Utils::num2str(frameCount); frameCount++; //tries to get the frame //tries to get the frame pFrame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fName.c_str());

//adds the frame to the animation holder
if (pFrame)
holderAnim->addSpriteFrame(pFrame);

//continues too loop till the next frame is null
} while(pFrame != NULL);

return holderAnim;
}

[/cpp]

Create Cocos2d-X 2.0 Sprite Sheet

Sunday, July 22nd, 2012

Cocos2dx is the C++ implementation of Cocos2d. Even though it is C++ and cross platform it still uses plists. There are probably a lot of tools out there that can help you create these sprite sheets. I am currently using SpriteHelper which has export settings specifically targeting Cocos2D-X. I used to use Texture Packer for Cocos2D because it has a nice PVR viewer.

The main reason i am using SpriteHelper is because of Level Helper. Level Helper looks like it has a lot of potential.

cocos2d: WARNING: format is not supported for CCSpriteFrameCache

Monday, September 13th, 2010

I have been working away at same iPhone game development.

I found a nice sprite sheet editor zwoptex: http://zwoptexapp.com which is compatible with the engine I was using cocos2d: http://code.google.com/p/cocos2d-iphone/

Little did I know there was some version issues between the exporter and the engine. Zwoptex exports a png and a plist that contains all the meta-data for the sprite sheet. If you are getting the bellow mentioned error you might be reading online various suggestions like installing older versions of zwoptex or a new versions of the  engine.

Solution:

As it turns out all you have to do is un-check  rotate from inside zwoptex publish the files. Edit the plist generated by zowptex and change the root>>metadata>>format number from 2 to 1 save clean your build recompile and your done.

cocos2d error:

ocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:

Full Error:

Terminating app due to uncaught exception ‘NSInternalInconsistencyException’, reason: ‘cocos2d: WARNING: format is not supported for CCSpriteFrameCache addSpriteFramesWithDictionary:texture:’
2010-09-13 16:01:36.353 TileMap[12915:40b] Stack: (
46889040,
48046892,
46626571,
4768532,
450434,
452386,
10372,
298606,
9808,
8936,
8317326,
8321359,
8346942,
8328439,
8360408,
57418108,
46168220,
46164136,
8319521,
8352626
)