第一个cocos 项目问题总结

1、物理引擎碰撞产生两边数据不同步问题?
2、android原生截图黑屏,cocos 截图存储位置问题?
3、android重复播放音乐不连贯中间有空隙问题?

[1] 导致问题1出现的原因是moveTo函数导致位置精度不准从而引起碰撞位置,碰撞反弹角度等出现偏差从而导致碰撞结果不同,解决办法,在Canvas层同级新建一个物理碰撞组建存放图层,该图层为实际碰撞图层但显示为透明区,然后根据碰撞位置映射到显示层,在碰撞层直接设置位置,显示层执行移动函数。
[2]导致问题2的原因为cocos使用的是SurfaceView从而普通截屏无法使用截屏为黑屏,在Cocos2dxRenderer类 onDrawFrame方法了里使用 private static Bitmap getBitmapFromGL(int w, int h, GL10 gl) {
int b[] = new int[w * (h)];
int bt[] = new int[w * h];
IntBuffer ib = IntBuffer.wrap(b);
ib.position(0);
gl.glReadPixels(0, 0, w, h, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);

    for (int i = 0, k = 0; i < h; i++, k++) {
        for (int j = 0; j < w; j++) {
            int pix = b[i * w + j];

// Log.d("-------------","--------------截图"+pix);
int pb = (pix >> 16) & 0xff;
int pr = (pix << 16) & 0xffff0000;
int pix1 = (pix & 0xff00ff00) | pr | pb;
bt[(h - k - 1) * w + j] = pix1;
}
}

    return Bitmap.createBitmap(bt, w, h, Bitmap.Config.ARGB_8888);
} 实现截屏成功但华为手机还是黑屏,后来使用cocos截图   HMFUtils.captureScreen = function (canvasNode) {
    //注意,EditBox,VideoPlayer,Webview 等控件无法截图

    var size =  cc.director.getVisibleSize()
    var h = size.height;
    var w = size.width;
    if(CC_JSB) {
        //如果待截图的场景中含有 mask,请开启下面注释的语句
        var renderTexture = cc.RenderTexture.create(w,h, cc.Texture2D.PIXEL_FORMAT_RGBA8888, gl.DEPTH24_STENCIL8_OES);
        // var renderTexture = cc.RenderTexture.create(750,1280);

        //把 renderTexture 添加到场景中去,否则截屏的时候,场景中的元素会移动
        canvasNode.parent._sgNode.addChild(renderTexture);
        //把 renderTexture 设置为不可见,可以避免截图成功后,移除 renderTexture 造成的闪烁
        renderTexture.setVisible(false);

        //实际截屏的代码
        renderTexture.begin();
        //this.richText.node 是我们要截图的节点,如果要截整个屏幕,可以把 this.richText 换成 Canvas 切点即可
        canvasNode._sgNode.visit();
        renderTexture.end();
        renderTexture.saveToFile("/sdcard/xxx.jpg",cc.ImageFormat.JPG, true, function () {
            //把 renderTexture 从场景中移除
            renderTexture.removeFromParent();
        });
        //打印截图路径
    }
} 实现截图成功 注意修改存储图片路径将CCRenderTexture.cpp中saveToFile方法中std::string fullpath = FileUtils::getInstance()->getWritablePath() + fileName更换成std::string fullpath = fileName即可/sdcard/xxx.jpg为存放图片路径及图片名称和格式

[3]android重复播放问题需更改音频格式为ogg格式,并修改AudioPlayerProvider.cpp中static AudioFileIndicator __audioFileIndicator[] = {
{“default”, 128000}, // If we could not handle the audio format, return default value, the position should be first.
{".wav", 1024000},
{".ogg", 900000},
{".mp3", 900000}
}将空间修改大点即可。