Sunday, January 23, 2011

Game Developers Resources for Cocos2D, Box2D, Chipmunk

Have you just stepped into the game developers community? I think you are looking for some helping resource, article, tools. You may have already googled about it I think. I am providing some links here which may be helpful.

1. Cocos2D Forum : You may find plenty of problems and their solution here.
2. Ray Winderlech : I think you should spend more time in this site. This is the boss site for iPhone newbie game developers. Plenty of sample games and plenty of interesting things to learn from this site. Thanks to Ray Winderlech - the author of this site.
3. StackOverFlow : Must register here. If you face any problem just post it here. You may also many problem's solution from here.
4. Box2D : Must study this site. Here you will find in depth details of box2D which is essential part of Cocos2D for implementing physics and other motions.

Make Jumping Sprite/Parabolic Jump/Projectile Movement of Sprite

If you want to make your Sprite move in a parabolic path or projectile path then you are in right place. I am going to explain how to do it simply.

You can do it using CCJumpBy action.

Let your sprite position is (0,0), You want to jump it to position (100,0) with a maximum height of 50.

id jump = [CCJumpBy actionWithDuration:1 position:ccp(100, 0) 
height:50 jumps:1];
[sprite runAction:jump];

Let me explain now-

actionWithDuration : 1 - is the time required to reach the destination or simply your action time.
postion : ccp(x,y) - is the position you want to see your sprite at the end. In our case it is (100,0).
height : h - is the maximum height it will reach during jump. In our case it is 50.
jumps : 1 - it is the number of jumps it will do to reach the destination. For a projectile or parabolic path it is tend to be 1.

You may play around with it to see the results and effects.

How to Make App Does not run in the Background in ios4

In ios3 or lower version making app does not run in the background is easy as there exists "UIApplication does not run in the background" key in the info.plist. But in ios4 it is not available by default. You will have to add it manually.
If you don't want your program to run in the background then do the following things-

- Go to your info.plist in the resource folder of your app and open it with a text editor.
- Now Add this

<key>UIApplicationExitsOnSuspend</key>
<true>

- Save, close and run the app. Your application will not run in the background anymore.

Friday, January 21, 2011

Disabling Multi Touch in Cocos2D

If you have already experienced developing iPhone Apps, then I think you know how to disable multi touch. It is very simple, using Interface Builder.

In Cocos2D as there is no interface builder, you have to do it on your own in code. It may frustrate you if you don't know the method. It is quite simple though.

- (BOOL)ccTouchesBegan:(NSSet *)touches
 withEvent:(UIEvent *)event {
       NSSet *allTouches = [event allTouches];
       if(allTouches>1)
            return;
       //else your rest of the code  
}

Quite simple isn't it? You can use it in CCTouchesMoved or CCTouchesEnded also.

Change Sprite Image

You can Change the image of the sprite very easily. Let your Sprite be "sprite" and image be "myImage.png". I am using Cocos2D Version 0.99.0.

[sprite setTexture:[[CCTextureCache sharedTextureCache] 
addImage:@"myImage.png"]];
It worked for me.

Saturday, January 15, 2011

Want to make games in iPhone?

If you are familiar with making iPhone apps and not yet developed any iPhone games, you are probably wondering how to make such wonderful, addictive iPhone games you see in your iPods or iPhone. Don't worry. I will show you the way.

Step 1- You can't probably make any games with such animations, motions, movements, physics logic and others those are essential for iPhone Games from a scratch. So you will need some help or some pre coded things to handle those motions, physics, movements and others. These are normally called framework/game engine what ever you say. You will need it, otherwise it will be years project to make a simple game.
Cocos2D ,Box2D and Chipmunk are such framework.

Step 2- Download Cocos 2D framework from Latest Cocos2D . I have developed games using Cocos 2D version 0.99.0. You can download it also from Version 0.99.0 .

Step 3 - After download unzip it. Then it is needed to install.

Tuesday, January 11, 2011

Passing a parameter with Selector in Cocos2D or Box2D

Hope you know how to pass the parameter in selector in normal iPhone App. If you don't know it no problem, I am reminding it here once again.

[self performSelectorInBackground:@selector(startParsing)
 withObject:OBJECTNAME];

Here the object you are passing is OBJECTNAME.

In Cocos2D  or Box2D framework you have similar type of interface/class to call a function by selector. The interfaces are-
- CallFunc
- CallFuncN
- CallFuncND

I will not describe these all as the topic is not this. I will say what you will have to do in order to pass the parameter from a selector function.

You will have to use CallFuncND.
Suppose you want to pass an Array named myArray

If you can see CallFuncND has a init function like these one below...

-(id) initWithTarget : (id)t selector : (SEL)s 
data : (void*)d;

Here you will have to pass data as a parameter. You can see that it's data type is void pointer. So you have to pass the object as a void pointer parameter. Don't worry about passing Array. We will solve it here. Just pass the object in data.

Suppose your array is array.

Then init your CallFuncND as below...

[CallFuncND initWithTarget : self 
selector : @selector(myTest : data:) data : array ];

array is passing as a void pointer.
Now create your selector callback function as

-(void)myTest : (id) sender data:(void*)myData{
NSMutableArray *myArray=(NSMutableArray*)myData;
} 

Here you have to type caste the void pointer array to NSMutableArray. You have successfully passed the pointer of the object whether is void or anything it does not matter. If you what the object really is then you may type cast it into anything. Then you may use that object as you desired. Here I have shown it with an array. It can be done for any object as well.