Cocos2d 3.0 screen adaptation, cocos2d3.0

Source: Internet
Author: User

Cocos2d 3.0 screen adaptation, cocos2d3.0
1. Solution

The solution is provided directly, and then explained slowly. Of course, this solution is not completely perfect.


// For a horizontal screen game: glview-> setDesignResolutionSize (960,640, ResolutionPolicy: FIXED_HEIGHT); // auto visibleSize = Director: getInstance () displayed on a 960x640 background () -> getVisibleSize (); auto Bg = Sprite: create ("Bg.png"); this-> addChild (Bg); Bg-> setPosition (visibleSize. width/2, visibleSize. height/2); // Bg-> setPosition (visibleSize. width/2,320); // equivalent to the Bg-> setScaleX (visibleSize. width/960); // a Sprite auto Spr = Sprite: create ("test.png"); this-> addChild (Spr); Spr-> setPosition (visibleSize. width-50, visibleSize. height-50); // visibleSize because it is a fixed height. the height is always 640, so the following statement is also acceptable // Spr-> setPosition (visibleSize. width-50,590); // for a portrait game: glview-> setDesignResolutionSize (640,960, ResolutionPolicy: FIXED_WIDTH );

All art plots follow the 960x640 specifications.


2. Start testing various adaptation methods


The screen adaptation should actually start with Opengles. I skipped it here. If you are interested, read this article to learn about 2D Development Using OpenGL ES in Android (drawing the first triangle outside the article)"

Glview-> setDesignResolutionSize (960,640); the third parameter is ignored first. What does setDesignResolutionSize mean? Is to setFixed AreaProgram layout and art design. Then, you can use Opengles to project to a specific mobile phone screen, regardless of the specific mobile phone screen size. For example, if the screen resolution of a mobile phone is 480x320, Opengles automatically doubles the screen resolution for projection. The mobile phone screen resolution is 1920x1280, and Opengles will automatically zoom in twice for projection. There is no need to change both the program and the Art: a set of program coordinates and a set of art resources.

