护盾的实现
在游戲中,由于大多的船的都是長方形的,所以把護盾做成了橢圓,而不是圓,另外也再護盾的外圍增加一個護盾的持續時間的一個計時器。效果:
所以,這里當敵人的子彈發射過來時,要在護盾邊上觸發(也就是橢圓的邊上)。
class ProtectBody : public CCSprite { public:static ProtectBody* create(float scaleX, float scaleY);//創造護盾bool init(float scaleX, float scaleY);//初始化bool checkCollision(const CCPoint &weaponPos);//橢圓碰撞void protectStart();//開始啟動護盾void protectFinished();//護盾啟動完成void protectEnd();//護盾結束void protectTimeEnd();//計時結束CC_SYNTHESIZE_READONLY(float, _sx, SX);//x軸縮放(不同的體型控制不同的縮放)CC_SYNTHESIZE_READONLY(float, _sy, SY);//y軸縮放(不同的體型控制不同的縮放)CC_SYNTHESIZE_READONLY(bool, _active, Active);//是否激活private:CCProgressTimer* _protectTime;//計時器 };實現的代碼十分簡單,可以一目了然。ProtectBody* ProtectBody::create(float scaleX, float scaleY) {ProtectBody* protectBody = new ProtectBody();if(protectBody && protectBody->init(scaleX, scaleY)) {protectBody->autorelease();return protectBody;}else {delete protectBody;protectBody = NULL;return NULL;} } bool ProtectBody::init(float scaleX, float scaleY) {bool result = false;if(CCSprite::init()) {//初始化this->_active = false;this->_sx = scaleX;this->_sy = scaleY;this->initWithSpriteFrameName(protect_body_png);this->setScale(0);this->setOpacity(0);ccBlendFunc blend = {GL_SRC_ALPHA, GL_ONE};this->setBlendFunc(blend);this->_protectTime = CCProgressTimer::create(CCSprite::createWithSpriteFrameName(protect_time_png));this->_protectTime->setOpacity(50);this->_protectTime->setVisible(false);this->_protectTime->setType(kCCProgressTimerTypeRadial);this->_protectTime->setReverseProgress(true);CCSize size = this->getContentSize();this->_protectTime->setPosition(ccp(size.width / 2, size.height / 2));this->addChild(this->_protectTime);result = true;}return result; } bool ProtectBody::checkCollision(const CCPoint &weaponPos) {CCPoint shipPos = GameLayer::shareGameLayer()->getShip()->getPosition();CCSize s = this->getContentSize();//橢圓碰撞float x = weaponPos.x - shipPos.x;float y = weaponPos.y - shipPos.y;float w = s.width * this->_sx;float h = s.height * this->_sy;float r = (x * x) / (w * w) + (y * y) / (h * h);if(abs(r) <= 0.25) {return true;}return false; } void ProtectBody::protectStart() {this->_active = true;CCActionInterval* scaleAction = CCScaleTo::create(0.5f, this->_sx, this->_sy);CCActionInterval* fadeAction = CCFadeTo::create(0.5f, 255);CCCallFunc* callback = CCCallFunc::create(this, callfunc_selector(ProtectBody::protectFinished));this->runAction(CCSequence::create(CCSpawn::create(scaleAction, fadeAction, NULL), callback, NULL));} void ProtectBody::protectFinished() {GameLayer::shareGameLayer()->getShip()->setIsProtected(true);CCProgressFromTo* to = CCProgressFromTo::create(STATIC_DATA_FLOAT("protect_time"), 100, 0);CCCallFunc* callback = CCCallFunc::create(this, callfunc_selector(ProtectBody::protectEnd));this->_protectTime->setPercentage(100);this->_protectTime->setVisible(true);this->_protectTime->runAction(CCSequence::create(to, callback, NULL)); } void ProtectBody::protectEnd() {CCActionInterval* scaleAction = CCScaleTo::create(0.5f, 0, 0);CCActionInterval* fadeAction = CCFadeTo::create(0.5f, 0);CCCallFunc* callback = CCCallFunc::create(this, callfunc_selector(ProtectBody::protectTimeEnd));this->runAction(CCSequence::create(CCSpawn::create(scaleAction, fadeAction, NULL), callback, NULL));GameLayer::shareGameLayer()->getShip()->setIsProtected(false);GameLayer::shareGameLayer()->setIsAppearShield(false); } void ProtectBody::protectTimeEnd() {this->_active = false;//不激活this->_protectTime->setVisible(false);//計時器設置不可見 }
總結
- 上一篇: python3 获取商店里App评论+
- 下一篇: opencv 数学形态学(2) 膨胀运算