Cocos2d-iphone compatibility with iphone/ipad issues

Source: Internet
Author: User

We know that in iOS development, to be compatible with Iphone/ipad, as long as the development of the version is set to Universal version, but the settings are relatively simple, and the size and location of the material inside is more difficult to control. The reason is that the ratio of iphone to ipad is not uniform, the iphone is (480*320/960*640) 3:2 screen, and the ipad screen is (1024*768,new ipad is 2048*1536) 4:3 screen.
There is a very important way to Cocos2d-iphone in the CCP, this method is used to create cgpoint. To minimize the resources for iOS games, we developed the universal version to share a set of resources. Because Gaoqing (retina960*640) in iOS is twice times larger than the normal version (480*320). To get closer to reality, we use the retina version footage from the iphone in the ipad. Now let's introduce our ideas:
1, the expansion of the CCP method
Because in the pre-development process did not consider the universal version, in 2.0 to expand, so the previous position is a lot of the CCP, the location of the iphone version, some are written numbers, so to this method to expand. We have defined another method, FCCCF, that can be returned to the real location based on the system's device, and the incoming parameter is the x, Y axis on the iphone.
#define ISPAD (ui_user_interface_idiom () = = Uiuserinterfaceidiompad)//define whether the ipad is a macro
#define FCCCF (__x__,__y__) Getpointbyphonexandy (__x__,__y__)//To be consistent with CCP usage
/*
* Incoming iphone location
*/
Cg_inline cgpoint Getpointbyphonexandy (cgfloat x,cgfloat y) {
Cgpoint p;
if (Ispad) {
P=CCP ((x+16) * *, (Y+32) * *);
P=CCP (x/0.46875, y/0.41667);
}
else{
P=CCP (x, y);
}
return p;
}
2. Rewrite some base class methods in Cocos2d-iphone
We know that in Cocos2d-iphone, if you pass in the normal footage path and support the retina version, Cocos2d-iphone will automatically recognize the retina version footage, with the principle that (1) The png,jpg image is added @2x before the suffix, such as Icon.png and[email protected], (2) for plist files and Pvr.ccz files (image compression processing) files are added "-hd", such as Keybutton.plist and Keybutton-hd.plist. Trace to We can find in fact in the organization resource Real, cocos2d-iphone the resource filename has been processed again, based on this idea. We can also work with file names on the ipad.
The main base classes in Cocos2d-iphone are Ccsprite and cclayer, we rewrite a base class named Fcbasesprite:ccsprite, rewrite + (ID) spritewithfile: (NSString *) FileName, + (ID) spritewithspriteframename: (nsstring*) Spriteframename methods. Another way to extend a method is for some background nature of the sprite may need to pull up, and then the above two methods overloaded. Then Fcbasesprite is defined as follows:
@interface Fcbasesprite:ccsprite {

}
+ (ID) spritewithfile: (NSString *) filename stretch: (bool) Isstretch;
+ (ID) spritewithspriteframename: (NSString *) Spriteframename stretch: (bool) Isstretch;
@end
The implementations were:
/*
*stretch whether the ipad version is deformed
*/
+ (ID) spritewithfile: (NSString *) filename stretch: (bool) Isstretch {
Filename=[filename Getdoubleimagefilename];
ccnode* n= [Super Spritewithfile:filename];//[[[self alloc] initwithfile:filename] autorelease];
if (Isstretch&&ispad) {
n.scalex=1024.0/960;
n.scaley=768.0/640;
}
return n;
}
/*
*stretch whether the ipad version is deformed
*/
+ (ID) spritewithspriteframename: (NSString *) Spriteframename stretch: (BOOL) Isstretch
{
Ccspriteframe *frame = [[Fcbaseccspriteframecache Sharedspriteframecache] spriteframebyname:spriteframename];

NSAssert1 (Frame!=nil, @ "Invalid spriteframename:%@", spriteframename);

Ccnode *n=[super Spritewithspriteframe:frame];
if (Ispad && isstretch) {
n.scalex=1024.0/960;
n.scaley=768.0/640;
}
return n;
}
The Getdoubleimagefilename method is an extension of our approach to the NSString type, that is, to get the real resource file name
Here's how:
#define RETINA2X @ "@2x"
#define RETINAHD @ "-HD"

-(nsstring*) Getdoubleimagefilename {
if (Ispad) {
nsstring* filenamenoext= [self stringbydeletingpathextension];
nsstring* extension = [self pathextension];

NSString *extension = [path pathextension];

if (![ Filenamenoext hassuffix:retina2x]) {
if ([self retaincount]>0) {
[Self release];
//            }
Self = [NSString stringwithformat:@ "%@%@.%@", filenamenoext,retina2x,extension];
NSLOG (@ "New plist=%@", *plist_p);
}
}
return self;
}

The above is a part of my development process, please the prawn criticism pointed out.

Cocos2d-iphone compatibility with iphone/ipad issues

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.