cocos2d-x by Example Beginner's Guide 第5章 Rocket Through

来源:互联网 发布:考试软件大全下载 编辑:程序博客网 时间:2024/05/16 05:28

废话少说,先上游戏截图。


游戏里有几个重要的地方。

1.向量知识。

2.橡皮筋线的绘制。

3.粒子系统。

void LineContainer::draw(){    switch (_lineType) {        case LINE_NONE:            break;        case LINE_TEMP:            ccDrawColor4F(1.0F, 1.0F, 1.0F, 1.0F);            ccDrawLine(_tip, _pivot);            ccDrawCircle(_pivot,10,CC_DEGREES_TO_RADIANS(360),10,false);            break;                    case LINE_DASHED:            ccDrawColor4F(1.0F, 1.0F, 1.0F, 1.0F);            ccDrawCircle(_pivot, 10, M_PI, 10, false);            {//复合语句                int segments = _lineLength / (_dash + _dashSpace);                float t = 0.0F;                float x_,y_;                float dt = 1.0F / segments;                for (int i = 0; i < segments; i++) {                    x_ = _pivot.x + t * (_tip.x - _pivot.x);                    y_ = _pivot.y + t * (_tip.y - _pivot.y);                    ccDrawCircle(ccp(x_,y_), 4, M_PI, 6, false);                    t += dt;                }            }            break;        default:            break;    }    //画能量条    ccDrawColor4F(0.0F, 0.0F, 0.0F, 1.0F);    ccDrawLine(ccp(_energyLineX, _screenSize.height * 0.1F), ccp(_energyLineX, _screenSize.height * 0.1F));    ccDrawColor4F(1.0F, 0.5F, 0.0F, 1.0F);    ccDrawLine(ccp(_energyLineX, _screenSize.height * 0.1F), ccp(_energyLineX, _screenSize.height * 0.1F + _energy * _energyHeight));}



最重要的部分

void GameLayer::update(float dt) {    if (!_running) {        return ;    }    CCPoint rocketPos = _rocket->getPosition();    if (_lineContainer->getLineType() != LINE_NONE) {        _lineContainer->setTip(rocketPos);    }    //检测是否与边缘发生碰撞,若碰消线    if (_rocket->collideWithSides()) {        _lineContainer->setLineType(LINE_NONE);    }    _rocket->update(dt);    //尾部火焰    if (!_jet->isActive()) {        _jet->resetSystem();    }    _jet->setRotation(_rocket->getRotation());    _jet->setPosition(rocketPos);    //更新 timers    _cometTimer += dt;    float newY;    if (_cometTimer > _cometInterval) {        _cometTimer = 0;        if (!_comet->isVisible()) {            _comet->setPositionX(0);            newY = (float) rand()/ ((float)RAND_MAX/_screenSize.height * 0.6F) + _screenSize.height * 0.2F;            if (newY > _screenSize.height * 0.9F) {                newY = _screenSize.height * 0.9F;            }            _comet->setPositionY(newY);            _comet->setVisible(true);            _comet->resetSystem();//要不要和上面的一句交换一下位置//?        }    }    if (_comet->isVisible()) {        //comet碰撞        CCPoint cometPos = _comet->getPosition();        if (pow(cometPos.x - rocketPos.x, 2) + pow(cometPos.y - rocketPos.y, 2) <= pow(_rocket->getRadius(), 2)) {            if (_rocket->isVisible()) {                killPlayer();            }        }        _comet->setPositionX(cometPos.x + 50 * dt);        if (_comet->getPositionX() > _screenSize.width * 1.5F) {            _comet->stopSystem();            _comet->setVisible(false);        }    }        //    _lineContainer->update(dt);    _rocket->setOpacity(_lineContainer->getEnergy() * 255);#ifdef DEBUG    CCLog("_lineContainer->getEnergy():%f",_lineContainer->getEnergy());    CCLog("_rocket->getOpacity():%f",_rocket->getOpacity());#endif        //行星碰撞    GameSprite* planet;    CCPoint planetPos;    int planetCount = _planets->count();    for (int i = 0; i < planetCount; i++) {        planet = (GameSprite *)_planets->objectAtIndex(i);        planetPos = planet->getPosition();        if (pow(planetPos.x - rocketPos.x ,2) + pow(planetPos.y - rocketPos.y,2) <=            pow(_rocket->getRadius() * 0.8F + planet->getRadius() * 0.65F, 2)) {            if (_rocket->isVisible()) {                killPlayer();            }            break;        }    }        //  星星    CCPoint starPos = _star->getPosition();    if (pow(starPos.x - rocketPos.x, 2) + pow(starPos.y - rocketPos.y, 2) <=        pow(_rocket->getRadius() * 1.2F,2) ) {        _pickup->setPosition(starPos);        _pickup->resetSystem();                if (_lineContainer->getEnergy() + 0.25F < 1) {            _lineContainer->setEnergy(_lineContainer->getEnergy() + 0.25F);        }        else {            _lineContainer->setEnergy(1.0F);        }                _rocket->setSpeed(_rocket->getSpeed() + 2);        _lineContainer->setEnergyDecrement(0.001F);        SimpleAudioEngine::sharedEngine()->playEffect("pickup.wav");        resetStar();                int points = 100 - _timeBetweenPickups;        points = MAX(0, points);                _score += points;        char szValue[60] = {0};        sprintf(szValue, "%i",_score);        _scoreDisplay->setString(szValue);                _timeBetweenPickups = 0;    }        _timeBetweenPickups += dt;    if (_lineContainer->getEnergy() == 0) {//float类型 可以和 0 比较相等        if (_rocket->isVisible()) {            killPlayer();        }    }    }



0 0
原创粉丝点击