1. 程式人生 > >為什麼cocos2d-x3.x版本的碰撞監聽不起作用?因為沒有設定掩碼

為什麼cocos2d-x3.x版本的碰撞監聽不起作用?因為沒有設定掩碼

好吧,其實這篇也是臨時冒出來的,最近朋友要做個物理遊戲,以前做物理還是用box2d,呃,確實要花些功夫才能搞懂其中的精髓,但是聽講這套引擎重新封裝了一次,要容易很多,所以就簡單嘗試了一下,感覺確實要簡單不少,不過在這其中還是遇到了些問題,首先就來說說onContactBegin這個回撥函式響應問題。

先說說情況,簡單做了一個打磚塊的遊戲,前面一切都很順利,只是做到碰撞檢測的時候,發現回撥函式弄死都不呼叫。開始我以為函式寫錯了,後來查了api,testCpp都沒有錯,在3.0的api中,沒有關於PhysicsBody,PhysicsWorld這些類的說明,所以大家想查移步到3.2的api中吧。

onContactBegin函式的引數就一個,

bool onContactBegin(PhysicsContact& contact),這和3.0beta版本又有些不同,以前是2個,所以在新增事件監聽的時候,不要寫錯,like this,

?
1 2 3 auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin,
this); _eventDispatcher->addEventListenerWithSceneGraphPriority(contactListener, this);

回撥函式是在當兩個物體有接觸的時候就會響應一次,裡面的引數從名字中也能看出來,Contact接觸,自然會涉及到兩個物體。

好了,下面該到重點了,為啥這個onContactBegin函式不響應呢?問題出在三個掩碼值上,這裡推薦一下看下這篇博文。

或者開啟引擎的原始碼,可以在CCPhysicsShape這個標頭檔案裡看下這段程式碼,

?
1 2 3 4 5 6 7 8 /** * A mask that defines which categories this physics body belongs to. * Every physics body in a scene can be assigned to up to 32 different categories, each corresponding to a bit in the bit mask. You define the mask values used in your game. In conjunction with the collisionBitMask and contactTestBitMask properties, you define which physics bodies interact with each other and when your game is notified of these interactions. * The default value is 0xFFFFFFFF (all bits set). */ inline void setCategoryBitmask(int bitmask) { _categoryBitmask = bitmask; } inline int getCategoryBitmask() const { return _categoryBitmask; }
?
1 2 3 4 5 6 7 8 /** * A mask that defines which categories of bodies cause intersection notifications with this physics body. * When two bodies share the same space, each body’s category mask is tested against the other body’s contact mask by performing a logical AND operation. If either comparison results in a non-zero value, an PhysicsContact object is created and passed to the physics world’s delegate. For best performance, only set bits in the contacts mask for interactions you are interested in. * The default value is 0x00000000 (all bits cleared). */ inline void setContactTestBitmask(int bitmask) { _contactTestBitmask = bitmask; } inline int getContactTestBitmask() const { return _contactTestBitmask; }

相關推薦

為什麼cocos2d-x3.x版本碰撞作用因為沒有設定

好吧,其實這篇也是臨時冒出來的,最近朋友要做個物理遊戲,以前做物理還是用box2d,呃,確實要花些功夫才能搞懂其中的精髓,但是聽講這套引擎重新封裝了一次,要容易很多,所以就簡單嘗試了一下,感覺確實要簡單不少,不過在這其中還是遇到了些問題,首先就來說說onContact

BrowserSync的大坑,作用解決辦法,附gulp瀏覽器同步自動重新整理部署方法

                能將本地伺服器代理到localhost:3000/localhsot:3001的操作:按照官網來操作的程式碼是不起作用的:browser-sync start --proxy "127.0.0.1:8081" "E:/newcrm/trunk/4_Tools/crm/**/*.

Mac OS X 10.11 sudo指令作用問題

OS X El Capitan中,在核心下引入了Rootless機制,以下路徑: /System /bin /sbin /usr (except /usr/local) 均屬於Rootless範圍,即使root使用者無法對此目錄有寫和執行許可權,只有Apple以及Apple授權簽名的軟體(包括命令列工具)可

記錄一次處理https正確的過程

