Saturday, July 14, 2012

Problem in Landscape Orientation iPad

I have faced this problem very often. When you intend to make your project work only in Landscape mode specially in iPad, you will see that even you have put the code -

- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation
{
  //Return YES for supported orientations
    return NO;
}
 
It is showing weird. It is just showing your project in Portrait. Even if you have put the project settings just for LandscapeLeft and LandscapeRight. Your app is just not working in Landscape!

To get rid of this problem you should follow the following code -

- (BOOL)shouldAutorotateToInterfaceOrientation:
(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return UIInterfaceOrientationIsLandscape(interfaceOrientation);

}



Thats it! Then your code will work perfectly in Landscape mode only both in Right and Left!

Enjoy!

Sunday, February 06, 2011

Box 2D Object runAction/Animate

You can not run any action or animation on a box2D object directly. You have to access the box2D objects userdata (i e CCSprite). Then run the action or animation code on this sprite. This is the code to access the box2D objects sprite-

for(b2Body *b = _world->GetBodyList(); b; b=b->GetNext()) {    
   if (b->GetUserData() != NULL) { 
     CCSprite *sprite = (CCSprite *)b->GetUserData();
     if(sprite.tag==aNumber)
     {
     //Do the action or animation code 
     }
    }
}

Here _world = Your Box2d World
        aNumber = The tag you must set to identify the box2d object when you create them

Adding iAd and AdMob in your Cocos 2D Game

iAd is allowed in ios4.0 or later versions. If you want both iAd and AdMob to run on your game depending on ios. If ios < 4 then show AdMob else show iAd.

Go to this link-
http://www.cocos2d-iphone.org/forum/topic/8424

Here you will find the best example how to do it.

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.