Cocos2d-x3.8.1制作飞翔的小鸟(下)

来源:互联网 发布:数据挖掘相关研究生 编辑:程序博客网 时间:2024/04/28 04:18

//上次介绍了flappyBird怎么创建物理场景接下来介绍cocos2d-x引擎的碰撞事件以及触摸事件的处理。

 

//playGameScene.cpp文件中的init()函数添加以下代码

 

auto hitEvent=EventListenerPhysicsContact::create();

hitEvent->onContactBegin=[this](PhysicsContact& contact)

{

        //关闭定时器

this->unschedule(schedule_selector(playGameScene::addBlocks,this));

this->unscheduleUpdate();

 

obstaclesBlock::Blocks->empty();//容器置空

this->removeAllChildren();

 

Director::getInstance()->replaceScene(TransitionMoveInB::create(0.2f,gameOverScene::createScene()));

return true;

};

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(hitEvent,this);

 

 

//触摸时间处理

auto touchEvent=EventListenerTouchOneByOne::create();

touchEvent->onTouchBegan=[this](Touch* touch,Event* event)

{

m_heroBird->runJump();

 

return true;

};

 

Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(touchEvent,this);

 

//heroBird.cpp加入jump()函数

 

void heroBird::runJump()

{   

     //PhysicsBody一个向上的物理速度

this->getPhysicsBody()->setVelocity(Vec2(0,350));

}

 

1 0
原创粉丝点击