I am new to cocos2d-x.
I am not able to rotate a line about its center, its rotating around some other point.
auto laser = DrawNode::create();
laser->drawLine(Vec2(100,100),Vec2(150,100), cocos2d::Color4F(100,150,150,1));
laser->setAnchorPoint(Vec2(0.5,0.5));
this->addChild(laser);
laser->runAction(RepeatForever::create(RotateBy::create(5.0f, 360.0f)));
How to make it rotate about its center and its end?
I thought setAnchorPoint would help me with both, like for rotation about one end I could
laser->setAnchorPoint(Vec2(1,0));
but its not working.
Hey,
Your code is not working because you rotate relative to the DrawNode position which in this case is at (0,0).
In order to get what you want you need to set the position of your DrawNode at the middle of the line you want to draw and then draw the line relative to this position.
So in this case:
auto laser = DrawNode::create();
laser->setPosition(Vec2(125, 100));
laser->drawLine(Vec2(-25,0),Vec2(25,0), cocos2d::Color4F(100,150,150,1));
this->addChild(laser);
laser->runAction(RepeatForever::create(RotateBy::create(5.0f, 360.0f)));
devilator:
auto laser = DrawNode::create();
laser->setPosition(Vec2(125, 100));
laser->drawLine(Vec2(-25,0),Vec2(25,0), cocos2d::Color4F(100,150,150,1));
this->addChild(laser);
laser->runAction(RepeatForever::create(RotateBy::create(5.0f, 360.0f)));
Thank you so much! I didn’t know we should move the DrawNode!!