cocos2d-x 3.2 物理小游戏教程4 block it 刚体的隐藏和显示 源码资源下载

来源:互联网 发布:ubuntu下安装vmware12 编辑:程序博客网 时间:2024/05/16 14:03

修改一下wall.h

#ifndef __WALL_H__
#define __WALL_H__

#include "cocos2d.h"

class Wall : public cocos2d::Sprite
{
public:
    //创建方法 参数是图片路径
    static Wall* create(const std::string& spriteFrameName);
    bool init(const std::string& spriteFrameName);

    //隐藏 移除刚体
    void hide();

    //显示 恢复刚体
    void show();

private:
    //隐藏 显示的时间
    const float fadeTime = 0.1f;

    //防止连击 做一个判断
    bool canTouch = true;
};

#endif // !__WALL_H__
==============================================================


在wall.cpp中添加两个方法



void Wall::hide()
{
    

    //隐藏动作
    auto fadeOut = FadeOut::create(fadeTime);
    
    auto bodyUnEnableFunc = CallFunc::create([this]{
        //设置刚体无效
        this->getPhysicsBody()->setEnable(false);
    });

    //设置可以单击
    auto canTouchFunc = CallFunc::create([this]{
        canTouch = true;
            
    });

    /*
        1.精灵隐藏
        2.刚体无效
        3.可以点击
    */
    auto seq = Sequence::create(fadeOut, bodyUnEnableFunc, canTouchFunc, nullptr);

    this->runAction(seq);

}

void Wall::show()
{
    //放止连击
    if (!canTouch)
    {
        return;
    }

    canTouch = false;

    //显示动作
    auto fadeIn = FadeIn::create(fadeTime);

    auto bodyEnableFunc = CallFunc::create([this]{
        //设置刚体生效
        this->getPhysicsBody()->setEnable(true);
    });

    auto seq = Sequence::create(fadeIn, bodyEnableFunc, DelayTime::create(fadeTime), CallFunc::create(CC_CALLBACK_0(Wall::hide, this)), nullptr);

    this->runAction(seq);
}


=========================================================


在GameScene.h中


#include "Wall.h"

Wall *wallDown = nullptr;


把cpp中的wallDown换成全局变量

修改创建下方墙壁的方法

//创建下方墙壁
    wallDown = Wall::create("width.png");
    wallDown->setPosition(vSize.width * 0.5, wallLeft->getBoundingBox().getMinY() - wallTop->getContentSize().height / 2);
    this->addChild(wallDown);
    wallDown->hide();


修改onTouchBegin


bool GameScene::onTouchBegan(Touch *touch, Event *unused_event)
{
    if (isFirstTouch)
    {
        isFirstTouch = false;
        ball->move();
    }
    else
    {
        wallDown->show();
    }

    return true;
}


最后注释掉调试模式

//设置物理世界调试模式:显示全部

 //world->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);


资源和代码下载地址:点击打开链接

0 0
原创粉丝点击