不知爲何,chipmunk的 native版本 在 ios及 android上的 表現會不一樣.
我用的是 cocos3.8.1
這個函數 在 mac與 ios 的 native平臺,調用了以後 沒有任何效果.
/// Set a collision handler to be used whenever the two shapes with the given collision types collide.
/// You can pass null for any function you don't want to implement.
Space.prototype.addCollisionHandler = function(a, b, begin, preSolve, postSolve, separate)
{
assertSpaceUnlocked(this);
// Remove any old function so the new one will get added.
this.removeCollisionHandler(a, b);
var handler = new CollisionHandler();
handler.a = a;
handler.b = b;
if(begin) handler.begin = begin;
if(preSolve) handler.preSolve = preSolve;
if(postSolve) handler.postSolve = postSolve;
if(separate) handler.separate = separate;
this.collisionHandlers[hashPair(a, b)] = handler;
};
以下是我的碰撞檢測函數.在 android及其它平臺上,效果都是正常的.並且表現形式也一樣.
space.addCollisionHandler(1,
5,self._collisionWallBegin.bind(this),null,null,null);
_collisionWallBegin = function(arbiter, space){
//子彈與 牆碰撞
var self = this;
var shapes = arbiter.getShapes();
var collTypeA = shapes[0].collision_type;
var collTypeB = shapes[1].collision_type;
var p = arbiter.getPoint(0);
var notfire = false;
if(p.x-22>=cc.winSize.width || p.y>=350){
notfire = true;
}
if(!notfire){
//檢查是什麼類型子彈.別和牆碰撞
if(!shapes[0]._bulletsprite.isCollisionBottomWall()){
return false;
}
}
console.log( 'Collision Type A:' + collTypeA );
console.log( 'Collision Type B:' + collTypeB + ' nofire:'+notfire + ' x:'+ p.x);
if(shapes[0]._bulletsprite.clearBulletPartial(notfire?0.1:0.03,notfire)){
return false;
}
self.space.addPostStepCallback(function(){
self._fightLogic.attackMiss(myconfig.controlconfig.collisiontype_wall);
});
return false;
}
這樣的代碼 在 mac 及 ios上 無法工作..視覺上的確已經發生了碰撞,但是這段碰撞檢測代碼根本就不調用
於是就想辦法解決.
後來嘗試添加了一下 默認的 碰撞檢測
/// Set a default collision handler for this space.
/// The default collision handler is invoked for each colliding pair of shapes
/// that isn't explicitly handled by a specific collision handler.
/// You can pass null for any function you don't want to implement.
Space.prototype.setDefaultCollisionHandler = function(begin, preSolve, postSolve, separate)
{
assertSpaceUnlocked(this);
var handler = new CollisionHandler();
if(begin) handler.begin = begin;
if(preSolve) handler.preSolve = preSolve;
if(postSolve) handler.postSolve = postSolve;
if(separate) handler.separate = separate;
this.defaultHandler = handler;
};
也就是調用這個函數添加默認碰撞檢測.
this.space.setDefaultCollisionHandler(function(arbiter, space){
// console.log('default collision begin');
var self = this;
var shapes = arbiter.getShapes();
var collTypeA = shapes[0].collision_type;
var collTypeB = shapes[1].collision_type;
},null,null,null);
結果很興奮,這個檢測函數可以工作了.(爲什麼興奮?工作不是纔是正常的麼? :( 要是你也碰到這種奇葩問題 也會很興奮的.)
既然可以檢測碰撞了.那麼 就可以解決問題了.
爲了與 其它平臺能用.就做了個以下的特殊處理.問題得以解決
if(cc.sys.os == cc.sys.OS_OSX ||cc.sys.os == cc.sys.OS_IOS )
{//特殊處理
var collisions = {};
this.space.addCollisionHandler = function(typea,typeb,fun){
if(!collisions[typea]){
collisions[typea] = {};
}
collisions[typea][typeb] = fun;
};
this.space.setDefaultCollisionHandler(function(arbiter, space){
// console.log('default collision begin');
var self = this;
var shapes = arbiter.getShapes();
var collTypeA = shapes[0].collision_type;
var collTypeB = shapes[1].collision_type;
// console.log( 'Collision Type A:' + collTypeA );
// console.log( 'Collision Type B:' + collTypeB );
var coll = collisions[collTypeA];
if(coll && coll[collTypeB]){
return coll[collTypeB](arbiter,space);
//特殊處理
}else if(collisions[collTypeB] && collisions[collTypeB][collTypeA]){
//顛倒的.```
var p = arbiter.getPoint(0);
var myarb = {
getShapes:function(){
return [shapes[1],shapes[0]];
},
getPoint:function(x){
return p;
}
};
return collisions[collTypeB][collTypeA](myarb,space);
}
},null,null,null);
}
大功告成!
我後來想想 也有可能 我使用 addCollisionHandler
這個函數的 姿勢不對?..希望有大牛幫忙解惑.