微信小游戏-CocosCreator 基础(十六)

碰撞检测系统
1: creator有碰撞检测系统 +物理碰撞系统,这个是两个独立的模块;
2: 给creator的游戏世界中的物体来进行分组,指定节点的分组与分组的碰撞矩阵;
3: 代码中获取节点的分组和分组索引: group与groupIndex;
4:  为每个节点添加碰撞检测区域-->碰撞器(物体形状), 编辑碰撞区域;
5:  代码开启碰撞检测系统(默认是关闭碰撞检测),开启和关闭碰撞检测的调试:
        var manager = cc.director.getCollisionManager(); // 
        manager.enabled = true; // 开启碰撞
        manager.enabledDebugDraw = true; // 允许绘制碰撞的区域
6: 碰撞检测函数响应,发生碰撞检测的节点,会调用这个节点上所有组件的统一的三个接口:
     onCollisionEnter: function (other, self) // 开始
     onCollisionStay: function (other, self)  // 持续
     onCollisionExit: function (other, self)    // 结束
     其中other是与这个节点碰撞的节点的碰撞器组件
     其中self是自身节点的碰撞器组件   
     是碰撞器组件,不是节点-->碰撞器组件.node
   ======================================================
     开启碰撞
cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional,

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Creator打地鼠游戏hitmouse.zip // Learn cc.Class: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/class.html // - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/class.html // Learn Attribute: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html // - [English] http://docs.cocos2d-x.org/creator/manual/en/scripting/reference/attributes.html // Learn life-cycle callbacks: // - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html // - [English] https://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html cc.Class({ extends: cc.Component, properties: { mouse: [cc.Node], hammer: cc.Node, txtCount: cc.Label, }, // LIFE-CYCLE CALLBACKS: onLoad () { var manager = cc.director.getCollisionManager(); manager.enabled = true; this.count = 0; this.txtCount.string = this.count; }, start () { cc.director.getScheduler().schedule(this.logic, this, 0.5, false); this.node.on(cc.Node.EventType.TOUCH_START, function(event){ for(let i = 0; i < this.mouse.length; i++){ if(this.mouse[i].opacity == 0){ continue; } let parent = this.mouse[i].parent; let maskPos = parent.parent.convertToWorldSpaceAR(parent.position); let maskRect = cc.rect(maskPos.x - parent.width / 2 , maskPos.y , parent.width, parent.height); let mouse = this.mouse[i]; let mousePos = parent.convertToWorldSpaceAR(mouse.position); let mousRect = cc.rect(mousePos.x - mouse.width / 2, mousePos.y - mouse.height / 2, mouse.width, mouse.height); let mixRect = new cc.Rect(); maskRect.intersection(mixRect, mousRect); if(mixRect.contains(event.g

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值