小球与道具碰撞

哇!板球 源代码分析四_数组

 

小球进了网后得分效果

哇!板球 源代码分析四_数组_02

 

遍历小球

 

for(int i=0;i<arr->count();i++)//遍历小球
	{
		//从arr获取每一个小球及当前位置
		BallTest* temp =(BallTest*)arr->objectAtIndex(i);
		CCPoint ballPoint = temp->getPosition();

遍历守门员

 

 

		for(int j=0;j<array->count();j++)//遍历守门员
		{
			//从array获取每一个守门员及当前位置
			FielderSprite* tempSprite = (FielderSprite*)array->objectAtIndex(j);
			CCPoint fielderPoint = tempSprite->getPosition();

假设吃到第三种道具。 从左至扫描出火效果, 当道的守门员所有移除

 

 

			if(blurFlag)
			{
				CCPoint firePoint = fire[1]->getPosition();
				if(fielderPoint.x-firePoint.x<10)
				{
					array->removeObject(tempSprite);
					this->removeChild(tempSprite);
				}
			}
遍历道具,检測小球与道具是否是碰撞。吃到不同的道具有不同的威力

 

 

			for(int k=0;k<pArr->count();k++)//遍历道具
			{
				Prop* pTemp =(Prop*)pArr->objectAtIndex(k);
				CCPoint propPoint = pTemp->getPosition();
				//吃到道具, 小球碰撞到道具
				if(abs(propPoint.x-ballPoint.x)<17&&abs(propPoint.y-ballPoint.y)<27)
				{
					int m = pTemp->mark;//推断吃到什么道具, 共3种道

					switch(m)
					{
					case 0:	
						if(temp->mark!=1)//第1种道具,是带火的球加火
						{
							temp->setMark(1);
						}
						break;
					case 1:
						stopFielder();//第2种道具,停止守门员运动
						break;
					case 2:outFire(); //第3种道具。 从左至扫描出火效果
					break;
					}
					//吃到道具 道具消失
					pTemp->stopAllActions();
					pArr->removeObject(pTemp);
					this->removeChild(pTemp);
				}
			}

//另外一种道具停止守门员运动
void BanQiuLayer::stopFielder()
{
	for(int j=0;j<array->count();j++)//守门员
	{
		FielderSprite* tempSprite = (FielderSprite*)array->objectAtIndex(j);

		tempSprite->stopAction(tempSprite->ccat);//black 守门员停止
		stArr->addObject(tempSprite);//加入到停止数组里
		tempSprite->stopTime=getCurrentTime();
	}
}

//第三种道具从左至扫描出火效果
void BanQiuLayer::outFire()
{
	blurFlag = true;//第三种道具置成true

	CCParticleSystem* cps = CCParticleSun::create();
	cps->retain();
	cps->setPosition( ccp( 0, 7) );
	cps->setLife(0.01f);

	for(int i=0;i<48;i++)
	{
		fire[i]=CCSprite::create("pic/fire_2.png");
		fire[i]->setPosition(ccp(50,10*i));
		addChild(fire[i],GAME_LEVEL_WYF);

		fire[i]->addChild(cps, GAME_LEVEL_WYF);
		CCActionInterval* cs = CCMoveTo::create(1,CCPointMake(830,10*i));
		CCDelayTime *delay = CCDelayTime::create(3);
		fire[i]->runAction(
				CCSequence::create(
						cs,delay,CCCallFuncND::create(this, callfuncND_selector(BanQiuLayer::removeBlur), NULL),NULL));
	}

}

检測到小球与移动的守门员碰撞了,碰撞了有回弹效果

 

 

			if(abs(fielderPoint.x-ballPoint.x)<17&&abs(fielderPoint.y-ballPoint.y)<27)
			{
				//若碰到守门员则反弹
				float vx = temp->x;
				float vy = temp->y;
				int mark = temp->getMark();
				if(temp->mark==0)//推断为普通的球
				{
					//第一次碰到守门员把小球碰撞标志位置成false
					if(temp->getHitFlag())
					{
						temp->setHitFlag(false);
						tempCount++;//球被挡次数加1
						char a[4];
						snprintf(a, 4, "%d",tempCount);
						overLabel->setString(a);
					}
					if(tempCount==(this->targetCount)+1-guanka)//撞到守门员的次等于生命数,则失败
					{
						mag->toLoseLayer(tempScore,guanKa);//

						if(MUSIC)
						{
							//播放失败音乐
							CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect
							(
									"sound/failure.mp3"
							);
						}
					}
					//temp->stopAllActions();
					//temp->cleanup();
					temp->stopAction(seq);//停止曾经的动作

创建反弹的动作

 

 

					CCActionInterval* back=CCMoveBy::create(OUTBALL_T,CCPointMake(-BALL_V*vx,BALL_V*vy));
					CCSequence* atBack = CCSequence::create
					(
						back,
						CCCallFuncND::create(this, callfuncND_selector(BanQiuLayer::removeBall), NULL),
						NULL
					);

					temp->runAction(back);
推断当前小球的进了哪个网

 

 

if(ballPoint.x<GRID_ONE_BOTTOM_X&&ballPoint.x>GRID_ONE_BOTTOM_X-GRID_WIDTH+GRID_OFFSET
		&&ballPoint.y<GRID_ONE_BOTTOM_Y+GRID_HEIGHT&&ballPoint.y>GRID_ONE_BOTTOM_Y
		)//若进了以下1号网
		{
			//移除小球
			this->removeChild(temp);
			arr->removeObject(temp);

			plussScore(GRID_ONE_BOTTOM_X-GRID_WIDTH/2,GRID_ONE_BOTTOM_Y+GRID_HEIGHT,GRID_ONE_BOTTOM_INDEX);

		}else if(ballPoint.x<GRID_ONE_TOP_X&&ballPoint.x>GRID_ONE_TOP_X-GRID_WIDTH
				&&ballPoint.y<GRID_ONE_TOP_Y&&ballPoint.y>GRID_ONE_TOP_Y-GRID_HEIGHT
		)//若进了上面1号网
		{
			//移除小球
			this->removeChild(temp);
			arr->removeObject(temp);
			plussScore(GRID_ONE_TOP_X-GRID_WIDTH/2,GRID_ONE_TOP_Y-GRID_HEIGHT,GRID_ONE_TOP_INDEX);
		}
		else if(ballPoint.x<GRID_TWO_BOTTOM_X&&ballPoint.x>GRID_TWO_BOTTOM_X-GRID_WIDTH

				&&ballPoint.y<GRID_TWO_BOTTOM_Y+GRID_HEIGHT&&ballPoint.y>GRID_TWO_BOTTOM_Y
		)//若进了以下2号网
		{
			//移除小球
			this->removeChild(temp);
			arr->removeObject(temp);

			plussScore(GRID_TWO_BOTTOM_X-GRID_WIDTH/2,GRID_TWO_BOTTOM_Y+GRID_HEIGHT,GRID_TWO_BOTTOM_INDEX);
		}
		else if(ballPoint.x<GRID_TWO_TOP_X&&ballPoint.x>GRID_TWO_TOP_X-GRID_WIDTH

				&&ballPoint.y<GRID_TWO_TOP_Y&&ballPoint.y>GRID_TWO_TOP_Y-GRID_HEIGHT
		)//若进了上面2号网
		{
			//移除小球
			this->removeChild(temp);
			arr->removeObject(temp);

			plussScore(GRID_TWO_TOP_X-GRID_WIDTH/2,GRID_TWO_TOP_Y-GRID_HEIGHT,GRID_TWO_TOP_INDEX);
		}
		else if(ballPoint.x>GRID_SIX_X-GRID_HEIGHT

				&&ballPoint.y<GRID_SIX_Y+GRID_WIDTH/2&&ballPoint.y>GRID_SIX_Y-GRID_WIDTH/2+GRID_OFFSET

		)//若进了6号网
		{
			//移除小球
			this->removeChild(temp);
			arr->removeObject(temp);

			plussScore(GRID_SIX_X-GRID_HEIGHT,GRID_SIX_Y, GRID_SIX_INDEX);
		}

 

进网后加分效果,plussScore函数处理

 

void BanQiuLayer::plussScore(float x,float y,int index)//參数顺序:分值起点x轴,及y轴,进入的几号网调用及移动轨迹动作

定义5条移动的轨迹线

 

 

	ccBezierConfig bezier;//贝塞尔曲线
	bezier.controlPoint_1 = ccp(350, 240);
	bezier.controlPoint_2 = ccp(250, 106);
	bezier.endPosition = ccp(SCORE_X,SCORE_Y);
	CCActionInterval*  bezierForward = CCBezierTo::create(1.5, bezier);

	ccBezierConfig bezier_1;//贝塞尔曲线
	bezier_1.controlPoint_1 = ccp(400, 350);
	bezier_1.controlPoint_2 = ccp(250, 100);
	bezier_1.endPosition = ccp(SCORE_X,SCORE_Y);
	CCActionInterval*  bezierForward_1 = CCBezierTo::create(1, bezier_1);

	ccBezierConfig bezier_2;//贝塞尔曲线
	bezier_2.controlPoint_1 = ccp(350, 200);
	bezier_2.controlPoint_2 = ccp(250, 100);
	bezier_2.endPosition = ccp(SCORE_X,SCORE_Y);
	CCActionInterval*  bezierForward_2 = CCBezierTo::create(1, bezier_2);

	ccBezierConfig bezier_3;//贝塞尔曲线
	bezier_3.controlPoint_1 = ccp(550, 200);
	bezier_3.controlPoint_2 = ccp(250, 100);
	bezier_3.endPosition = ccp(SCORE_X,SCORE_Y);
	CCActionInterval*  bezierForward_3 = CCBezierTo::create(1, bezier_3);

	ccBezierConfig bezier_4;//贝塞尔曲线
	bezier_4.controlPoint_1 = ccp(350, 200);
	bezier_4.controlPoint_2 = ccp(450, 100);
	bezier_4.endPosition = ccp(SCORE_X,SCORE_Y);
	CCActionInterval*  bezierForward_4 = CCBezierTo::create(1, bezier_4);

//1号网系列
	CCSequence *one = CCSequence::create
	(
		bezierForward_1,
		CCCallFuncND::create(this,callfuncND_selector(BanQiuLayer::removeScoreOne),NULL ),
		NULL
	);
	CCSequence *oneDown = CCSequence::create
	(
		bezierForward_2,
		CCCallFuncND::create(this,callfuncND_selector(BanQiuLayer::removeScoreOne),NULL ),
		NULL
	);
	//2号网动作系列
	CCSequence *two = CCSequence::create
	(
		bezierForward_3,
		CCCallFuncND::create(this,callfuncND_selector(BanQiuLayer::removeScoreTwo),NULL ),
		NULL
	);
	//2号网动作系列
	CCSequence *twoDown = CCSequence::create
	(
		bezierForward_4,
		CCCallFuncND::create(this,callfuncND_selector(BanQiuLayer::removeScoreTwo),NULL ),
		NULL
	);
	//6号网动作系列
	CCSequence *six = CCSequence::create
	(
		bezierForward,
		CCCallFuncND::create(this,callfuncND_selector(BanQiuLayer::removeScoreSix),NULL ),
		NULL
	);

进入几号网,创建对应的分值精灵,并执行其轨迹动作

 

 

CCSprite *sixSprite=NULL;
	switch(index)
	{
	case 1:sixSprite = CCSprite::create("pic/score_1.png");
	sixSprite->runAction(one);
	break;
	case 2:sixSprite = CCSprite::create("pic/score_1.png");
	sixSprite->runAction(oneDown);
	break;
	case 3:sixSprite = CCSprite::create("pic/score_2.png");
	sixSprite->runAction(two);
	break;
	case 4:sixSprite = CCSprite::create("pic/score_2.png");
	sixSprite->runAction(two);
	break;
	case 5:sixSprite = CCSprite::create("pic/score_6.png");
	sixSprite->runAction(six);
	break;
	}
	//设置精灵对象的位置
	sixSprite->setPosition(ccp(x,y));
	//将精灵加入到布景中
	this->addChild(sixSprite, BACKGROUND_LEVEL_WYF);