Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d-x3.6 连连看触摸事件传递大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

@H_197_2@我的博客:@L_607_0@

上一篇基本已经实现了连连看的整个游戏。
这一篇讲加一个倒计时和一个Game Over的界面,当游戏结束的时候弹出Game Over的对话框,还可以Play again。

倒计时很简单,就是一个时间进度条。

// 时间条背景框
    auto progressFrame = Sprite::createWithTexture(textureCache->getTextureForKey(s_time_slot));
    // 锚点,左下角
    progressFrame->setAnchorPoint(Vec2(0,0));

    progressFrame->setPosition(120,wSize.height-50);

    addChild(progressFramE);

    // 时间条精灵
    auto pSprite = Sprite::createWithTexture(textureCache->getTextureForKey(s_time_bars));

    progress = ProgressTimer::create(pSpritE);
    // 锚点,左下角
    progress->setAnchorPoint(Vec2(0,0));
    // 类型,条形
    progress->setType(ProgressTimer::Type::BAR);

    progress->setPosition(120,wSize.height - 50);
    // 水平变化
    progress->setMidpoint(Vec2(0,0));
    // 一次一个单位
    progress->setBarChangeRate(Vec2(1,0));
    // 初始100
    progress->setPercentage(100);

    addChild(progress);

    // 时间数字
    numberTime = Label::createWithSystemFont("100","Thonburi",24);

    numberTime->setAnchorPoint(Vec2(0,0));

    numberTime->setPosition(400,wSize.height - 50);

    numberTime->setColor(Color3B::BLACK);

    addChild(numberTimE);

然后倒计时每一秒减少一格,每秒调一次update()函数

schedule(SEL_scheDULE(&GameScene::@H_674_133@update),1.0);

来看update函数:

void GameScene::update(float dt)
{
    // 当前进度
    int current = progress->getPercentage();
    // 为零就gameover
    if (current == 0) {
        // game over
        gameOver();
    }else{
        // 减一
        current--;
        progress->setPercentage(current);
        char time[] = {25};
        sprintf(time,"%d",current);
        numberTime->setString(timE);
    }
}

很简单的,获取当前时间条的值,减一,如果为0就调game Over。

来看gameOver函数:

void GameScene::gameOver()
{
    // 停止update函数调度。
    unschedule(SEL_scheDULE(&GameScene::update));


    // 创建一个颜色层
    auto layer = LayerColor::create(Color4B(100,100,100));
    // 锚点默认是左下角
    layer->setPosition(0,0);
    addChild(layer,1000);

    // 对话框背景
    auto sprite = Sprite::createWithTexture(Director::geTinstance()->getTextureCache()->getTextureForKey(s_game_dialog));
    sprite->setPosition(wSize.width / 2,wSize.height / 2);
    layer->addChild(spritE);

    // Game Over 提示
    auto title = Label::createWithSystemFont("Game Over",35);
    title->setPosition(layer->getContentSize().width / 2,sprite->getContentSize().height - 100);
    title->setColor(Color3B::rED);
    layer->addChild(titlE);

    // Font Item
    MenuItemFont::setFontName("fonts/Marker Felt.ttf");
    // Play Again 按钮
    auto item1= MenuItemFont::create("Play Again",CC_CALLBACK_0(GameScene::playAgain,this) );
    auto color_action = TintBy::create(0.5f,0,-255,-255);
    auto color_BACk = color_action->reverse();
    auto seq = Sequence::create(color_action,color_BACk,nullptr);
    item1->runAction(RepeatForever::create(seq));

    auto menu = Menu::create(item1,nullptr);

    menu->setPosition(layer->getContentSize().width / 2,sprite->getContentSize().height / 2);

    layer->addChild(menu);

    // Game Over层添加触摸事件
    auto dispatcher = Director::geTinstance()->getEventDispatcher();
    auto listener = EventListenerTouchOneByOne::create();
    // 吞掉触摸事件,即下面的层不会响应此次触摸
    listener->setSwallowTouches(true);
    // Lambda表达式实现触摸函数,空的,因为不需要做触摸实现
    listener->onTouchBegan = [](Touch*touch,Event* event){return true;}; listener->onTouchMoved = [](Touch*touch,Event* event){}; listener->onTouchEnded = [](Touch*touch,Event* event){}; listener->onTouchCancelled = [](Touch*touch,Event* event){}; // 加到GameOver层 dispatcher->addEventListenerWithSceneGraphpriority(listener,layer); }

这里有一点要注意,我这个GameScene本身是一个Layer,然后里面的GameOver页面又是一个Layer。就是一个场景里面有两个层。

前面我们在init()里面添加的触摸事件并不能解决这里的问题,GameOver层弹出来以后,点击屏幕,连连看里面的小图标还能被点中,会动的。看init()里面的触摸事件:

auto dispatcher = Director::geTinstance()->getEventDispatcher();
    auto listener = EventListenerTouchOneByOne::create();
    listener->setSwallowTouches(true);
    listener->onTouchBegan = CC_CALLBACK_2(GameScene::onTouchBegan,this);
    listener->onTouchMoved = CC_CALLBACK_2(GameScene::onTouchMoved,this);
    listener->onTouchEnded = CC_CALLBACK_2(GameScene::onTouchEnded,this);
    listener->onTouchCancelled = CC_CALLBACK_2(GameScene::onTouchCancelled,this);
    dispatcher->addEventListenerWithSceneGraphpriority(listener,this);

这是init()里面的触摸,可以看到设置了触摸事件吞噬,listener->setSwallowTouches(true),然并卵。为什么:

在一个场景里面,如果有多个层,所有的层都必需要注册了触控事件才能进行控制。可以看到这里只是注册了当前层。所以如果只有initi()里面的触摸函数设置,是不能满足需求的。我需要当GameOver层弹出来的时候,后面的层不能被点击。

所以我们的GameOver也需要注册触摸事件:

// Game Over层添加触摸事件
    auto dispatcher = Director::geTinstance()->getEventDispatcher();
    auto listener = EventListenerTouchOneByOne::create();
    // 吞掉触摸事件,即下面的层不会响应此次触摸
    listener->setSwallowTouches(true);
    // Lambda表达式实现触摸函数,空的,因为不需要做触摸实现
    listener->onTouchBegan = [](Touch*touch,Event* event){return true;};
    listener->onTouchMoved = [](Touch*touch,Event* event){};
    listener->onTouchEnded = [](Touch*touch,Event* event){};
    listener->onTouchCancelled = [](Touch*touch,Event* event){};
    // 加到GameOver层
    dispatcher->addEventListenerWithSceneGraphpriority(listener,layer);

最后一行:dispatcher->addEventListenerWithSceneGraphpriority(listener,layer);
这就是给layer层注册触摸事件。

由于GameOver层不需要做什么触摸逻辑,仅仅是把这个事件吞掉,不再向下传递,所有我直接用了几个空的Lambda表达式来做了

然后可以看到,达到效果了。

大佬总结

以上是大佬教程为你收集整理的cocos2d-x3.6 连连看触摸事件传递全部内容,希望文章能够帮你解决cocos2d-x3.6 连连看触摸事件传递所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。