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) { // }, });
