加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-js 把JS错误打印到屏幕上

发布时间:2020-12-14 17:10:59 所属栏目:百科 来源:网络整理
导读:在编程或者QA测试过程中,把debug的包中JS错误打印在屏幕上可以增加开发效率,降低定位bug时间成本。 修改ScriptingCore.cpp文件: void ScriptingCore::reportError(JSContext *cx,const char *message,JSErrorReport *report){ js_log("%s:%u:%sn",report

在编程或者QA测试过程中,把debug的包中JS错误打印在屏幕上可以增加开发效率,降低定位bug时间成本。

修改ScriptingCore.cpp文件:

void ScriptingCore::reportError(JSContext *cx,const char *message,JSErrorReport *report)
{
    js_log("%s:%u:%sn",report->filename ? report->filename : "<no filename="filename">",(unsigned int) report->lineno,message);

	// xiaohei add error layer
	ScriptingCore::getInstance()->showErrorLayer();
};

void ScriptingCore::showErrorLayer()
{
	int delv = 0;
	auto isDelv = localStorageGetItem("xh_error");
	if (!isDelv.empty()) {
		delv = atoi(isDelv.c_str());
	}

	if (_js_log_buf && delv > 0) {
		auto winSize = Director::getInstance()->getWinSize();
		auto errLayer = LayerColor::create();
		errLayer->initWithColor(Color4B(0,0),winSize.width,winSize.height);
		errLayer->setAnchorPoint(Point(0.5f,0.5f));
		errLayer->setPosition(Point(0,0));
		errLayer->setOpacity(200);
		Director::getInstance()->getRunningScene()->addChild(errLayer,10000000,952700);

		auto listener = EventListenerTouchOneByOne::create();
		listener->onTouchBegan = CC_CALLBACK_0(ScriptingCore::onErrorLayerTouchBegin,this);
		listener->setSwallowTouches(true);
		Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,errLayer);

		std::string errlog = _js_log_buf;
		auto errorLabel = Label::createWithTTF(errlog,"common/font/DFGB_Y7_0.ttf",18);
		errorLabel->setColor(Color3B::WHITE);
		errorLabel->setAnchorPoint(Point(0.5f,0.5f));
		errorLabel->setPosition(Point(winSize.width / 2.0,winSize.height / 2.0 + 150));
		errLayer->addChild(errorLabel,10);

		std::string path = "res/ui/common/common_close.png";
		auto swapMenuItem = MenuItemImage::create(path,path,CC_CALLBACK_0(ScriptingCore::removeErrorLayer,this));
		auto swapMenu = Menu::createWithItem(swapMenuItem);
		swapMenu->setPosition(winSize.width - 100,winSize.height - 100);
		errLayer->addChild(swapMenu,100);
	}
}

bool ScriptingCore::onErrorLayerTouchBegin()
{
	return true;
}

void ScriptingCore::removeErrorLayer()
{
	Director::getInstance()->getRunningScene()->removeChildByTag(952700);
}
重写reportError把错误展示在屏幕上,并支持关闭操作

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读