0

I made a drawNode to draw primitive using this code:

    var.drawNode = cc.DrawNode.create();
    drawNode.drawSegment(this.pos, cc.p(this.pos.x + this.length * Math.sin(this.rotation), this.pos.y + this.length * Math.cos(this.rotation)), STICK_THICKESS, cc.color(255,255,0,255));

It basically draws a line from this.pos to another point.

Now I want to rotate the line around this.pos, so I thought I just need to simply add this:

    drawNode.setAnchorPoint(this.pos);
    var rotate = cc.RotateBy.create(2, 360);
    drawNode.runAction(rotate);

But it's still rotating around some random point.

Hieu Phan
  • 581
  • 3
  • 7
  • 14
  • anchor point is not a position but a factor (typically) in the range of 0 to 1 – CodeSmile Jul 15 '14 at 12:25
  • so what exactly is it for then? a point being a factor, this is so confusing :( – Hieu Phan Jul 15 '14 at 15:06
  • ok i did a google search and this really helped http://www.qcmat.com/understanding-anchorpoint-in-cocos2d/ – Hieu Phan Jul 15 '14 at 15:12
  • coincidentally this also happens to be my 2nd most upvoted answer, with more suggestions on when and when not to modify anchorPoint: http://stackoverflow.com/questions/7808981/moving-a-stick-figure-anchorpoints-animation-or-something-else/7810180#7810180 – CodeSmile Jul 15 '14 at 16:22
  • Thank you @LearnCocos2D I now understand what anchor point really does. So I have changed my code so that drawNode is added as a child of a ccNode which is positioned at the point where I want drawNode to be rotated around. Is that the right way to do it because my code is still not working... – Hieu Phan Jul 18 '14 at 07:34

1 Answers1

1

Ugly but working method:

drawNode.setContentSize(1, 1);
drawNode.setAnchorPoint(this.pos);
drawNode.setPosition(this.pos);
var rotate = cc.RotateBy.create(2, 360);
drawNode.runAction(rotate);

BTW create methods are deprecated in Cocos2d-html5 3.0+. Use cc.rotateBy() instead of cc.RotateBy.Create(), new cc.DrawNode() instead of cc.DrawNode.create()

musikov
  • 640
  • 1
  • 4
  • 13