Faheem Rajput
Faheem Rajput

Reputation: 601

How to load different images depending on the iOS device in cocos2d?

I am working on a game in cocos2d: I want to load different images depending on the iOS device. One method I am already using is in the init method I check the device and then add the image in same ccsprite object.

But it is very lengthy and hard to manage. What I think I should do is create images for all devices with the same name and place them in different folders; at application start just check the device and set the path matching the iOS device. e.g

images/iphone/abc.png
images/iPad/abs.png

How how to add child from different paths?

Upvotes: 0

Views: 309

Answers (2)

Sebastián Castro
Sebastián Castro

Reputation: 1408

If you are using cocos2d 2.0 you have the -ipad suffix for this. In fact, you have -hd, -ipad and -ipadhd

Upvotes: 3

Eiko
Eiko

Reputation: 25632

You can select the image at runtime:

UIImage *image;

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
   image = // Load iPad image here
else
   image = // Load iPhone image here

This is of course a pain if you use it everywhere in your code, so you might just add a helper function, a category on UIImage or a macro for this, i.e.

+(UIImage *)deviceDependentImageWithName:(NSString *)name {
   return (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? /* iPadImage */ : /* iPhoneImage */;
}

The latter method works especially easy if you follow your naming rules like putting them in separate folders (make sure that these directories will exist in the distribution and are not just copied to the top level directory as by default). Adding "-iPhone" or "-iPad" might work equally well without worrying about directories.

Upvotes: 0

Related Questions