Cocos2d-x 背景图适应Android各种分辨率

  次阅读 来源:互联网(转载协议) 2015-09-27 15:01 我要评论(0)

使用方法:设计大小为320x480的时候; 例如一个Sprite在右下方,距离屏幕右方40像素,距离屏幕下方为30像素。则需要如下设置:sprite->SetScale( CCGetScaleX() ); // 应用放缩,仅仅X方向即可。sprite->setPosition( CCGetScreenPoint(-40.0f, 30.0f, CC_ALIGNMENT_RB) );那么在屏幕大小为480x800的时候,看到的效果是:sprite被放大了1.5倍数sprite在右下方,距离屏幕右方60像素,距离屏幕下方为50像素。以上一点小代码,希望对大家能有所帮助,如有建议欢迎指出。附源代码如下:#ifndef CCLayout_h__#define CCLayout_h__#include "cocos2d.h"USING_NS_CC;// design size.// you can change it yourself.#define CC_DESIGN_W (480.0f)#define CC_DESIGN_H (320.0f)inline float CCGetScaleX(){ CCSize winSize =CCDirector::sharedDirector()->getWinSize(); return winSize.width / CC_DESIGN_W;}inline float CCGetScaleY(){ CCSize winSize =CCDirector::sharedDirector()->getWinSize(); return winSize.height / CC_DESIGN_H;}inline float CCGetCenterX(){ CCSize winSize =CCDirector::sharedDirector()->getWinSize(); return winSize.width / 2.0f;}inline float CCGetCenterY(){ CCSize winSize =CCDirector::sharedDirector()->getWinSize(); return winSize.height / 2.0f;}inline CCPoint CCGetCenterXY(){ CCSize winSize =CCDirector::sharedDirector()->getWinSize(); return ccp(winSize.width / 2.0f, winSize.height / 2.0f);}enum CCAlignment{ CC_ALIGNMENT_LB = 0, // left bottom CC_ALIGNMENT_CB = 1, // center bottom CC_ALIGNMENT_RB = 2, // right bottom CC_ALIGNMENT_LC = 3, // left center CC_ALIGNMENT_CC = 4, // center center CC_ALIGNMENT_RC = 5, // right center CC_ALIGNMENT_LT = 6, // left top CC_ALIGNMENT_CT = 7, // center top CC_ALIGNMENT_RT = 8, // right top};inline CCPoint CCGetScreenPoint(float x, float y, CCAlignment alignment){ CCSize winSize =CCDirector::sharedDirector()->getWinSize(); const float scaleX = winSize.width / CC_DESIGN_W; const float scaleY = winSize.height / CC_DESIGN_H; CCPoint result; if (alignment % 3 == 0) { result.x = x * scaleX; } else if (alignment % 3 == 2) { result.x = winSize.width + x * scaleX; } else { result.x = winSize.width / 2.0f + x * scaleY; } if (alignment / 3 == 0) { result.y = y * scaleY; } else if (alignment / 3 == 2) { result.y = winSize.height + y * scaleY; } else { result.y = winSize.height / 2.0f + y * scaleY; } return result;}inline CCPoint CCGetScreenPoint(const CCPoint& point, CCAlignment alignment){ return CCGetScreenPoint(point.x, point.y, alignment);}#endif // CCLayout_h__ 本文转载于Cocos2d-x中文论坛http://cn.cocos2d-x.org/bbs/forum.php?mod=viewthread&tid=144&extra=page%3D1?

本站部分文章来源于网络以及网友投稿,本站只负责对文章进行整理、排版、编辑,是出于传递更多信息之目的,并不意味着赞同其观点或证实其内容的真实性。如果您有什么意见或建议,请联系QQ28-1688-302!

人工智能实验室
相关文章相关文章
  • Oculus公布原型机,大幅度提升可视角,能实现140°的视场水平

    Oculus公布原型机,大幅度提升可视角,能实现140°的视场水平

  • 2018深圳国际人工智能展览会 2018 shenzhen International Artif

    2018深圳国际人工智能展览会 2018 shenzhen International Artif

  • Michael I. Jordan带你解读百万奖金ATEC蚂蚁人工智能大赛

    Michael I. Jordan带你解读百万奖金ATEC蚂蚁人工智能大赛

  • teamLab创始人猪子寿之: 抛去衣食住行,我还剩下什么?

    teamLab创始人猪子寿之: 抛去衣食住行,我还剩下什么?

网友点评网友点评
阅读推荐阅读推荐

据外媒报道,STEER打造了首款完全自动驾驶停车技术,旨在使常规车辆转变为无人驾驶车辆。STEER的首款技术应用是4级自动驾驶及网络安全停车...

近日,美国软性机器抓手制造商 Soft Robotics 宣布,获得 2000 万美元的融资,本轮投资者包括 Scale Venture Partners,Calibrate Ventures...

据外媒报道,加州车管局发布了《2017自动驾驶脱离报告(California Autonomous Vehicle Disengagement Reports)》,其中谈及了脱离的具体...

用人机语音交互,来解决智能家居适老的问题;通过家庭门禁与安防套件、空气净化套件、可燃气体与有害气体监控套件等相互联动,在不同生活情...