負載均衡 https 502 nginx 金山雲 今天開發反饋在測試金山雲設備的時候遇到了這樣的一個現象:wget https://funchlscdn.lechange.cn/LCLR/2K02135PAK01979/0/0/20170726085033/dev_201707260850

vue使用element-ui的el-input了鍵盤事件解決

運行 string 封裝 應該 login ring 執行 -i 隱藏 vue使用element-ui的el-input監聽不了鍵盤事件,原因應該是element-ui自身封裝了一層div在input標簽外面,把原來的事件隱藏了,所以如下代碼運行是無響應的: <el-

dwz日期變化,dwz日期控件onchange作用,dwz框架時間控件支持onchange事件

getdate mst art sta rst this show onchange log 轉載自:http://blog.csdn.net/sp308036654/article/details/50638348 <input type="text" class

vue到props中的值時

dia div media handle iat prop hand function handler 已解決: watch: {   data: {     immediate: true,     handler: function(newVal

Android藍芽耳機的按鍵事件 藍芽無法響應KeyEvent

需求:藍芽耳機的按鍵事件,暫停/播放 音訊/視訊 ,無法響應藍芽KeyEvent的事件 此問題是由於中的藍芽KeyEvent監聽不到導致的,可以通過以下方法監聽藍芽按鍵事件    private MediaSession mSession;  &

onchangeinput值變化及input隱藏後change事件觸發的原因與解決方法(設定readonly後onchange作用的解決方案)

轉自:https://www.cnblogs.com/white0710/p/7338456.html 1. onchange事件監聽input值變化的使用方法: <input id="test"></input> $("input"

onchangeinput值變化及input隱藏後change事件觸發的原因與解決方法(設置readonly後onchange作用的解決方案)

com sdn 使用方法 pan val 內容 tar span .html 轉自:https://www.cnblogs.com/white0710/p/7338456.html 1. onchange事件監聽input值變化的使用方法: <input id=

CentOS7.0 Apache2.4.x 多埠配置

1 檢視Apache 版本 #httpd -v 如果返回的結果不是2.4.x 版本,下面配置方式不適用。 2 檢視httpd 所在路徑 #whereis httpd 3 根據第2步的結果中找到conf檔案所在目錄 4 使用vim開啟httpd.conf #vim

Cocos2d-x3.x遊戲開發之旅》學習

1.addEventListenerWidthSceneGraphPriority函式,這個函式的兩個引數作用如下:    EventListener *listener:事件監聽物件,當觸控事件發生時通過它來回調;    Node *node:繫結的物件,當node物件

(轉)Android 軟鍵盤回車作用

1. imeOptions <EditText android:imeOptions="actionSend"/> 或 mEditText.setImeOptions(EditorInfo.IME_ACTION_SEND);

vue中使用element-ui的el-input了回車事件???

在vue中,直接給input繫結回車事件,直接這麼寫:<input type="text" placeholder="請輸入暱稱" v-model="search" @keyup.13="sear

Windows 搭建 cocos2d-x3.x 開發環境及 android 打包

cocos2d版本更新之快及版本之混亂是出了名的。cocos2d打包android apk也是出了名的麻煩。所以,對於我這種新手來說,配置cocos2d-x的安卓環境就是出了名的難。經過兩天的折騰,終

cocos2d-x3.x的拖尾效果

作為一個實習,也就是傳說中的碼農,自身的程式碼基礎也不夠,所以暫時就不扒開底層對每個引數一一研究了,不過其實知道了api,完全可以通過自己檢視一步步理解,每個函式底層程式碼裡邊都有註釋。 Motion

vue使用element-ui的el-input了回車事件解決

vue使用element-ui的el-input監聽不了回車事件,原因應該是element-ui自身封裝了一層input標籤之後,把原來的事件隱藏了,所以如下程式碼執行是無響應的: <

關於使用element-ui的el-input了回車事件

使用element-ui的el-input監聽不了回車事件,原因應該是element-ui自身封裝了一層input標籤之後,把原來的事件隱藏了,所以如下程式碼執行是無響應的: <el-inp

cocos2d-x3.x 獲取系統時間

void getLocationTime() { struct tm *tm; time_t timep; #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) time(&timep); #else

JScheckbox的選擇獲取取消事件代案列

事件 onclick att 選中 types bsp put color type function OncheckBox(index){ if ($(index).attr("checked") == "checked") { alert($(ind