精灵和drawDot之间的碰撞检测? (JavaScript的)

时间:2016-01-14 16:31:54

标签: cocos2d-x cocos2d-js

我有这个精灵(方形图像)

this.player = new cc.Sprite();
this.player.initWithFile(res.player_png, cc.rect(0,0,50,50));
this.player.setAnchorPoint(cc.p(0.5, 0.5));
this.player.setPosition(cc.p(this.size.width/2, this.size.height/2));
this.addChild(this.player, 0);

我有这个点

this.dot = new cc.DrawNode.create();
this.dot.drawDot(cc.p(
    this.size.width/2, 
    this.size.height/2), 
    100, 
    cc.color(12, 156, 194, 100));

this.addChild(this.circle, 0);

我如何检查它们是否发生碰撞?我知道怎么做,如果两者都是精灵,而不是如果一个是由DrawNode绘制的。

1 个答案:

答案 0 :(得分:1)

当在DrawNode中绘制点时 - 它只是在画布上绘制,之后您无法单独访问它的几何体。你可以做的是将几何存储在一个单独的数组中:

var dotGeometry = [];

this.dot = new cc.DrawNode.create();
this.dot.drawDot(cc.p(
this.size.width/2, 
this.size.height/2), 
100, 
cc.color(12, 156, 194, 100));

dotGeometry.push( cc.rect(this.size.width/2-50, this.size.height/2-50, 100, 100) );

(我使用cc.rect进行矩形交叉,你可以改变形状以使其更准确) 然后你可以像往常一样使用cc.rectIntersectsRect(cc.rect只是一个几何表示(4个值)所以它不会影响性能/浪费大量内存)