zoukankan      html  css  js  c++  java
  • cocos creator基础-(十五)碰撞检测系统

    1: 掌握碰撞检测的基本步骤;
    2: 掌握开启碰撞检测和响应碰撞;
    3: 完成道具拾取案例,理解group与groupIndex;



    1: creator有碰撞检测系统 +物理碰撞系统,这个是两个独立的模块;
    2: 给creator的游戏世界中的物体来进行分组,指定节点的分组与分组的碰撞矩阵;
    3: 代码中获取节点的分组和分组索引: group与groupIndex(0~n);
    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, default is true
            //    visible: true,      // optional, default is true
            //    displayName: 'Foo', // optional
            //    readonly: false,    // optional, default is false
            // },
            // ...
            is_enbale: true,
            is_debug: true,
        },
    
        // use this for initialization
        onLoad: function () {
            if (this.is_enbale) {
                var manager = cc.director.getCollisionManager();
                manager.enabled = true; // 开启碰撞
                if (this.is_debug) {
                    manager.enabledDebugDraw = true; // 调试状态绘制出我们物体的碰撞器的形状
                }
            }
        },
    
        // called every frame, uncomment this function to activate update callback
        // update: function (dt) {
    
        // },
    });
     
    碰撞检测脚本
    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, default is true
            //    visible: true,      // optional, default is true
            //    displayName: 'Foo', // optional
            //    readonly: false,    // optional, default is false
            // },
            // ...
        },
    
        // use this for initialization
        onLoad: function () {
    
        },
    
        // other是道具的碰撞器组件
        // self 是自己节点的碰撞器组件 
        // 碰撞器是一个组件,所以我们可以通过组件 -->节点
        // 碰撞开始
        onCollisionEnter: function (other, self) {
            console.log("other.name = ", other.node.name, other.node.group, other.node.groupIndex);
            if (other.node.groupIndex === 2) { // 与道具相撞
                var prop = other.node.getComponent("prop");
                console.log("我们捡到了道具:", prop.prop_type);
            }
        },
    
        // 碰撞持续
        onCollisionStay: function (other, self) {
    
        },
        // end 
    
        // 碰撞结束
        onCollisionExit: function (other, self) {
    
        },
        // called every frame, uncomment this function to activate update callback
        // update: function (dt) {
    
        // },
    });
     
  • 相关阅读:
    【原】git常见用法
    【转】EDID的简介和解析
    rsa公钥和私钥的生成
    往redis中存储数据是利用pipeline方法
    对于接口文档个的说明内容包括哪些
    blueprint的使用
    flask中如何生成迁移文件
    flask中自定义过滤器
    jsonify
    flask自定义处理错误方法
  • 原文地址:https://www.cnblogs.com/orxx/p/10453351.html
Copyright © 2011-2022 走看看