下面简单记录一下在最近cocos2d-x项目在iOS平台真机测试和模拟器测试中遇到的一些要注意的地方(使用ipod):

1、图片大小

游戏中基本上都是会用到图片,那么在使用图片的时候要特别注意图片的size。

注意:一般来说,在设计图片的时候,其大小要设计为我们所需要图片大小的两倍大小。(why,下面解释)

例如说:

我们需要一张50*50大小的图片,用于精灵显示,也就说在屏幕中(无论是模拟器还是真机)显示的大小都是50*50。那么我们设计的图片大小要多少呢?没错,就是100*100。那么我们在模拟器中测试的时候,要注意将其scale设置为0.5,这样在模拟器中就可以显示我们所需要的正确的大小了;那到了真机测试的时候,就不用设置图片的scale了,直接使用这100*100的图片就可以正确显示我们所需要的50*50大小的图片了。

为什么会这样呢?

我们可以在debug消息中,看到:

①如果是模拟器,cocos2d: surface size: 320x480

②如果是真机,cocos2d: surface size: 640x960

没错了,这就是因为屏幕分辨率的问题导致了,这里我用到的模拟器设备是iPhone。

那么如果你模拟器使用下面的Retina的话,屏幕分辨率就是640*960了。

2、坐标点位置

上面已经讲到由于模拟器和真机不同的分辨率导致图片size的问题,其实还有一个问题也同样要注意---坐标点的位置大小。

因为我们一般都是基于320*480这样大小的屏幕下进行程序设计,那么相应的位置坐标设置也是相当于320*480这样大小来设计的。那么由于实际真机的的屏幕是640*960的,那么我们如果在320*480的模拟器下调试好了,直接将程序放到真机中的话,图片等等的位置是错乱的。为什么呢?因为坐标点的大小都缩小了一半。那么解决的方法也是很简单的,就是将所有的坐标点的x轴和y轴坐标数据乘以2,就可以了。

总结:上面所遇到的问题的根源就是我们使用的模拟器和真机之间的分辨率不同,模拟器是320*480,而真机是640*960。由于个人习惯是在320*480的模拟器下进行程序设计,所有就需要对图片大小,坐标点大小进行一些额外的处理(其实也很简单)。但是如果嫌麻烦的话,可以之间使用iPhone(Retina 3.5-inch)的模拟器,这样的模拟器分辨率就是和真机一样的640*960。

总之,只要我们在图片大小和坐标点大小处理的时候,始终是基于真机的640*960进行处理,那么将程序移植到真机的时候,就没有什么问题了。

下面对这部分内容进行一些补充:

其实关于关于cocos2d-x屏幕分辨率的适配问题,网上已经有好多博客进行了介绍,主要是针对android平台的多分辨率屏幕,但是对于iOS平台还比较好屏幕分辨率比较固定。

那么针对多分辨率屏幕的适配,其实在cocos2dx中有一个方法是用于解决这个问题的---setDesignResolutionSize。

这个方法在应用程序启动的时候进行调用applicationDidFinishLaunching()。

 /**
* Set the design resolution size.
* @param width Design resolution width.
* @param height Design resolution height.
* @param resolutionPolicy The resolution policy desired, you may choose:
* [1] kResolutionExactFit Fill screen by stretch-to-fit: if the design resolution ratio of width to height is different from the screen resolution ratio, your game view will be stretched.
* [2] kResolutionNoBorder Full screen without black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two areas of your game view will be cut.
* [3] kResolutionShowAll Full screen with black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two black borders will be shown.
*/
virtual void setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy);

其中的第三个参数可以为:

enum 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.
kResolutionExactFit,
// The entire application fills the specified area, without distortion but possibly with some cropping,
// while maintaining the original aspect ratio of the application.
kResolutionNoBorder,
// 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.
kResolutionShowAll,
// 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
kResolutionFixedHeight,
// 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
kResolutionFixedWidth, kResolutionUnKnown,
};

关于具体的屏幕适配解决方法这里不做介绍,可以参考这篇文章:点击打开链接

下面针对上面我说到的iOS平台下,关于图片大小和坐标点大小的设置。

我之前已经说过,一般是基于320*480进行设计程序的,那么我们可以在applicationDidFinishLaunching()中(注意在

pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); 之后添加)添加下面两行代码

CCSize designSize = CCSizeMake(320, 480);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionShowAll);

这样的话,我们就将真机的屏幕大小设置成了320*480的size了。

通过 CCDirector::sharedDirector()->getWinSize() 获得的真机的size也是320*480了。

那么在程序中我们设置坐标点的时候,真机和模拟器的坐标点是一致的。但是假如你使用的是放大一倍的图片,那么需要对图片的scale进行0.5的处理了。这样做的好处是,模拟器和真机进行调试的时候二者是一样的,就不会出现图片大小或者坐标点大小不一致的问题了。

最新文章

  1. 解决VS2015安装Android SDK 后文件不全及更新问题
  2. liunx下,只获取主机的IP?
  3. Hibernate 注解的用法以及说明(二)
  4. [转]Python程序员必须知道的30条编程技巧
  5. 分享10款激发灵感的最新HTML5/CSS3应用
  6. android的ScaleGestureDetector缩放类详解
  7. NSUserDefaults偶尔/有时候保存数据会失败/失效
  8. (转)一个form表单实现提交多个action
  9. java hascode
  10. Android的TabHost组件-android的学习之旅(四十)
  11. Tree 树形结构
  12. Alpha冲刺(10/10)
  13. mysql 批量导入
  14. 【English】20190308
  15. BZOJ.3165.[HEOI2013]Segment(李超线段树)
  16. Vue中table表头合并的用法
  17. opencv 图像深度(depth)
  18. EditPlus 4.3.2583 中文版已经发布
  19. ViewPager滑动不畅及灵敏度的问题
  20. 基于Cocos2d-x学习OpenGL ES 2.0系列——你的第一个三角形(1)

热门文章

  1. SQL 数据库表标识列初始化 DBCC
  2. POJ 1422 Air Raid (最小路径覆盖)
  3. python练习程序(c100经典例4)
  4. QR二维码(转)
  5. mysql:mysql_query(): Unable to save result set
  6. Java中ThreadLocal的深入理解
  7. Oracle 单实例 迁移到 RAC 实例 -- 使用RMAN 异机恢复
  8. 2016第20周四java基础概念
  9. 参考:iPhone OS 3.0中的字体列表
  10. 输出图片的php代码前面不能有空白行