cocos2d-JS 实现 横 竖 屏 显示提示图片效果 !!!!!

前端之家收集整理的这篇文章主要介绍了cocos2d-JS 实现 横 竖 屏 显示提示图片效果 !!!!!前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

全局添加旋转屏幕帧听 :

<code class="hljs javascript has-numbering" style="display: block; padding: 0px; color: inherit; Box-sizing: border-Box; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">window.addEventListener(<span class="hljs-string" style="color: rgb(0,136,0); Box-sizing: border-Box;">'orientationchange'</span>,<span class="hljs-function" style="Box-sizing: border-Box;"><span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">function</span><span class="hljs-params" style="color: rgb(102,102); Box-sizing: border-Box;">(event)</span>{</span>
    <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//获取当前场景</span>
    <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">var</span> currentScene = cc.director.getRunningScene();
    <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//如果当前场景不存在  return</span>
    <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span>(currentScene == <span class="hljs-literal" style="color: rgb(0,102,102); Box-sizing: border-Box;">null</span>) <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">return</span>;
    <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//如果是竖屏   就创建 LayerColor 和 一个提示图片 rotate </span>
    <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span> ( window.orientation == <span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">180</span> || window.orientation==<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">0</span> ) {
        <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//alert("竖屏");</span>
        <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">var</span> rotateImg = <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">new</span> cc.Sprite(<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"res/rotate.png"</span>);
        rotateImg.x = cc.winSize.width/<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">2</span>;
        rotateImg.y = cc.winSize.height/<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">2</span>;
        <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">var</span> _LayerColor =  <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">new</span> cc.LayerColor(cc.winSize.height,cc.winSize.width);
        _LayerColor.addChild(rotateImg);
        _LayerColor.setVisible(<span class="hljs-literal" style="color: rgb(0,102); Box-sizing: border-Box;">true</span>);
        <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//创建时候 添加tag值 便于之后寻找对应对象 (tag = 300)</span>
        currentScene.addChild(_LayerColor,<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">9999</span>,102); Box-sizing: border-Box;">300</span>);
    }
    <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//如果横屏就移除</span>
    <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span>( window.orientation == <span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">90</span> || window.orientation == -<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">90</span> ) {
        <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//alert("横屏");</span>
        <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//通过tag值 寻找对应对象</span>
        <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">var</span> first = currentScene.getChildByTag(<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">250</span>);
        <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span>(first != <span class="hljs-literal" style="color: rgb(0,102); Box-sizing: border-Box;">null</span>){
            first.removeFromParent()
        }
        <span class="hljs-comment" style="color: rgb(136,136); Box-sizing: border-Box;">var</span> second = currentScene.getChildByTag(<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">300</span>);
        <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span>(second != <span class="hljs-literal" style="color: rgb(0,102); Box-sizing: border-Box;">null</span>){
            second.removeFromParent()
        }
    }
});</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,221,221); list-style: none; text-align: right; background-color: rgb(238,238,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li><li style="Box-sizing: border-Box; padding: 0px 5px;">20</li><li style="Box-sizing: border-Box; padding: 0px 5px;">21</li><li style="Box-sizing: border-Box; padding: 0px 5px;">22</li><li style="Box-sizing: border-Box; padding: 0px 5px;">23</li><li style="Box-sizing: border-Box; padding: 0px 5px;">24</li><li style="Box-sizing: border-Box; padding: 0px 5px;">25</li><li style="Box-sizing: border-Box; padding: 0px 5px;">26</li><li style="Box-sizing: border-Box; padding: 0px 5px;">27</li><li style="Box-sizing: border-Box; padding: 0px 5px;">28</li><li style="Box-sizing: border-Box; padding: 0px 5px;">29</li><li style="Box-sizing: border-Box; padding: 0px 5px;">30</li><li style="Box-sizing: border-Box; padding: 0px 5px;">31</li><li style="Box-sizing: border-Box; padding: 0px 5px;">32</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li><li style="Box-sizing: border-Box; padding: 0px 5px;">20</li><li style="Box-sizing: border-Box; padding: 0px 5px;">21</li><li style="Box-sizing: border-Box; padding: 0px 5px;">22</li><li style="Box-sizing: border-Box; padding: 0px 5px;">23</li><li style="Box-sizing: border-Box; padding: 0px 5px;">24</li><li style="Box-sizing: border-Box; padding: 0px 5px;">25</li><li style="Box-sizing: border-Box; padding: 0px 5px;">26</li><li style="Box-sizing: border-Box; padding: 0px 5px;">27</li><li style="Box-sizing: border-Box; padding: 0px 5px;">28</li><li style="Box-sizing: border-Box; padding: 0px 5px;">29</li><li style="Box-sizing: border-Box; padding: 0px 5px;">30</li><li style="Box-sizing: border-Box; padding: 0px 5px;">31</li><li style="Box-sizing: border-Box; padding: 0px 5px;">32</li></ul>

初始化游戏场景时调用 : (因为全局帧听需要选择才会起作用,所以第一次需要手动添加一次提示旋转图)

<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; Box-sizing: border-Box; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">this</span>._checkOrientation()</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221,238);"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li></ul>

第一次初始化判断是不是竖屏 创建一个 LayerColor 和 所需提示旋转图片

<code class="hljs avrasm has-numbering" style="display: block; padding: 0px; color: inherit; Box-sizing: border-Box; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;">    _checkOrientation: function(){
        if (window<span class="hljs-preprocessor" style="color: rgb(68,68,68); Box-sizing: border-Box;">.orientation</span> == <span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">180</span> || window<span class="hljs-preprocessor" style="color: rgb(68,102); Box-sizing: border-Box;">0</span>) {
            //alert(<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"竖屏"</span>)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">;</span>
            var _rotateImg = new cc<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.Sprite</span>(<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"res/rotate.png"</span>)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">;</span>
            _rotateImg<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.x</span> = cc<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.winSize</span><span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.width</span>/<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">2</span><span class="hljs-comment" style="color: rgb(136,68); Box-sizing: border-Box;">.y</span> = cc<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.height</span>/<span class="hljs-number" style="color: rgb(0,0); Box-sizing: border-Box;">;</span>
            var _LayerColor =  new cc<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.LayerColor</span>(cc<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.height</span>,cc<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.width</span>)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">;</span>
            _LayerColor<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.addChild</span>(_rotateImg)<span class="hljs-comment" style="color: rgb(136,68); Box-sizing: border-Box;">.setVisible</span>(true)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">;</span>
            var currentScene = cc<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.director</span><span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.getRunningScene</span>()
            currentScene<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.addChild</span>(_LayerColor,102); Box-sizing: border-Box;">250</span>)
            //cc<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.pause</span>()
        }

        //if( window<span class="hljs-preprocessor" style="color: rgb(68,102); Box-sizing: border-Box;">90</span> || window<span class="hljs-preprocessor" style="color: rgb(68,68); Box-sizing: border-Box;">.orientation</span> == -<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">90</span> ) {
        //    alert(<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"横屏"</span>)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">;</span>
        //}
    },</code>

猜你在找的Cocos2d-x相关文章