简体   繁体   中英

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. eg

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

How how to add child from different paths?

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

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, ie

+(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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM