关于cocos2d-x 3.3网格特效笔记

本文记录了cocos2d-x 3.3版本中网格特效的实现,包括PageTurn3D、Shaky3D、FlipX3D等特效的代码示例,并提醒读者注意与2.x版本的函数参数差异,同时提到了在3.3中使用Vec2参数的错误及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

记录一些关于网格特效的笔记,在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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值