哇!板球 源码分析三

守门员出场

哇!板球 源码分析三_第1张图片

守门员出场,每个守门员是从屏幕的右侧中间的位置随机方向向左侧移动

FielderSprite* fielderSprite1 = FielderSprite::create("pic/fielder.png");
	//守门员精灵初始位置为右侧中间位置
	fielderSprite1->setPosition(ccp(GOALKEEPER_X, GOALKEEPER_Y));
	fielderSprite1->setAnchorPoint(ccp(0.5, 0.5));//默认锚点为0.5,0.5
	//将fielder精灵添加到布景中
	this->addChild(fielderSprite1, GAME_LEVEL_WYF);


动作序列中的6个动作,终点都在最左侧

	CCActionInterval* at1=CCMoveBy::create(3.5,CCPointMake(-800,-170));
	CCActionInterval* at2=CCMoveBy::create(4,CCPointMake(-800,-100));
	CCActionInterval* at3=CCMoveBy::create(2.5,CCPointMake(-800,-30));
	CCActionInterval* at4=CCMoveBy::create(4.5,CCPointMake(-800,40));
	CCActionInterval* at5=CCMoveBy::create(5,CCPointMake(-800,110));
	CCActionInterval* at6=CCMoveBy::create(3,CCPointMake(-800,180));


随机移动的6条线路

	int r = rand()%6;
	switch(r)
	{
	case 0:
		fielderSprite1->runAction(at1);
		fielderSprite1->ccat=at1;
		fielderSprite1->mark=1;break;
	case 1:
		fielderSprite1->runAction(at2);
		fielderSprite1->ccat=at2;
		fielderSprite1->mark=2;break;
	case 2:
		fielderSprite1->runAction(at3);
		fielderSprite1->ccat=at3;
		fielderSprite1->mark=3;break;
	case 3:
		fielderSprite1->runAction(at4);
		fielderSprite1->ccat=at4;
		fielderSprite1->mark=4;break;
	case 4:
		fielderSprite1->runAction(at5);
		fielderSprite1->ccat=at5;
		fielderSprite1->mark=5;break;
	case 5:
		fielderSprite1->runAction(at6);
		fielderSprite1->ccat=at6;
		fielderSprite1->mark=6;break;
	}


将定时器在一次被调用的时间内创建的守门员都添加到pArr中,用于后面检测是否与球碰撞了

array->addObject(fielderSprite1);

 

 

道具出场

哇!板球 源码分析三_第2张图片

道具5秒随机出现一次

	if(outPropFlag)//5'置一次ture
	{
		outPropFlag = false;
		outProp();//出道具
	}


进入outProp(), 创建道具从左上角往下落,若发射的球击中道具,会增额外的技能

	Prop * propSprite1 = NULL;//道具对象
	//3种道具随机出
	int w = rand()%3;
	switch(w)
	{
	case 0:
		propSprite1 = Prop::create("pic/red.png");
		propSprite1->mark=0;break;
	case 1:
		propSprite1 = Prop::create("pic/black.png");
		propSprite1->mark=1;break;
	case 2:
		propSprite1 = Prop::create("pic/blue.png");
		propSprite1->mark=2;break;
	}

	//设置道具精灵的初始位置
	propSprite1->setPosition(ccp(0,480 ));
	propSprite1->setAnchorPoint(ccp(0.5, 0.5));
	this->addChild(propSprite1, GAME_LEVEL_WYF);


道具的3种运动线路, 都是贝塞尔曲线动作

ccBezierConfig bezier_7;//贝塞尔曲线
	bezier_7.controlPoint_1 = ccp(100, 460);
	bezier_7.controlPoint_2 = ccp(150, 400);
	bezier_7.endPosition = ccp(200,30);
	CCActionInterval*  cat_7 = CCBezierTo::create(2.5, bezier_7);

	ccBezierConfig bezier_8;//贝塞尔曲线
	bezier_8.controlPoint_1 = ccp(50, 480);
	bezier_8.controlPoint_2 = ccp(200, 400);
	bezier_8.endPosition = ccp(250,30);
	CCActionInterval*  cat_8 = CCBezierTo::create(2, bezier_8);

	ccBezierConfig bezier_9;//贝塞尔曲线
	bezier_9.controlPoint_1 = ccp(100, 480);
	bezier_9.controlPoint_2 = ccp(250, 400);
	bezier_9.endPosition = ccp(300,30);
	CCActionInterval*  cat_9 = CCBezierTo::create(3, bezier_9);

	CCSequence* cc_7 = CCSequence::create
	(
		cat_7,
		CCCallFuncND::create(this, callfuncND_selector(BanQiuLayer::removeProp), NULL),
		NULL
	);

	CCSequence* cc_8 = CCSequence::create
	(
		cat_8,
		CCCallFuncND::create(this, callfuncND_selector(BanQiuLayer::removeProp), NULL),
		NULL
	);

	CCSequence* cc_9 = CCSequence::create
	(
		cat_9,
		CCCallFuncND::create(this, callfuncND_selector(BanQiuLayer::removeProp), NULL),
		NULL
	);


每次随机走1条路线

	int r = rand()%3;
	switch(r)
	{
	case 0:
		propSprite1->runAction(cc_7);break;
	case 1:
		propSprite1->runAction(cc_8);break;
	case 2:
		propSprite1->runAction(cc_9);break;
	}


将定时器在一次被调用的时间内创建的道具都添加到pArr中,用于后面检测是否与球碰撞了

	pArr->addObject(propSprite1);


 

发球:

哇!板球 源码分析三_第3张图片

 

在屏幕上点哪球就往哪方向发

点屏屏幕时获取其位置

void BanQiuLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent)
{
	long ct = getCurrentTime();

	//发球的时间间隔大于0.3秒
	if((ct-currTime)>SPACE_TIME)
	{
		outBallFlag = true;//发球标志位
		currTime = ct;//把这次发球时间给currTime
	}

	//获取第一个触点位置,保存
	CCSetIterator it = pTouches->begin();
	CCTouch* touch = (CCTouch*)(*it);
	location = touch->getLocation();
}

创建小球

	BallTest* ballSprite = BallTest::create("pic/ball.png");
	ballSprite->setXY(location.x,location.y);//设置小球的方向
	//设置小球的起点位置
	ballSprite->setPosition(ccp(OUTBALL_X,OUTBALL_Y));
	//设置hitFlag标志位为true
	ballSprite->setHitFlag(true);
	ballSprite->setAnchorPoint(ccp(0.5, 0.5));//默认锚点为0.5,0.5

将定时器在一次被调用的时间内创建的小球都添加到arr中,用于后面检测是否与球碰撞了

	arr->addObject(ballSprite);
	//将精灵添加到布景中
	this->addChild(ballSprite, BACKGROUND_LEVEL_WYF);
出球动作

	float vx = ballSprite->x;
	float vy = ballSprite->y;
	//出球动作
	CCActionInterval* at=CCMoveBy::create(OUTBALL_T,CCPointMake(BALL_V*vx,BALL_V*vy));
	seq = CCSequence::create
	(
		at,
		CCCallFuncND::create(this, callfuncND_selector(BanQiuLayer::removeBall), NULL),
		NULL
	);

	if(ballCount%10==0)//每5个小球里出来一个带火的小球
	{
		ballSprite->setMark(1);//给1代表带火的球, 粒子效果
	}

	ballCount++;//发出球的总数加1
	ballSprite->runAction(at);//执行出球动作
发球的声音

	if(MUSIC)
	{
		//播放出球音乐
		CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect
		(
				"sound/fire.mp3"
		);
	}






你可能感兴趣的:(哇!板球 源码分析三)