3

I am new to Cocos 2d js..... I want to know how can I draw a rectangle having a border to it using cocos2d js??.. I tried to google but didn't find any sample code or something similar.. which is quite simple to do using HTML and CSS... Thanks.

kaushik reddy
  • 167
  • 3
  • 16

1 Answers1

5

Yo need to add a draw node to your scene/layer and draw a rectangle on it. For example, say you have the following method within your layer:

{
  ...
  var dn = new cc.DrawNode();
  this.addChild(dn);
  dn.drawRect(cc.p(50,50), cc.p(200,300), cc.color(255,0,0,255), 3, cc.color(0,255,0,255));
  ...
}

The function call parameters are: drawRect(origin, destination, fillColor, lineWidth, lineColor).

This is from the samples found in the samples/js-tests folder that should be in your cocos2d-js folder. For more information, check out the API on the drawing nodes here: http://www.cocos2d-x.org/reference/html5-js/V3.3/symbols/cc.DrawNode.html

PS: if you want to draw a filled circle with a line color, however, note that there's not a function for that currently. There are a few workarounds, the best one I've found is to use a drawDot for the "inner solid part" of the circle, and a drawCircle for the outer part.

Sebastián Vansteenkiste
  • 2,234
  • 1
  • 19
  • 29
  • That works fine....How do I run the app on an emulator???Insetead of using my device evrytime.@Sebastián Vansteenkiste – kaushik reddy Mar 16 '15 at 06:28
  • Assuming you have cocos console installed, open a command prompt in your project's folder and type `cocos run -p web`. That'll run the game in your default internet browser. As for how to run on an emulator.. That's a bit too broad a question for a comment I think. (Unless `cocos run -p android` does the trick, of course) – Sebastián Vansteenkiste Mar 17 '15 at 04:38
  • ya thanks...I started using cocos ide...and it fixed all my issues...How do i do collision detection between two nodes...the documentation for collide detect isnt that great to go with..??? @Sebastián Vansteenkiste – kaushik reddy Mar 17 '15 at 06:53
  • You should really check out the official forums. Are you trying to use a physics engine? Or just plain sprites? – Sebastián Vansteenkiste Mar 17 '15 at 20:44
  • I am just using 2 triangles made using drawPoly(), between which i wanna find a collision detection? @Sebastián Vansteenkiste – kaushik reddy Mar 18 '15 at 01:33