记录一些关于网格特效的笔记,在3.x与2.x中,网格特效这块区别还是挺大的,下面是一些笔记和代码:
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size size= Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + size.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
auto label = Label::createWithTTF("Hello World", "fonts/Marker Felt.ttf", 24);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + size.width/2,
origin.y + size.height - label->getContentSize().height));
// add the label as a child to this layer
this->addChild(label, 1);
// add "HelloWorld" splash screen"
auto sprite = Sprite::create("HelloWorld.png");
// position the sprite on the center of the screen
sprite->setPosition(Vec2(size.width/2 + origin.x, size.height/2 + origin.y));
// add the sprite as a child to this layer
//this->addChild(sprite, 0);
//=================================================================================================================
//创建一个网格节点
NodeGrid * nodegrid= NodeGrid::create();
//指定其位置
nodegrid->setPosition(Vec2::ZERO);
//把所需特效的节点添加到网格节点中(所需特效的节点不能先添加到当前节点中,否则出错)
nodegrid->addChild(sprite);
//把网格节点添加到节点中
this->addChild(nodegrid);
Director::getInstance()->setDepthTest(false); //关闭深度检测
// Director::getInstance()->setDepthTest(true); //开启深度检测
// //翻页效果
// auto pageturn = PageTurn3D::create(2, Size(20, 18));
// nodegrid->runAction(pageturn);
// //震动效果
// auto shaky = Shaky3D::create(6, Size(2, 1), 5, false);
// nodegrid->runAction(shaky);
// //翻转效果
// auto flipX = FlipX3D::create(2);
// auto reFlipX = flipX->reverse();
// auto delay = DelayTime::create(2); //延时2秒
// nodegrid->runAction(Sequence::create(flipX, delay, reFlipX, NULL));
// //透镜效果
//参数依次为:持续时间、网格大小、中心坐标、半径
// auto lens = Lens3D::create(5, Size(32, 24), Size(size.width/2, size.height/2), 300);
//nodegrid->runAction(lens);
// //液体效果
//参数依次为:持续时间、网格大小、波纹数、振幅
// auto liquid = Liquid::create(3, Size(16, 12), 4, 10); //参数依次为:持续时间、网格大小、波纹数、振幅
// nodegrid->runAction(liquid);
// //波纹效果
// auto ripple = Ripple3D::create(5, Size(20, 16), Size(size.width/2, size.height/2), 300, 6, 50);
// nodegrid->runAction(ripple);
// //波浪效果
// auto wave = Waves3D::create(5, Size(25, 20), 6, 30);
// nodegrid->runAction(wave);
// 更多效果可以参考http://blog.csdn.net/jskafkashd/article/details/51273060
// 但是改参考的例子是用coco2d-x 2.x 版本,所以函数名去掉CC其他不变,其中参数有所变化,
// 但变化不大,只是位置改动和增减一些参数,可以看原定义了解一下就好了。
// 注意:在3.3中使用Vec2在网格特效函数参数中使用时,会出错,出错类型是Vec2无法转换成const Vec2,
//所以我这里使用的是Size来代替。
//创建一个反转特效
auto * pFlipX3d = FlipX3D::create(2.0f);
//网格节点运行创建的特效
nodegrid->runAction(pFlipX3d);
//计时器的运用,schedule_selector接受一个带float参数的回调函数。
schedule(schedule_selector(HelloWorld::schedule_function));
//===========================================================================================================
return true;
}
//计时器所调用的函数
void HelloWorld::schedule_function(float dt)
{
CCLOG("1111111111111111");
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}