spritepolygoncocos2d-js

Drawing a Polygon and use that as a sprite in cocos2d js


I'm trying to use cocos2d js. While I was working I needed to draw a polygon which I've already done using DrawNode. But now I need to run different actions on that polygon such we run actions on sprites. I was searching for any way to run actions on the polygon but couldn't be able to do so.

Please help if anybody knows how to work with polygons as sprites.


Solution

  • You have to apply the actions to the DrawNode that contains the polygon. In this example, I draw a polygon and then I rotate it by 10 degrees each second (pivoting on the default anchor point 0,0).

    var dn = new cc.DrawNode();
    this.addChild(dn, 500);
    dn.drawPoly([cc.p(50,50), cc.p(100, 70), cc.p(110, 100), cc.p(120, 80), cc.p(70, 40)], cc.p(500,500),  cc.color(249,255,115), 100,  cc.color(249,255,115));
    dn.runAction(
        cc.repeatForever(
            cc.rotateBy(1, 10)
        )
    );