For example, for a background image, you only need a 960x640 image and it is OK to put it in the center of (480,320.

A genie assumes that it is to be placed in the upper right corner, and put it in the position (960,640. You do not need to consider visibleSize or anything in cocos2d.


In reality, the resolution of a mobile phone cannot be a multiple of 960x640. And we need to display it in full screen, so we have the third parameter to specify the specific projection method.

There are currently five resolutionpolicies for cocos2d:

enum class ResolutionPolicy{    EXACT_FIT,    NO_BORDER,    SHOW_ALL,    FIXED_HEIGHT,    FIXED_WIDTH};

2.1 EXACT_FIT


EXACT_FIT is completely projected to the full screen. This is the perfect thing for programs and fine arts, but it will be stretched.



The above is the original image of 960x540. I have added a reference object to the four corners. The total figure is an image.

Assume that the resolution of the target mobile phone is 1136x500 (the actual resolution does not exist). Using EXACT_FIT is the following effect:



In fact, it is acceptable to stretch the background. The main problem is that the genie will also be stretched, and the stretching area is uncontrollable.

EXACT_FIT: applicable to small personal projects. A slight stretch does not affect the displayed game. However, large projects in the company are basically unavailable.

When using EXACT_FIT, visibleSize is always 960x640 regardless of the Resolution of the mobile phone.


2.2 SHOW_ALL

First explain Show_All. It will not stretch, but there will be black edges on the left or right or up and down, without full screen.

1136x640


1024x768


Can Show_ALL be used? Almost unusable. Although its visibleSize is also 960x640. However, the Black edge cannot be used by Apple. Black borders here cannot be covered with things.


2.3 NO_BORDER

As the name implies, NO_BORDER does not have a Black edge and maintains the aspect ratio. The result is that some of them are cropped out.

1136x640


When the resolution of the mobile phone is relatively long, it is not enough to go up or down, and will be cut off. Pay attention to the references under the background.

VisibleSize width: 960
VisibleSize height: 540.8450927734375 // less than 640


1024x768


When the resolution of the IPad is relatively high, it is not enough and will be cut off. Pay attention to the reference on the left of the background.

VisibleSize width: 853.3333129882812 // less than 960
VisibleSize height: 640


The use of NO_BORDER will cause constant changes to visible and will be dropped.


2.4 FIXED_HEIGHT

FIXED_HEIGHT is similar to FIXED_WIDTH. FIXED_HEIGHT and FIXED_WIDTH are available only in cocos2d. This solution is fixed in one direction and has a fixed proportion. This will lead to partial coverage or partial removal, and dynamic adaptation of the program is required.

1136x640

Figure 7


VisibleSize width: 1136 // changes according to the cell phone screen resolution

VisibleSize height: 640 // The value is always 640


1024x768

VisibleSize width: 854 // changes according to the cell phone screen resolution

VisibleSize height: 640 // The value is always 640


Figure 7 although the effect here seems to be the same as that of SHOW_ALL, it is completely different. You can see the visibleSize. The Black edge here can be covered by manually stretching the background. Someone asked, isn't the stretching background the same as EXACT_FIT? Different! EXACT_FIT is to stretch the entire area, and the genie placed in the scenario will also be stretched. FIXED_HEIGHT will not stretch the genie.

The place where the genie is placed is exquisite. in the vertical direction, you can use the devil's number. 320 must be vertically centered.Horizontal considerationFor example, visibleSize should be taken into account to put a genie in the upper right corner.

SetPosition (visibleSize. width-50,640-50 );

Below is the simple Effect of stretching the background, plus a genie.

var visibleSize = cc.Director.getInstance().getVisibleSize();        var scaleRate = visibleSize.width / 960;                var bg = cc.Sprite.create("res/bg.jpg");        this.addChild(bg);        bg.setPosition(visibleSize.width / 2, visibleSize.height / 2);        bg.setScaleX(scaleRate);        var sp = cc.Sprite.create("res/menu1.png");        this.addChild(sp);        sp.setPosition(100 * scaleRate,320);


1136x640

1024x768

Other Android resolutions are not listed.


3. Talking about resource adaptation


We know that it is very difficult for you to adapt a set of images to 960x640,204 8x1536 or an iPhone 6 Plus 1920x1080 in the future. The main problem is that if you are using a set of 960x640 images, you will not be clear at high resolution. If you are using a set of X images, run the comparison card on a low-resolution mobile phone. In the above example, the 960x640 resolution is used as the design resolution. You can consider increasing the resolution of the mobile phone to 1248x832 in the future.


Another problem is the aspect ratio of the screen. The IPad is 1024/768, that is, the proportion of 1.33 is lower than 960/640 of 1.5, while the screen aspect ratio of iPhone 5 and iPhone 6 has increased to 1.775. Add the Android screen aspect ratio. We can consider using a ratio of 1.6 in the future. For example, 1248x780 is used as the design resolution. Then the IPad should be especially careful with special processing, horizontal, and individual UIS should be scaled out and adapted.


There are no best practices, or two sets of resources should be prepared, so the game package will be relatively large. At present, we usually prepare a set of low-resolution resources. In fact, it does not look too bad in terms of high resolution. Of course, if your game is an IPad game, you should consider it carefully.


3. You have prepared an exe to play.


You can download the program and debug it yourself. Http://www.waitingfy.com/archives/1304#3exe

Exe can be opened at \ runtime \ win32 \ PrebuiltRuntimeJs.exe Win7, but XP cannot.

In Config. json, init_cfg can be used to adjust the device resolution.

You can adjust the DesignResolutionSize and formula in main. js.

App. js can be used to change the background and other locations. The project is cocos2d js for convenient debugging, but the principle is the same.

More fun to understand.


Cocos2d-x screen Click Event

You need to reload the virtual void registerWithTouchDispatcher (); Function
Implementation
Director: getInstance ()-> getTouchDispatcher ()-> addTargetedDelegate (this, Menu: HANDLER_PRIORITY-1, true); I am 3.0
2. x is
CCDirector * pDirector = CCDirector: sharedDirector (); pDirector-> getTouchDispatcher ()-> addTargetedDelegate (this, 0, true );

Cocos2D Android adaptation

The Android app for resource adaptation is Apk, and Apk is also a Zip file. there are the following steps to process the resource file: 1) When Cocos2dxHelper is initialized, The ApkPath of the Java layer is passed to the native layer. 2) When CCFileUtils of native is initialized with javasfileutils, it will create its own derived class CCFileUtilsAndroid and unlock the apk to the temporary directory. when you need to load the resource file while running the program, go to the temporary directory to find 3) when the program exits, this temporary directory will be deleted 3) The main logic is completed in Cocos2dxRenderer, the most important thing is that Cocos2dxRenderer1) onSurfaceCreated nativeInit is called. All important Cocos2d objects (CCDirector, CCSence, HelloWorld, etc.) are initialized here. 2) onDrawFrame, it is to notify Cocos2d to draw a frame. For details, refer to how Cocos2D draws a frame. There are also input processing, cocos2dxRenderer is also called to transfer nativeInit from the java layer of Android to the native layer onSurfaceCreated of Cocos2d, that is, "opengl can be used for plotting, cocos2d you initialize it. "Cocos2d has done the following work: 1) set FrameSize2 Based on the screen size information provided by Android.) Create AppDelegate. Note that AppDelegate is the derived class of CCApplication. When AppDelegate run is called, it will call applicationDidFinishLaunching3) in applicationDidFinishLaunching, Director, Scene are created,

Related Article

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.