Пример #1
0
void GameScene::addTouchListener()
{
    
    auto touchListener = EventListenerTouchOneByOne::create();
    touchListener->onTouchBegan = [this](Touch* t,Event* e)->bool{
        auto menuCoo = menu->convertToNodeSpace(t->getLocation());
        Rect oldAABB =  buttonCoco->getBoundingBox();
        Vec2 oldVec = oldAABB.origin;
        Vec2 newVec = oldVec-buttonCoco->getContentSize()/2;
        Rect newAABB(newVec.x,newVec.y,oldAABB.size.width,oldAABB.size.height);
        
        if(mpProgressTimer->getPercentage()==100.0f&&newAABB.containsPoint(menuCoo)){
            isMagic = true;
            coco->playAnimation(BaseSprite::State::magic);
            if(!eff){
             eff = EffSprite::create("pfca/effect/eff_point_Coco_ult.fca", "effect/eff_point_Coco_ult");
            this->addChild(eff);
            }
            eff->setVisible(true);
            eff->setPosition(0-_director->getWinSize().width/2,0);
            eff->playAnimation(BaseSprite::State::magic,CallFunc::create(CC_CALLBACK_0(GameScene::effectCallback, this)));
            
            mpProgressTimer->setPercentage(0.f);
            
        }
        
        return true;
    };
    
    auto hurtLisener = EventListenerCustom::create("hurt", [this](EventCustom* e)->void{
      
        auto hurtObject = (BaseSprite*)e->getUserData();
        Label* l = nullptr;
       ProgressTimer* hp = nullptr;
        if(hurtObject==boss){
            l = l1;
            hp = effectHpProgressTimer[0];
            
        }else{
            l  = l2;
            hp = effectHpProgressTimer[1];
        }
        if(dynamic_cast<TreeBoss*>(hurtObject)){
            l->setPosition(hurtObject->getPosition()+Vec2(_director->getWinSize().width/2,_director->getWinSize().height/2+200));
            hp->setPosition(hurtObject->getPosition()+Vec2(_director->getWinSize().width/2,_director->getWinSize().height/2+180));
            
        }
        else{
              l->setPosition(hurtObject->getPosition()+Vec2(_director->getWinSize().width/2,_director->getWinSize().height/2+100));
            hp->setPosition(hurtObject->getPosition()+Vec2(_director->getWinSize().width/2,_director->getWinSize().height/2+130));
        }
       
        hp->setPercentage((hurtObject->getProperty().curLife/hurtObject->getProperty().maxLife)*100);

        
        
        l->setString("-"+Value((int)hurtObject->getCurHurt()).asString());
        if(!l->getParent()){
           
            this->addChild(l,3);
        }
        if(!hp->getParent()){
            this->addChild(hp,3);
        }
        hp->setVisible(true);
        if(!l->isVisible()){
            l->setVisible(true);
            l->setOpacity(255);
        }
        auto move = MoveBy::create(0.5f, Vec2(0,20));
        auto fade = FadeOut::create(0.5f);
        
        l->runAction(Sequence::create(move, fade,CallFunc::create(std::bind(&GameScene::lableCallback, this,l)),nullptr));
        
        hp->runAction(Sequence::create(DelayTime::create(1.0f),CallFuncN::create([](Node* n){
            n->setVisible(false);
        }), NULL));
    });
    
    
    _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener,buttonCoco);
    _eventDispatcher->addEventListenerWithSceneGraphPriority(hurtLisener,this);
    
    
}