Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I draw a line using Cocos2D-X?

I have been playing around with Cocos2D-X on my computer, and I have got it to build the hello world program on all the devices I would like to be able to build it on.

I know how to make the program display a sprite, and display a label, but I have not been able just to get the program to draw a line. How can I draw a line in Cocos2D-X?

like image 394
Jon Avatar asked Jul 19 '12 12:07

Jon


People also ask

How do I download Cocos2d-X?

To download Cocos2d-x, go to http://www.Cocos2d-x.org/download and download Version 2.2. 3 from the website. Once downloaded, you can unzip the Cocos2d-x-2.2.


2 Answers

use void ccDrawLine(const CCPoint& origin, const CCPoint& destination) function declared in CCDrawingPrimitives.h

Edit

I've never tried using primitives myself. But as I know everything in cocos2d is rendered vis CCNode or it's subclass. So you must put your code inside draw method of some CCNode or it's subclass.

like image 134
Andrew Avatar answered Sep 22 '22 21:09

Andrew


I have found another easy way to draw line in CCLayer. Cocos2d-x has a class named CCDrawNode. You can check reference here. And it is very easy to use the function:

void drawSegment(const CCPoint & from,
    const CCPoint & to,
    float   radius,
    const ccColor4F & color 
)

Little example:

CCDrawNode* node = CCDrawNode::create();
addChild(node,10);//Make sure your z-order is large enough
node->drawSegment(fromPoint,toPoint,5.0,ccc4f(180,180,180,100));
like image 37
einverne Avatar answered Sep 25 '22 21:09

einverne