cocos2d-js 3.0 螢幕適配方案 解析度適應

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   使用   strong   

首先介紹一個api和相應的參數:

cc.view.setDesignResolutionSize(1024, 768, cc.ResolutionPolicy.FIXED_WIDTH);

這裡設定遊戲製作的目尺規寸和顯示的模式。

模式包括:

cc.ResolutionPolicy = {    // The entire application is visible in the specified area without trying to preserve the original aspect ratio.    // Distortion can occur, and the application may appear stretched or compressed.    EXACT_FIT:0,    // The entire application fills the specified area, without distortion but possibly with some cropping,    // while maintaining the original aspect ratio of the application.    NO_BORDER:1,    // The entire application is visible in the specified area without distortion while maintaining the original    // aspect ratio of the application. Borders can appear on two sides of the application.    SHOW_ALL:2,    // The application takes the height of the design resolution size and modifies the width of the internal    // canvas so that it fits the aspect ratio of the device    // no distortion will occur however you must make sure your application works on different    // aspect ratios    FIXED_HEIGHT:3,    // The application takes the width of the design resolution size and modifies the height of the internal    // canvas so that it fits the aspect ratio of the device    // no distortion will occur however you must make sure your application works on different    // aspect ratios    FIXED_WIDTH:4,    UNKNOWN:5};

參考官方說明: http://www.cocos2d-x.org/wiki/Multi_resolution_support

EXACT_FIT會展開遊戲,充滿整個螢幕,最簡單最粗暴;
SHOW_ALL保持遊戲原比例,讓一邊佔滿螢幕,另外一側黑邊;
NO_BORDER跟SHOW_ALL類似,但讓短邊佔滿螢幕,另外一側超出螢幕,不顯示黑邊,一部分畫面在螢幕外,無法顯示;
FIXED_WIDTH和FIXED_HEIGHT都是NO_BORDER的升級版,指定那一側充滿螢幕,另外一側超出螢幕。

這裡建議使用FIXED_WIDTH和FIXED_HEIGHT,其他用法請參考:

http://www.cocoachina.com/game/20130513/6180.htmlhttp://www.cocoachina.com/cocos/20140516/8451.html這兩個方案適合UI沿著螢幕邊緣布局,而遊戲畫面置中,遊戲背景則可以裁剪(顯示一部分)的情況。  通過上邊的文章,我們知道winSize,visibleSize,designSize(自己的設計尺寸)。那麼在這兩個方案內,winSize和visibleSize是一致的,cc.director.getWinSize()和cc.director.getVisibleSize()擷取到一樣的資料。 以FIXED_WIDTH為例版面配置階段中,橫向就按照設計稿直接寫死絕對座標值都可以,因為cc.director.getVisibleSize().width就是我們的設計寬度,cocos2d通過縮放會讓橫向剛好佔滿螢幕;而縱向,就利用cc.director.getVisibleSize().height來布局。 y=0表示剛好在螢幕邊緣,在FIXED_WIDTH方案中,不像NO_BORDER會有visibleOrigin,這裡不需要考慮這個值,因為總是0,cocos2d自動把y=0放到這個visibleOrigin位置了。而螢幕上方則使用cc.director.getVisibleSize().height - 20類似的方式來布局。這裡的20也會隨著整個畫面的壓縮比例而變小。 例如設計寬高為1024*768,但實際放到725*225的地區運行,那麼cc.director.getVisibleSize()擷取到的是(1024, 315)左右。實際上這個尺寸並不是真實螢幕尺寸,但可以按照這個數值進行布局。  螢幕尺寸另外cc.view.getFrameSize可以擷取螢幕尺寸

cocos2d-js 3.0 螢幕適配方案 解析度適應

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.