Exemple #1
0
/*
 * @brief   Destroy a node.
 */
Node::~Node() {
    stopAllActions();
}
Exemple #2
0
bool GameMain::init()
{
    if(!Layer::init()){return false;}
    
    createBackground();
    createLabel();
    createCards();
    createBoards();
    createMenuButton();
    settingStart();
    
    auto listener=EventListenerTouchOneByOne::create();
    //ドラッグ開始
    listener->onTouchBegan=[this](Touch* touch,Event* event){
        ////
        if(!_moveCards.empty()){
            if(_moveCards.front()->getNumberOfRunningActions()){//ダブルタッチアニメーション中
                auto card=_moveCards.front();
                card->stopAllActions();//アニメーションをキャンセル
                this->dragTouchEnded(_homeCellLayer->getHomeCellTalonPosition(card->getCardType()));//CallFuncの処理を実行
            }
        }
        ////
        
        auto position=touch->getLocation();
        if(_gameState==GameState::TOUCH_WAITING){
            _moveCards.clear();
            if(TALON_AREA_RECT.containsPoint(position)){
                _moveCards=_talonLayer->dragCards(position);
                this->setDragLayer(_talonLayer);
            }else if(BOARD_CARD_AREA_RECT.containsPoint(position)){
                _moveCards=_boardCardLayer->dragCards(position);
                this->setDragLayer(_boardCardLayer);
            }else if(HOME_CELL_AREA_RECT.containsPoint(position)){
                _moveCards=_homeCellLayer->dragCards(position);
                this->setDragLayer(_homeCellLayer);
            }
            
            if(!_moveCards.empty()){//カードがドラッグできれば
                auto cardPosition=Vec2(0,-CARD_SIZE.height/4);
                int z=1;
                for(auto card:_moveCards){
                    card->setPosition(cardPosition);
                    _moveLayer->addChild(card,z);
                    cardPosition+=TALON_DIFF;
                    z++;
                }
                
                _moveLayer->setPosition(position);
                
                _gameState=GameState::DRAG;
                
                //ダブルタップ処理
                _doubleTouchFlag=false;
                if(_dragLayer==_talonLayer || _dragLayer==_boardCardLayer){//山札あるいは場札からのドロップ
                    if(_homeCellLayer->checkDropCard(_moveCards)){//ホームセルに置けるカードである
                        _doubleTouchFlag=true;//ダブルタップ処理フラグをtrue
                        _oneTouch=false;//一回タップされたかのフラグをfalse
                        _touchTime=0;//タップ時間の初期化
                    }
                }
            }else{
                this->setDragLayer(nullptr);
            }
        }
        return true;
    };
    //ドロップ、山札の切り替え
    listener->onTouchEnded=[this](Touch* touch,Event* event){
        auto position=touch->getLocation();
        if(_gameState==GameState::TOUCH_WAITING){
            if(TALON_AREA_RECT.containsPoint(position)){
                if(_talonLayer->touchTalonCard(position)){
                    //山札が一巡
                    if(_talonLayer->getPullCount()==3){
                        //引くカードが3枚ずつの場合-20
                        this->updateScore(-20);
                    }else if(_talonLayer->getPullCount()==1){
                        //引くカードが1枚ずつの場合-100
                        this->updateScore(-100);
                    }else{
                        log("Talon::_pullCoount neither 1 nor 3.");
                    }
                }
            }
        }else if(_gameState==GameState::DRAG){
            if(_doubleTouchFlag){//ダブルタップを行う可能性がある
                if(_oneTouch){//既に一度タップされている
                    if(_touchTime<TOUCH_TIME_PERIOD){//ダブルタップ受付時間以内
                        this->unschedule(DRAG_SCHEDULE);
                        auto card=_moveCards.front();
                        //this->dragTouchEnded(_homeCellLayer->getHomeCellTalonPosition(card->getCardType()));
                        _doubleTouchFlag=false;
                        
                        ////
                        //カードを移すホームセルの位置
                        auto homeCellPosition=_homeCellLayer->getHomeCellTalonPosition(card->getCardType());
                        //_moveLayerとGameMainレイヤーは基準座標が違うためconvertする
                        card->runAction(Sequence::create(MoveTo::create(0.3,_moveLayer->convertToNodeSpace(homeCellPosition))
                                                         ,CallFunc::create([this,homeCellPosition](){
                            this->dragTouchEnded(homeCellPosition);
                        })
                                                         ,NULL));
                        ////
                    }
                }else{
                    if(_touchTime<TOUCH_TIME_PERIOD){//ダブルタップ受付時間以内
                        _oneTouch=true;
                        //ダブルタップされなかったときのためにスケジュールを設定
                        this->scheduleOnce([this,position](float dt){
                            this->dragTouchEnded(position);
                            _doubleTouchFlag=false;
                        },TOUCH_TIME_PERIOD-_touchTime+0.1,DRAG_SCHEDULE);
                    }else{
                        this->dragTouchEnded(position);
                        _doubleTouchFlag=false;
                    }
                }
            }else{
                this->dragTouchEnded(position);
            }
        }
    };
    //ドロップ中の移動
    listener->onTouchMoved=[this](Touch* touch,Event* event){
        auto position=touch->getLocation();
        if(_gameState==GameState::DRAG){
            _moveLayer->setPosition(position);
        }
    };
    //キャンセル
    listener->onTouchCancelled=[this](Touch* touch,Event* event){
        if(_gameState==GameState::DRAG){
            _moveLayer->removeAllChildren();
            _dragLayer->cancelCards(_moveCards);
            _gameState=GameState::TOUCH_WAITING;
        }
    };
    //test
    
    Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener,this);
    
    return true;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
   //////////////////////////////
   // 1. super init first
   if (!Layer::init())
   {
      return false;
   }

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

   /*
   * Task : Create a menu with single button.
   * Button should listen to the mouse, and close the application on click
   */

   auto addDragObject = [this](const std::string& name, const int& shift, const Color3B& color)
   {
      auto label1 = Label::createWithTTF("BAUHS93.TTF", name);
      label1->setColor(color);

      auto labelMenuItem1 = MenuItemLabel::create(label1);
      labelMenuItem1->setPosition(Lazy::Common::VisibleRect::center() - Vec2(shift, shift));
      labelMenuItem1->setRotation(45.0f);
      labelMenuItem1->setScale(1.5f);

      auto label2 = Label::createWithTTF("BAUHS93.TTF", name);
      label2->setColor(color);

      auto labelMenuItem2 = MenuItemLabel::create(label2);
      labelMenuItem2->setPosition(Lazy::Common::VisibleRect::center() - Vec2(shift + 30, shift + 30));

      auto label3 = Label::createWithTTF("BAUHS93.TTF", name);
      label3->setColor(color);

      auto labelMenuItem3 = MenuItemLabel::create(label3);
      labelMenuItem3->setPosition(Lazy::Common::VisibleRect::center() - Vec2(shift + 50, shift + 50));

      labelMenuItem3->setRotation(-45.0f);
      labelMenuItem3->setScale(0.5f);

      auto dragObject = Lazy::Common::DragObject::create({ labelMenuItem1, labelMenuItem2, labelMenuItem3 });

      dragObject->onActivated = [this, label1, labelMenuItem1, label2, labelMenuItem2, label3, labelMenuItem3,  dragObject]()
      {
         labelMenuItem1->stopAllActions();
         labelMenuItem2->stopAllActions();
         labelMenuItem3->stopAllActions();

         auto rotateTo = RepeatForever::create(RotateBy::create(1.0, 360.0f));
         auto rotateTo2 = RepeatForever::create(RotateBy::create(0.5, 360.0f));
         auto rotateTo3 = RepeatForever::create(RotateBy::create(0.25, 360.0f));

         labelMenuItem1->runAction(rotateTo);
         labelMenuItem2->runAction(rotateTo2);
         labelMenuItem3->runAction(rotateTo3);

         dragObject->onDeactivated = [this, label1, labelMenuItem1, rotateTo, label2, labelMenuItem2, rotateTo2, label3, labelMenuItem3, rotateTo3]()
         {
            labelMenuItem1->stopAction(rotateTo);
            labelMenuItem2->stopAction(rotateTo2);
            labelMenuItem3->stopAction(rotateTo3);

            auto rotateBack = RepeatForever::create(RotateTo::create(1.0, 0.0f));
            auto rotateBack2 = RepeatForever::create(RotateTo::create(1.0, 0.0f));
            auto rotateBack3 = RepeatForever::create(RotateTo::create(1.0, 0.0f));

            labelMenuItem1->runAction(rotateBack);
            labelMenuItem2->runAction(rotateBack2);
            labelMenuItem3->runAction(rotateBack3);
         };
      };

      dragObject->setDrawBB(false);
      this->addChild(dragObject);
   };

   addDragObject("Vlad", 0, Color3B::BLUE);
   addDragObject("Irina", 50, Color3B::RED);
   addDragObject("Eugene_Goncharuk", -100, Color3B::GREEN);

   return true;
}