Cocos笔记
Cocos笔记好用的Api碰撞体范围检测包围盒范围检测(性能对比碰撞检测稍好)多边形碰撞体和矩形相交检测设置父节点并同步位置外部增加并调用回调函数网址其他代码混淆工具引用加载过久修改tsconfig.json脚本增加以下代码类型写法(举例)刮刮乐脚本修改后的挖图(用于引导挖多个空)增加恒久根节点游戏事件(看广告相关)自定义事件派发等比计算进度条基于oops的音频管理优化版DialogAudioSourceDialogAudio剔除以.meta结尾的文件加载bundle和使用bundle加载资源网上下载图片加载剔除闪频打包后html文件运行不了问题监控node节点父物体变化的情况cocos对象池bundle加载时进度输出制作放缩地图制作循坏摇摆的动画加载场景时进度条获取当天0点的时间将毫秒转为时分秒的格式(最小单位为秒,如:"00:01:59")通过Bound获取包围节点加载进度管理音效播放带手指偏移的物体移动点击mask区域无反应优化方案点击事件传递数据存储兼容老数组打散数组tween的监测更新分帧生成处理类通过代理获取"[]"数据精确到小数点后两位且不填充0(可优化位数)动画修改插槽获取向上移动像素后的坐标动画回调游戏后台暂停监听按钮事件监听全局点击事件(计算空闲点击)监听鼠标点击事件输出文件的md5哈希值使用的上传工具打包小游戏预览骨骼动画取消异步失败返回没有catch接收的黑色弹窗(报错)修改引用查询(防止引用过多加载时间过长)同时多个异步等待后端通信加密取消tween的Node节点绑定(3.8.7版本新增)取消h5页面ScrollView组件的鼠标滚轴滑动好用的Api碰撞体范围检测checkCollider(){letcollider=this.node.getComponent(BoxCollider2D);if(!collider)returnnull;// 获取目标节点碰撞体的世界坐标范围letworldBounds=collider.worldAABB;// 测试与周围矩形范围相交的所有碰撞体letcolliders=PhysicsSystem2D.instance.testAABB(worldBounds);// 处理碰撞结果for(leti=colliders.length-1;i=0;i--){letotherCollider=colliders[i];letcolParent=otherCollider.getComponent(ReceiveController);//TODO 这里写碰撞体处理的代码}}/**调整后的包围盒大小 */getAdjustedBoundingBox(node:Node){// 获取节点的包围盒letboundingBox=node.getComponent(UITransform)!.getBoundingBoxToWorld();letoffset=3;// 调整包围盒的宽度和高度boundingBox.width-=offset;boundingBox.height-=(offset+10);// 可选:调整包围盒的位置,使其仍然居中//boundingBox.x += offset/2;boundingBox.y+=5;returnboundingBox;}包围盒范围检测(性能对比碰撞检测稍好)myWidth=0;myHeight=0;protectedonLoad():void{constmyTransform=this.node.getComponent(UITransform);this.myWidth=myTransform.width;this.myHeight=myTransform.height;}checkItem(){letnodeArr=this.getAllRect();if(!nodeArr||nodeArr.length==0){this.setMask(false);return;}letthisGray=false;// 处理碰撞结果for(leti=0;inodeArr.length;i++){letotherNode=nodeArr[i];letotherControl=otherNode.getComponent(eliminateItemController);letotherLayer=otherNode.getSiblingIndex();letmyLayer=this.node.getSiblingIndex();//处理层级置灰问题if(otherLayermyLayer){if(!otherControl.getMaskGray()){otherControl.setMask(true);}}elseif(otherLayermyLayer){//其他物体盖在上面将自己置灰if(!this.getMaskGray()){this.setMask(true);}thisGray=true;}//otherControl.checkCompleteOcclusion();}if(!thisGray){this.setMask(false);}}getAllRect(){letnodes=[];letboundingBox=this.getAdjustedBoundingBox(this.node);for(leti=0;ithis.node.parent.children.length;i++){letitem=this.node.parent.children[i];if(this.checkPosition(item.position)){if(item!==this.node){// 获取当前节点的包围盒letnodeBoundingBox=this.getAdjustedBoundingBox(item);letcomp=item.getComponent(eliminateItemController);// 检查包围盒是否相交if(compboundingBox.intersects(nodeBoundingBox)){nodes.push(item);}}}}returnnodes;}/**首先判断位置 */checkPosition(targetPos:Vec3):boolean{if(targetPos.xthis.node.position.x+this.myWidth){// 物体在右侧returnfalse}elseif(targetPos.xthis.node.position.x-this.myWidth){// 物体在左侧returnfalse}elseif(targetPos.ythis.node.position.y+this.myHeight){// 物体在上方returnfalse}elseif(targetPos.ythis.node.position.y-this.myHeight){// 物体在下方returnfalse}else{returntrue;}}/**调整后的包围盒大小 */getAdjustedBoundingBox(node:Node){// 获取节点的包围盒letboundingBox=node.getComponent(UITransform)!.getBoundingBoxToWorld();letoffset=3;// 调整包围盒的宽度和高度boundingBox.width-=offset;boundingBox.height-=(offset+10);// 可选:调整包围盒的位置,使其仍然居中//boundingBox.x += offset/2;boundingBox.y+=10;returnboundingBox;}getMaskGray(){returnthis.maskNode.active;}setMask(val){this.maskNode.active=val;}多边形碰撞体和矩形相交检测letcheckBox=this.node.getChildByName("checkBox");letwbox=checkBox.getComponent(UITransform).getBoundingBoxToWorld();letglassNode=childLayer.children[j].getChildByName("glassSp");letcol:PolygonCollider2D=glassNode.getComponent(PolygonCollider2D);//这个Intersection2D也可以用于其他形状的检测if(Intersection2D.rectPolygon(wbox,col.worldPoints)){canClick=false;break;}设置父节点并同步位置setParent(parent){letworldPos=this.node.getWorldPosition();// 改变父节点this.node.setParent(parent);// 转换世界坐标为相对于新父节点的局部坐标letlocalPos=parent.getComponent(UITransform).convertToNodeSpaceAR(worldPos);// 应用新的局部坐标this.node.setPosition(localPos);}外部增加并调用回调函数//渲染事件(渲染器)@property({type:[EventHandler],})publicclickEvent=[];callBack(){for(leti=0;ithis.clickEvent.length;i++){this.clickEvent[i].emit([this.node]);}}网址creator示例地址混淆工具网址其他代码混淆工具混淆工具网址引用加载过久修改tsconfig.json脚本增加以下代码"include":["assets/**/*"],"exclude":["node_modules","temp","extensions","build","dist"]类型写法(举例)@property({type:[Node],visible(){returnthis.articles==ArticlesObj.ClidkObj;},tooltip:'执行完毕后将数组内物体Active打开'})addObjects:Node[]=[];刮刮乐脚本import{_decorator,Component,v2,Node,UITransform,CCBoolean,CCFloat,Graphics,Mask,Vec3,typeVec2}from'cc';constHANG_MAX=14;const{ccclass,property}=_decorator;@ccclass('ScrapeImg')exportclassScrapeImgextendsComponent{@property(CCBoolean)canTouch:boolean=false;@property({type:CCFloat,tooltip:'当前遮罩的编号,画完时进行广播'})maskIndex:number=0;@property({tooltip:'勾选绘制圆形,取消绘制矩形'})isCircle=true;@property(Node)myMask:Node=null;@property({type:Node,tooltip:'检测区域的节点'})checkNode:Node=null;@property({min:20,tooltip:"绘制圆形半径",visible(){returnthis.isCircle;},})size:number=30;@property({min:20,tooltip:"绘制矩形宽度",visible(){return!this.isCircle;},})width:number=30;@property({min:20,tooltip:"绘制矩形长度",visible(){return!this.isCircle;},})height:number=30;@property({tooltip:"分片后阈值:\n结果判断会依赖此值"})value=40;protectedpositionProgress:Boolean[]=[];protectedpositions:Vec2[]=[];graphics:Graphics;finish=false;onLoad(){if(this.canTouch){this.node.on(Node.EventType.TOUCH_START,this._onTouchBegin,this);this.node.on(Node.EventType.TOUCH_MOVE,this._onTouchMoved,this);this.node.on(Node.EventType.TOUCH_END,this._onTouchEnd,this);}this.graphics=this.myMask.getComponent(Graphics);//不反向就填充if(!this.myMask.getComponent(Mask).inverted){this.graphics.circle(0,0,0);this.graphics.close();this.graphics.stroke();this.graphics.fill();}//检测区域this.positions=[];this.checkNode.children.forEach((item)={this.positions.push(v2(item.position.x,item.position.y));});this.positionProgress=newArray(this.positions.length);}_onTouchBegin(event){letlocation=event.getUILocation();letposVec3=newVec3(location.x,location.y,0);letpoint=this.node.getComponent(UITransform).convertToNodeSpaceAR(posVec3);this._addGraphics(point);}_onTouchMoved(event){letlocation=event.getUILocation();letposVec3=newVec3(location.x,location.y,0);letpoint=this.node.getComponent(UITransform).convertToNodeSpaceAR(posVec3);this._addGraphics(point);this._checkProgress();}_onTouchEnd(event){letlocation=event.getUILocation();letposVec3=newVec3(location.x,location.y,0);letpoint=this.node.getComponent(UITransform).convertToNodeSpaceAR(posVec3);this._addGraphics(point);this._checkProgress();}drawGraphics(pos){lettranfrom=this.node.getComponent(UITransform);letnodePos=tranfrom.convertToNodeSpaceAR(pos);if(this._checkBound(v2(nodePos.x,nodePos.y))){this.node.dispatchEvent(newValueNotification(Events.checkStage,true,this.maskIndex));}this._addGraphics(nodePos);this._checkProgress();}checkBound(pos){lettranfrom=this.node.getComponent(UITransform);letnodePos=tranfrom.convertToNodeSpaceAR(pos);if(this._checkBound(v2(nodePos.x,nodePos.y))){this.node.dispatchEvent(newValueNotification(Events.checkStage,true,this.maskIndex));}}/**绘制函数*/_addGraphics(point){if(!this.graphics)return;this.graphics.lineWidth=10;if(this.isCircle)this.graphics.circle(point.x,point.y,this.size);elsethis.graphics.rect(point.x-this.width/2,point.y-this.height/2,this.width,this.height);this._checkPoint(point);//3.8.2使用this.graphics.close();this.graphics.stroke();this.graphics.fill();//下面是3.8.3及以上使用//fillRect()也行不过用之前先clear()}private_checkPoint(point:Vec2){for(leti=0;ithis.positions.length;i++){letp=this.positions[i];// 此点在关键点附近if(p.x+this.valuepoint.xp.x-this.valuepoint.xp.y+this.valuepoint.yp.y
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2481863.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!