cocos2dx实现简单卡牌翻转效果

将两张图片放入图层,将其中一个图片x的比例设置为0,即隐藏起来。
分别给两个图片绑定点击监听事件,事件触发后执行缩放动作。将自身x比例设置为0,完成后将另一张图片x比例设置为1。
详见代码注释。

bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();

    //使用两张图片分别创建精灵
    auto logo1 = Sprite::create("up.jpg");
    auto logo2 = Sprite::create("down.jpg");
    //设置居中
    logo1->setPosition(visibleSize/2);
    logo2->setPosition(visibleSize/2);
    //将第二张图片x轴缩放至隐藏
    logo2->setScale(0, 1);
    //加入图层
    addChild(logo1);
    addChild(logo2);

    //创建监听器
    auto touchListener1 = EventListenerTouchOneByOne::create();
    //设置点击事件
    touchListener1->onTouchBegan = [logo1, logo2](Touch* touch, Event* event){
    //判断点击位置是否在精灵显示范围内
        if(event->getCurrentTarget()->getBoundingBox().containsPoint(
                        touch->getLocation()))
        {
        //执行缩放动作
            auto move1 = ScaleTo::create(1, 0, 1);
            auto move2 = ScaleTo::create(1, 1, 1);
            logo1->runAction(move1);
            logo2->runAction(move2);
        }
        return false;
    };
    //同上
    auto touchListener2 = EventListenerTouchOneByOne::create();
    touchListener2->onTouchBegan = [logo1, logo2](Touch* touch, Event* event){
        if(event->getCurrentTarget()->getBoundingBox().containsPoint(
                                                                     touch->getLocation()))
        {
            auto move1 = ScaleTo::create(1, 0, 1);
            auto move2 = ScaleTo::create(1, 1, 1);
            logo2->runAction(move1);
            logo1->runAction(move2);
        }
        return false;
    };

    //分别将两个监听器与两个精灵绑定
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener1, logo1);
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchListener2, logo2);

    return true;
}

cocos2dx实现简单卡牌翻转效果
https://shiyi.threebody.xyz/posts/25241.html
作者
Yi Shi
发布于
2016年7月23日
许可协议