Watch Out!(中文名:贱鸟跳跳)

本文介绍了如何使用cocos2d-x开发平台模拟一款名为'WatchOut'的手游,包括游戏核心功能实现,如角色跳跃、碰撞检测等。提供了详细代码示例和下载链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

喜欢游戏,喜欢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);
				}
			}
		}


完整代码下载:

http://download.youkuaiyun.com/detail/u011743345/8005321

https://github.com/514737546/watchout

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值