本文转载:http://www.areskill.com/?p=356
Cocos2d-x 截屏 3.2以上版本
使用utils::captureScreen 截屏,然后回调
auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = [this](Touch *touch,Event *event){
capture(nullptr);
return false;
};
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);
//屏幕截图
@H_404_29@void HelloWorld::capture(Ref* sender) {
log("ScreenShot");
utils::captureScreen(CC_CALLBACK_2(HelloWorld::afterCapture,this),"ScreenShot.png");
}
//截图后执行afterCapture
@H_404_29@void HelloWorld::afterCapture(bool succeed,@H_404_29@const std::string& outputFile) {
if (succeed) {
log("%s",outputFile.c_str());
//显示截图
Sprite* sp = Sprite::create(outputFile);
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
sp->setPosition(Vec2(visibleSize.width/2+origin.x,visibleSize.height/2+origin.y));
this->addChild(sp);
sp->setScale(0.5); //放缩
}
else {
log("Capture screen Failed.");
}
}