【问题标题】:Collision detection between sprite and drawDot? (javascript)sprite和drawDot之间的碰撞检测? (javascript)
【发布时间】:2016-01-14 16:31:54
【问题描述】:

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

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 绘制的,我知道该怎么做。

【问题讨论】:

    标签: cocos2d-x cocos2d-js


    【解决方案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个值),所以它不会影响性能/浪费很多内存)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多