zoukankan      html  css  js  c++  java
  • Cocos2d-x 关于在iOS平台真机测试的一些注意


    下面简单记录一下在最近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的处理了。这样做的好处是,模拟器和真机进行调试的时候二者是一样的,就不会出现图片大小或者坐标点大小不一致的问题了。


  • 相关阅读:
    spring boot 配置时区差别
    概率期望
    Euler函数与Euler定理
    素数&筛法
    等差子序列
    8.19 T2
    8.19 T1
    量化交易
    挺进

  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3263221.html
Copyright © 2011-2022 走看看