API differences between JSB and HTML5

API differences between JSB and HTML5
0.0 0



Is there any guide illustrating API differences between JSB and HTML5. Is it totally guaranteed that single code base will work on both? Documentation says ‘a little changes may be needed’. then which kind of changes are needed? I am writing this because I encountered some situations when something worked on web, but not on win32 target. With a more detail in my personal game project clippingnode works on web as expected, but not on win32. I can neither trace the problem nor debug it for win32. Below code even works when it is firstly called (for win32), then during the game, player somehow updates MG.Rail then draw_rail function called second time, and this.fg1 (sprite to be clipped) disappears totally and game starts to behave strage like a silently corruption without crash. Only clue I have is that MG.pVertices is not cleared as expected when the game corrupts.

using cocos2d-x 3.14

draw_rail: function(){
    var triangles = triangulate(MG.Rail);
    var n_tri = triangles.length/3;

    // this.stencil = new cc.DrawNode(); // stencil was defined in ctor
    // this.clipper = new cc.ClippingNode(); // clipper was defined in ctor
    for(i=0; i<n_tri; i++){
        var a_triangle = [cc.p(MG.Rail[triangles[3*i]]), cc.p(MG.Rail[triangles[3*i+1]]), cc.p(MG.Rail[triangles[3*i+2]])];
        this.stencil.drawPoly(a_triangle, cc.color(0,255,0,255), 0,  cc.color(0,255,0,255));

    //this.clipper.setStencil(stencil); //set in ctor
    //this.clipper.setAnchorPoint(0.5, 0.5); // set in ctor
    //this.clipper.setPosition(0, 0); // set in ctor


    MG.pVertices = [];
    MG.direction = '';


I have solved the issue related with clipping node. As soon as a node or sprite created using new keyword, I call retain() function and it worked. I think that retain must be called for JSB target. for instance:

    this.fg1 = new cc.Sprite(res.Fg_png); 

    this.stencil = new cc.DrawNode();

    this.clipper = new cc.ClippingNode(); 

I think you also have to release them in somewhere like:

var sprite = new cc.Sprite(some_resource);
// use it for something


That is a common thing for JSB. JavaScript memory model and cocos2d-x memory model are different. And all nodes, that are not retained nor addChild’ed, will be destroyed on the next update(). Storing link on them in private field won’t work!