cocos學習1

今天把飛機遊戲的一些動畫給實現了一部分:

飛機連續發射子彈:

1.首先定義一個常量;#define SP 10

2.在.h文件定義一個私有變量;private:
                                                            int sp = 0;

3.在構造函數裏面賦值0;this->sp = 0;

4.利用moveTo,移動子彈;

auto move = MoveTo::create(0.5, Vec2(visibleSize.width / 2, visibleSize.height + sp->getContentSize().height / 2));
this->addChild(sp);
sp->runAction(move);

5.變量的自加;this->sp++;

6.判定變量是否等於常量;this->sp >= SP

7.利用計時器循環發射子彈;this->scheduleUpdate();

8.最後在判斷裏面重新賦值0;this->sp = 0;


飛機的移動:

1.通過API 查找到相關的移動,先是通過複製一段代碼實現了隨着鼠標移動,很多地方不是很懂

2.最主要的還是對邊界的判定;

x軸的判定:

if (target->getPositionX() + touch->getDelta().x >= visibleSize.width - target->getContentSize().width / 2){
   target->setPositionX(visibleSize.width - target->getContentSize().width / 2);
  }else if (target->getPositionX() + touch->getDelta().x <= target->getContentSize().width / 2){
   target->setPositionX(target->getContentSize().width / 2);
  }

y軸的判定:
  if (target->getPositionY() + touch->getDelta().y >= visibleSize.height - target->getContentSize().height / 2){
   target->setPositionY(visibleSize.height - target->getContentSize().height / 2);
  }
  else if (target->getPositionY() + touch->getDelta().y <= target->getContentSize().height / 2){
   target->setPositionY(target->getContentSize().height / 2);
  }
  target->setPosition(target->getPosition() + touch->getDelta());

今天也有說到對飛機子彈的跟隨飛機的移動一起移動,但是沒有完成。




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章