Cocos2d-x3.0 RenderTexture(三)

时间:2023-03-10 06:04:02
Cocos2d-x3.0 RenderTexture(三)

.h

#include "cocos2d.h"
#include "cocos-ext.h"
#include "ui/CocosGUI.h"
#include "cocostudio/CocoStudio.h"
USING_NS_CC; USING_NS_CC_EXT;
using namespace ui;
private:
cocos2d::SpriteBatchNode *mgr;; cocos2d::Sprite *sp1;
cocos2d::Sprite *sp2;
cocos2d::Sprite *sp3;
cocos2d::Sprite *sp4;
cocos2d::Sprite *sp5;
cocos2d::Sprite *sp6;
cocos2d::Sprite *sp7;
cocos2d::Sprite *sp8;
cocos2d::Sprite *sp9;

.cpp

    auto listener = EventListenerTouchAllAtOnce::create();

        auto label = Label::createWithTTF("vertexZ = 50", "Marker Felt.ttf", 64);
label->setPosition(Point(size.width / 2, size.height * 0.25f));
this->addChild(label); auto label2 = Label::createWithTTF("vertexZ = 0", "Marker Felt.ttf", 64);
label2->setPosition(Point(size.width / 2, size.height * 0.5f));
this->addChild(label2); auto label3 = Label::createWithTTF("vertexZ = -50", "Marker Felt.ttf", 64);
label3->setPosition(Point(size.width / 2, size.height * 0.75f));
this->addChild(label3); label->setPositionZ(50);
label2->setPositionZ(0);
label3->setPositionZ(-50); SpriteFrameCache::getInstance()->addSpriteFramesWithFile("circle.plist");
mgr = SpriteBatchNode::create("circle.png", 9);
this->addChild(mgr);
auto sp1 = Sprite::createWithSpriteFrameName("circle.png");
sp2 = Sprite::createWithSpriteFrameName("circle.png");
sp3 = Sprite::createWithSpriteFrameName("circle.png");
sp4 = Sprite::createWithSpriteFrameName("circle.png");
sp5 = Sprite::createWithSpriteFrameName("circle.png");
sp6 = Sprite::createWithSpriteFrameName("circle.png");
sp7 = Sprite::createWithSpriteFrameName("circle.png");
sp8 = Sprite::createWithSpriteFrameName("circle.png");
sp9 = Sprite::createWithSpriteFrameName("circle.png"); mgr->addChild(sp1, 9);
mgr->addChild(sp2, 8);
mgr->addChild(sp3, 7);
mgr->addChild(sp4, 6);
mgr->addChild(sp5, 5);
mgr->addChild(sp6, 4);
mgr->addChild(sp7, 3);
mgr->addChild(sp8, 2);
mgr->addChild(sp9, 1); sp1->setPositionZ(400);
sp2->setPositionZ(300);
sp3->setPositionZ(200);
sp4->setPositionZ(100);
sp5->setPositionZ(0);
sp6->setPositionZ(-100);
sp7->setPositionZ(-200);
sp8->setPositionZ(-300);
sp9->setPositionZ(-400); sp9->setScale(2);
sp9->setColor(Color3B::YELLOW); //[=]能够掉用外部变量
listener->onTouchesBegan = [=](const std::vector<Touch *> &touches, cocos2d::Event *unused_event)
{
for (auto &item:touches) {
auto touch = static_cast<Touch*>(item);
auto location = touch->getLocation(); sp1->setPosition(location);
sp2->setPosition(location);
sp3->setPosition(location);
sp4->setPosition(location);
sp5->setPosition(location);
sp6->setPosition(location);
sp7->setPosition(location);
sp8->setPosition(location);
sp9->setPosition(location);
}
}; listener->onTouchesMoved = [=](const std::vector<Touch*>& touches,Event* unused_event)
{
for (auto &item:touches) {
auto touch = static_cast<Touch*>(item);
auto location = touch->getLocation(); sp1->setPosition(location);
sp2->setPosition(location);
sp3->setPosition(location);
sp4->setPosition(location);
sp5->setPosition(location);
sp6->setPosition(location);
sp7->setPosition(location);
sp8->setPosition(location);
sp9->setPosition(location);
} }; listener->onTouchesEnded = [=](const std::vector<Touch *> &touches, cocos2d::Event *unused_event){
auto texture = RenderTexture::create(512, 512);
if (NULL == texture)
{
return;
}
texture->setAnchorPoint(Point(0, 0));
texture->setKeepMatrix(true);
texture->begin(); this->visit(); texture->end(); auto sprite = Sprite::createWithTexture(texture->getSprite()->getTexture()); sprite->setPosition(Point(256, 256));
sprite->setOpacity(182);
sprite->setFlippedY(1);
this->addChild(sprite, 999999);
sprite->setColor(Color3B::GREEN); sprite->runAction(Sequence::create(FadeTo::create(2, 0),
Hide::create(),
NULL)); }; _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);

Cocos2d-x3.0 RenderTexture(三)