喜欢游戏,喜欢cocos2d-x开发,刚刚开始学习cocos2d-x,有什么不好的地方希望大伙指出
Watch Out!(中文名:贱鸟跳跳) iOS非常好玩的手游,用cocos2d-x v3.2模拟写了一遍
先看看游戏图:
主要代码:
圆圆(左边的) / 方方(右边的) 跳跃:
//判断触摸的位置(左/右)
if(location.x >= winSize.width / 2) {
//跳跃
auto jumpAction = fangfang->getActionByTag(tag_fangfangAction);
if(!jumpAction) {
fangfang->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("fangfang_jump.png"));
jumpAction = Sequence::create(JumpBy::create(0.5, Vec2(-80,0), _jumpHeight, 1),
CallFunc::create(CC_CALLBACK_0(WatchOut::callbackRunAction, this, fangfang)),
nullptr);
jumpAction->setTag(tag_fangfangAction);
fangfang->runAction(jumpAction);
}else if(jumpAction->isDone()) { //下一次点击的时候,只在跳跃完成才有效
fangfang->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("fangfang_jump.png"));
fangfang->runAction(jumpAction);
}
}
碰撞检测:
if(yyBox.intersectsRect(ffBox)) {
_gameState = GAME_STATE_OVER;
SimpleAudioEngine::getInstance()->playEffect(music_player_die);
auto startGame = this->getChildByTag(tag_start_game);
startGame->setVisible(true);
/* 爆炸效果 */
auto pExpVec = Vec2::ZERO;
auto pExpColor = Color4F::BLUE;
auto pExpTextureRect = Rect::ZERO;
//根据当前TAG来决定爆炸对象
if(_currentTag == yuanyuan->getTag()) {
yuanyuan->setVisible(false);
pExpVec = Vec2(
yuanyuan->getPositionX() + yuanyuan->getContentSize().width / 4,
yuanyuan->getPositionY() + yuanyuan->getContentSize().height / 4);
pExpColor = Color4F(57, 99, 207, 255);
pExpTextureRect = Rect(2,2,9,9);
}else {
fangfang->setVisible(false);
pExpVec = Vec2(
fangfang->getPositionX() + fangfang->getContentSize().width / 4,
fangfang->getPositionY() + fangfang->getContentSize().height / 4);
pExpColor = Color4F(212, 74, 123, 255);
pExpTextureRect = Rect(13,2,9,9);
}
auto pExplostion = ParticleExplosion::create();
pExplostion->setStartSize(3);
pExplostion->setStartColor(pExpColor);
pExplostion->setEndColor(pExplostion->getStartColor());
pExplostion->setLife(0.1);
pExplostion->setTotalParticles(100);
pExplostion->setRotationIsDir(false);
pExplostion->setAutoRemoveOnFinish(true);
pExplostion->setPosition(pExpVec);
pExplostion->setTextureWithRect(Director::getInstance()->getTextureCache()->addImage("watchout.png"), pExpTextureRect);
pExplostion->setScale(0.5);
top->addChild(pExplostion, 10);
}else if(ffBox.getMaxX() < yyBox.getMinX() &&
ffBox.getMinY() == 2 &&
yyBox.getMinY() == 2) {//如果没有碰撞 && 方方/圆圆都没处于跳跃状态
_gameState = GAME_STATE_RESET; //游戏重置
SimpleAudioEngine::getInstance()->playEffect(music_score);
yuanyuan->setVisible(false);
fangfang->setVisible(false);
_score ++;
//分数Label改变
auto scoreBg = this->getChildByTag(tag_score_bg);
LabelTTF* scoreLabel = (LabelTTF*)scoreBg->getChildByTag(tag_score_label);
scoreLabel->setString(StringUtils::format("%i", _score));
//分数增加(等级增加),速度增加,背景颜色改变
if(_score % 6 == 0) {
_runSpeed += 0.5;
int bgLevel = _score / 6;
LayerColor* topBg = (LayerColor*)this->getChildByTag(tag_bg_top);
LayerColor* bottomBg = (LayerColor*)this->getChildByTag(tag_bg_bottom);
if(bgLevel == 1) {
topBg->setColor(color_level_1);
bottomBg->setColor(color_level_1_b);
scoreLabel->setColor(color_level_1_cc3);
}else if(bgLevel == 2) {
topBg->setColor(color_level_2);
bottomBg->setColor(color_level_2_b);
scoreLabel->setColor(color_level_2_cc3);
}
}
}
完整代码下载: