Cocod2d compatibility with iphone/ipad

Source: Internet
Author: User

In iOS development, you must be compatible with the iphone/ipad. setting Devices to Universal can automatically adapt to the iphone/ipad. This is relatively simple and the difficulty is the adaptation of materials. There are two problems with material Adaptation: material location and size

1. Material Location Problems

There are two methods for Location: absolute location and relative location. As follows:

Absolute position: ccp (48, 32 );

Relative Position: ccp (size. width * 10%, size. height * 10% );

For example, if the current device size is 480*320, the two locations are equal. If the device size is 960*640, the location material will be offset, the relative position is equal to the ccp (10%, 10%), regardless of how the device size changes, it is always in the position of the screen. Obviously, we need to use relative positions to adapt to different devices.

2. Material Size issues

There are two solutions to the problem of material size: one is to adapt multiple sets of materials to different devices, and the other is to adapt one set of materials to multiple devices.

1. multiple sets of materials adapt to different devices

In our iOS development, materials and images with different suffixes are loaded based on different device types. By default: iphone/iphone Retina (HD mode), ipad/ipad Ratina, they correspond to the following materials: Name/Name-hd and Name-ipad/Name-ipadhd.

This is by default. You can also set the suffix name, create a project, and open AppDelegate. m to find the following code:

CCFileUtils * sharedFileUtils = [CCFileUtils sharedFileUtils];

[SharedFileUtils setEnableFallbackSuffixes: NO]; // Default: NO. No fallback suffixes are going to be used

[SharedFileUtils setiPhoneRetinaDisplaySuffix: @ "-hd"]; // Default on iPhone RetinaDisplay is "-hd"

[SharedFileUtils setiPadSuffix: @ "-ipad"]; // Default on iPad is "ipad"

[SharedFileUtils setiPadRetinaDisplaySuffix: @ "-ipadhd"]; // Default on iPad RetinaDisplay is "-ipadhd"

Here you can set the suffix name of the corresponding clip.

2. A set of materials to adapt to multiple devices

In some cases, we need to use a set of materials to adapt to multiple devices, so we need to scale down the image proportionally to adapt to different devices:

CGSize size;

Size. width = 480;

Size. height = 320;

CGSize newsize = [[CCDirector shareddire] winSize];

Float sp_scaleX = newsize. width/size. width;

Float sp_scaleY = newsize. height/size. height;

CCSprite * sprite = [CCSprite spriteWithFile: @ "Icon.png"];

Sprite. scaleX = sp_scaleX;

Sprite. scaleY = sp_scaleY;

The above code first sets the original ratio to size, then obtains the current screen size newsize, calculates the ratio of x and y to sp_scaleX and sp_scaleY, and finally assigns the value to scaleX and scaleY of the clip.

 

The following describes how to obtain the device type:

We use macro UI_USER_INTERFACE_IDIOM () to obtain the device type, as shown below:

If (UI_USER_INTERFACE_IDIOM () = UIUserInterfaceIdiomPhone)

{

// Iphone

}

If (UI_USER_INTERFACE_IDIOM () = UIUserInterfaceIdiomPad)

{

// Ipad

}

 

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.