Tag Archives: cocos2d

How to move a body manually in Box2D? or Give a force to a body in Box2D, iPhone.

By | April 28, 2012

This is a sample code to move a body in Box2D . First you have to make a body with variable name “moving_rec” and call the below function in a schedular at regular intervals. -(void) moveBody{ b2Vec2 force = b2Vec2(0,0); force = b2Vec2(0,3); //Giving the x an y to negative will move the body in… Read More »

Pause And Resume iPhone Game Using Cocos2D

By | April 26, 2012

More than usually we need to apply the functions of Pause and Resume/Play for our games. In Mac’s cocos2D programming it’s more than simple. Simply use the following code for Pause & Resume where ever you want! /*For Pausing the game*/ [[CCDirector sharedDirector] pause]; [self pauseSchedulerAndActions]; //Call for pausing all schedulers and actions /*For Resuming/Playing… Read More »

How to get Device Orientation in Cocos2D, iPhone in Objective C?

By | April 17, 2012

Hello all…… We may need to know that what is your current device orientation. Using the below code you can do this. Note: If you have to explicitly set the orientation of your phone to any of the above you can set this in the appDelegate.m file. You can put an “||” (OR) symbol in… Read More »

Sprite Animations Without Sprite Sheets

By | April 7, 2012

You can simulate any animations if you have a sequence of images which makes the illusion of that animations and rendering those images to the screen. In cocos2D it can be done in two ways, with sprite sheet animation and without sprite sheets. Here we are going to see how it can be done with… Read More »

Sprite Animations Without Sprite Sheets

By | April 7, 2012

You can simulate any animations if you have a sequence of images which makes the illusion of that animations and rendering those images to the screen. In cocos2D it can be done in two ways, with sprite sheet animation and without sprite sheets. Here we are going to see how it can be done with… Read More »

Sprite Animations Without Sprite Sheets

By | April 7, 2012

You can simulate any animations if you have a sequence of images which makes the illusion of that animations and rendering those images to the screen. In cocos2D it can be done in two ways, with sprite sheet animation and without sprite sheets. Here we are going to see how it can be done with… Read More »

Sprite Animations Without Sprite Sheets

By | April 7, 2012

You can simulate any animations if you have a sequence of images which makes the illusion of that animations and rendering those images to the screen. In cocos2D it can be done in two ways, with sprite sheet animation and without sprite sheets. Here we are going to see how it can be done with… Read More »

Show Alert in cocos2D Android.

By | February 4, 2012

Here is a simple example showing alert in android cocos2D. For this I am using an example from my previous android cocos2D tutorial.. So please read that tutorial before reading this because the I am using the classes and files from it. I am only changing the Gamelayer.java file to show an Alert Dialog when… Read More »

How to detect shake in iPhone ? An example

By | April 13, 2011

Hi, Sometimes you may want to detect shake gesture in iPhone using your programs. But how can you detect shake gesture in iPhone? Shake gesture in iPhone can be detected by using the accelerometer. The following lines of code can be used for detecting the shake gesture in iPhone. You can use these lines of… Read More »

How to remove box2D body from world ?

By | April 10, 2011

Hi, If you are a game developer and you are using box2D for real world simulations, you would probably need a particular body to be removed from the box2D world! But how can you remove box2D body from world ? See the following sample code which does the same. – (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {… Read More »

How to make a sprite visible and invisible in Cocos2D ?

By | March 27, 2011

Hi, In certain situations you may need to make visible or invisible certain sprites. But how to set a sprite visible and invisible in Cocos2D? See the following Cocos2D code. //Set the sprite’s visible value to 1 to make it visible yourSprite.visible = 1; //Set the sprite’s visible value to 0 to make it invisible… Read More »

How to change the Z value of a Sprite during an action in Cocos2D ?

By | March 27, 2011

Hi, There may arise some situations where you need to change the z value of a particular sprite during an action in Cocos2D. So how can you do that? See the following lines of code. CCSprite *sample= [CCSprite spriteWithFile:@”trialImage.png”]; sample.position = ccp(160,240); [self addChild:sample z:1]; /* Some of your actions goes here! */ [self reorderChild:sample… Read More »

How to stop the sprite sheet animation in cocos2D ?

By | March 17, 2011

Hi, In one of our previous post we have discussed how to animate sprite sheet in cocos2D. In some cases you may need to stop the sprite sheet animation abruptly. How to do that? Here the way to do that. Use the following. [spriteName stopAction:spriteNameAction]; where spriteName is the name of your sprite & spriteNameAction… Read More »

Copying a Sprite Image to another Sprite in Cocos2D

By | March 11, 2011

Hi, Suppose you want to copy a sprite image to another new sprite while using cocos2D. What you do? See the following code… Let oldSprite be your first sprite CCSprite *oldSprite = [CCSprite spriteWithFile:@”ImgOne.png”]; And newSprite be your second sprite CCSprite *newSprite = [CCSprite spriteWithTexture:[oldSprite texture]]; That’s it! Done! Now newSprite too contain ImgOne as… Read More »

Collision Detection In Box2D – Using With Cocos2D

By | March 5, 2011

For an interactive game building using Box2D, collision detection of Box2D bodies is necessary. There is an easy way to implement collision detection in Box2D. For checking whether two bodies have collided with each other in Box2D while using it with cocos2D, then use the following lines of code where ever you want to check… Read More »

Removing Unused Textures in Cocos2D

By | March 5, 2011

Sometimes unused textures may cause you memory leak! So you need to remove the unused textures. For removing unused textures, use the following line of code, where ever you want to remove unused textures. [[TextureMgr sharedTextureMgr] removeUnusedTextures]; 🙂

Removing Forces From Your World in Box2D Cocos2D

By | March 5, 2011

Hi, In certain situations you may need to remove all the forces you have applied on different bodies in your box2D world. Then what to do? Removing all the forces from the bodies in the box2D world is simple. Just use the single line of code. yourWorld->ClearForces(); Here yourWorld is your Box2D world. Simple, isn’t… Read More »