Cocos2d-js3.3 模态对话框的实现

首先,先了解一下什么是模态对话框,百度百科的给出了下面一个定义:

模态对话框(Modal Dialogue Box,又叫做模式对话框),是指在用户想要对对话框以外的应用程序进行操作时,必须首先对该对话框进行响应。如单击【确定】或【取消】按钮等将该对话框关闭。

游戏中经常会出现很多模态对话框,例如登陆框、提示框等等。

下面来看下效果图:

有几点值得关注的:

1.当对话框出现后,对话框外不响应触摸。

这就需要用到setSwallowTouches(),设置为true时,并且onTouchBegan返回true,就会吞没事件,不再向下传递

2.当对话框出现后,背景变暗

这里用到LayerColor的两个方法,设置颜色setColor()和设置透明度setOpacity()

下面,请看代码:

[javascript] view plain copy
 
  1. var ModalDialogueBox = cc.LayerColor.extend({  
  2.     _listener: null,  
  3.     ctor: function() {  
  4.         this._super(cc.color.BLACK);  
  5.         this.ignoreAnchorPointForPosition(false);   //忽略锚点设置为false,默认为true,锚点(0, 0)  
  6.         this.setOpacity(128);       //透明度  
  7.   
  8.         //初始化对话框  
  9.         this._initDialog();  
  10.   
  11.         return true;  
  12.     },  
  13.   
  14.     onEnter: function()  
  15.     {  
  16.         this._super();  
  17.         //监听器  
  18.         this._listener = new cc.EventListener.create({  
  19.             event: cc.EventListener.TOUCH_ONE_BY_ONE,  
  20.             swallowTouches: false,  
  21.             onTouchBegan: function(touch, event)  
  22.             {  
  23.                 return true;  
  24.             }  
  25.         });  
  26.   
  27.         //添加触摸监听  
  28.         cc.eventManager.addListener(this._listener, this);  
  29.     },  
  30.   
  31.     //初始化对话框  
  32.     _initDialog: function()  
  33.     {  
  34.         var winSize = cc.winSize;  
  35.   
  36.         //背景  
  37.         var bg = new cc.Sprite(res.dialog_png);  
  38.         bg.setPosition(cc.p(winSize.width / 2, winSize.height / 2));  
  39.         this.addChild(bg, 0, 101);  
  40.   
  41.         //OK按钮  
  42.         var OKLabel = new cc.LabelTTF("OK", "Arial", 36);  
  43.         var OKMenuItem = new cc.MenuItemLabel(OKLabel, this._onCallback, this);  
  44.         OKMenuItem.setPosition(cc.p(100, 50));  
  45.   
  46.         //Cancel按钮  
  47.         var cancelLabel = new cc.LabelTTF("Cancel", "Arial", 36);  
  48.         var cancelMenuItem = new cc.MenuItemLabel(cancelLabel, this._onCallback, this);  
  49.         cancelMenuItem.setPosition(cc.p(250, 50));  
  50.   
  51.         //菜单  
  52.         var menu = new cc.Menu(OKMenuItem, cancelMenuItem);  
  53.         menu.setPosition(cc.p(0, 0));  
  54.         bg.addChild(menu);      //注意是添加到背景里面  
  55.   
  56.         this.setVisible(false);     //默认设置为不可见  
  57.     },  
  58.   
  59.     _onCallback: function()  
  60.     {  
  61.         this.hidden();  
  62.     },  
  63.   
  64.     //弹出  
  65.     popup: function()  
  66.     {  
  67.         this.setVisible(true);  
  68.         this._listener.setSwallowTouches(true);  
  69.   
  70.         var bg = this.getChildByTag(101);  
  71.         bg.setScale(0);  
  72.         var scaleTo = new cc.ScaleTo(2.0, 1);  
  73.         var rotateBy = new cc.RotateBy(2.0, 360, 0);  
  74.         var spawn = new cc.Spawn(scaleTo, rotateBy);  
  75.         bg.runAction(spawn);  
  76.     },  
  77.   
  78.     //隐藏  
  79.     hidden: function()  
  80.     {  
  81.         this.setVisible(false);  
  82.         this._listener.setSwallowTouches(false);  
  83.     },  
  84.   
  85.     onExit: function()  
  86.     {  
  87.         this._super();  
  88.         //移除触摸监听  
  89.         cc.eventManager.removeListeners(cc.EventListener.TOUCH_ONE_BY_ONE, true);  
  90.     }  
  91. });  


源码下载:点击打开链接

原文地址:https://www.cnblogs.com/luorende/p/8806848.html