Environment:
cocos2d-x 3.14.1
Mac / Chrome Version 56.0.2924.87 (64-bit)
Mac / Safari Version 9.0.1 (11601.2.7.2)
webgl mode
It is hard to describe the problem I can only say that If I try to use a cc.Drawnode instance to draw something each frame and what I draw will become a mess randomly. And it seems to have something to do with using multi cc.DrawNode instance in the game and draw something with them at the same time.
I know that drawPoly can can only handle the Convex poly so I only test with Convex poly.
And the most important thing is the demo works fine with cocos2d-x 3.13.1 and cocos2d-x 3.12 !
So it seem like it is a bug caused in the latest version of cocos2d-x since I found this point in the change log:
cocos2d-x-3.14 Dec 22 2016
[REFINE] Web: Reimplement a much faster cc.DrawNode WebGL renderer
I have uploaded a simple demo in the attachment, I hope you guys can run and test it I think it is easy to reproduce the problem.
I have also uploaded two gif two show how to reproduce the problem. (One named 3.14.1.gif shows the problem and the one named 3.13.1.gif shows it works fine doing the same thing in cocos2d-x 3.13.1)
cocos2d-x 3.13.1:
cocos2d-x 3.14.1
js-template-default.zip (1.5 MB)
sample code:
this._shapeEditorVerticesArray = [];
this._drawNodeVertices = new cc.DrawNode(); // all the vertexs
this._drawNodeEdge = new cc.DrawNode(); // the current edge
this._drawNodeEdges = new cc.DrawNode(); // all the edges
this._drawNodePoly = new cc.DrawNode(); // draw the poly
this.addChild(this._drawNodePoly);
this.addChild(this._drawNodeVertices);
this.addChild(this._drawNodeEdge);
this.addChild(this._drawNodeEdges);
var self = this;
this._shapeEditorEvent = cc.eventManager.addListener({
event: cc.EventListener.TOUCH_ONE_BY_ONE,
swallowTouches: true,
onTouchBegan: function (touch, event) {
var location = touch.getLocation();
if (self._shapeEditorVerticesArray.length == 0) {
self._drawNodeVertices.drawDot(location, 5, cc.color.GREEN);
self._shapeEditorVerticesArray.push(location);
}
else {
self._drawNodeEdge.drawSegment(self._shapeEditorVerticesArray[self._shapeEditorVerticesArray.length - 1], location, 3, cc.color.YELLOW);
}
self._plot(location);
return true;
},
onTouchMoved: function (touch, event) {
if (self._shapeEditorVerticesArray.length > 0) {
var location = touch.getLocation();
self._drawNodeEdge.clear();
self._drawNodeEdge.drawSegment(self._shapeEditorVerticesArray[self._shapeEditorVerticesArray.length - 1], location, 3, cc.color.YELLOW);
self._plot(location);
}
},
onTouchEnded: function (touch, event) {
if (self._shapeEditorVerticesArray.length > 0) {
var location = touch.getLocation();
//
self._drawNodeEdge.clear();
self._drawNodeEdges.drawSegment(self._shapeEditorVerticesArray[self._shapeEditorVerticesArray.length - 1], location, 3, cc.color.YELLOW);
self._drawNodeVertices.drawDot(location, 5, cc.color.GREEN);
self._shapeEditorVerticesArray.push(location);
self._plot();
}
}
}, this);
_plot:function(position) {
if (this._shapeEditorVerticesArray.length > 2 || (position && this._shapeEditorVerticesArray.length > 1)) {
this._drawNodePoly.clear();
if (position) {
var ppp = this._shapeEditorVerticesArray.slice();
ppp.push(position);
this._drawNodePoly.drawPoly(ppp, cc.color.RED, 0, cc.color(0, 0, 0, 0));
}
else {
this._drawNodePoly.drawPoly(this._shapeEditorVerticesArray, cc.color.RED, cc.FLT_EPSILON, cc.color(0, 255, 0, 255));
}
}
}