实现子弹, 可以指定发射位置, 发发射角度, 有方向, 可以反弹

void CreateObstacleCopy::flyAction()
{
	int currentPositionX = getPositionX();//当前坐标
	int currentPositionY = getPositionY();
	speed = CCRANDOM_0_1() *500+200;//随机速度

	
	Point destinationPosition;
	int tempY = directionY;
	
	if (directionY >0)//向上运动
	{
			float areaHeight = visibleSize.height-currentPositionY;//获取飞行区域高度
			float getRandom = CCRANDOM_0_1() *(visibleSize.width+areaHeight*2);//随机左上右的一个位置
			
			if(getRandom >areaHeight+visibleSize.width)//飞行最后触碰到左边的位置
			{
				destinationPosition = Point( 0 , visibleSize.height-(getRandom - visibleSize.width-areaHeight));
				directionY=1;
			}
			else if(getRandom>areaHeight)//飞行最后触碰到上边的位置
			{
				destinationPosition = Point( getRandom-areaHeight , visibleSize.height);
				directionY = -1;
			}
			else//飞行最后触碰到右边的位置
			{
				destinationPosition = Point(visibleSize.width,  visibleSize.height-getRandom);
				directionY = 1;
			}
			//log("up");

	}
	else//向下运动
	{
		float areaHeight = currentPositionY;
		float getRandom = CCRANDOM_0_1() *(visibleSize.width+areaHeight*2);//随机左下右的一个位置

		if(getRandom >areaHeight+visibleSize.width)//飞行最后触碰到左边的位置
		{
			destinationPosition = Point( 0 , getRandom - visibleSize.width-areaHeight);
			directionY= -1;
		}
		else if(getRandom>areaHeight)//飞行最后触碰到下边的位置
		{
			destinationPosition = Point( getRandom-areaHeight , 0);
			directionY = 1;
		}
		else//飞行最后触碰到右边的位置
		{
			destinationPosition = Point(visibleSize.width, getRandom);
			directionY =-1;
		}
		//log("down");
	}

	float width = destinationPosition.x- currentPositionX;
	float heigth = destinationPosition.y - currentPositionY;//得到当前对象到目的地的矢量高
	
	float lenght = sqrt(width*width+heigth*heigth);//长度
	float radian = atan(width/ heigth);//h弧度
	float angle = (radian*360)/(2*3.141592);//化成角度了
	if(tempY<0) angle=180+angle;
	float during = lenght / speed;//飞行时间
	FiniteTimeAction* actionMoveTo = MoveTo::create(during,destinationPosition);//移动动画
	CallFunc * funcall= CallFunc::create(this, callfunc_selector(CreateObstacleCopy::animationDone));
	FiniteTimeAction* actionRotateTo = RotateTo::create(0.2,angle);//旋转动画
	FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);
	runAction(task);
}

void CreateObstacleCopy::animationDone()//动画结束后调用该函数
{
	//isCreate = false;
	flyAction();
}

void CreateObstacleCopy::firstLaunch()
{
	
	Point destinationPosition;
	
	//左上角到对象的角度
	float radian = atan(-getPositionX()/ (visibleSize.height-getPositionY()));//h弧度
	float angleUpLeft = (radian*360)/(2*3.141592);//化成角度了

	radian = atan((visibleSize.width-getPositionX())/ (visibleSize.height-getPositionY()));//h弧度
	float angleUpRight = (radian*360)/(2*3.141592);//化成角度了

	radian = atan( (getPositionY())/(visibleSize.width-getPositionX()));//h弧度
	float angleDownRight = (radian*360)/(2*3.141592)+90;//化成角度了

	 radian = atan(float(getPositionY())/ float(getPositionX()));//h弧度
	float angleDownLeft = -(radian*360)/(2*3.141592)-90;//化成角度了
	
	int angle = launchAngle;//传进来的方向
	if (angle>360+angleDownLeft)//角度转换,因为这的角度有负数,传进来的角度是正数
	{
		angle = angle - 360;
	}
	
	if (angleDownLeft<=angle &&angle 
 

你可能感兴趣的:(实现子弹, 可以指定发射位置, 发发射角度, 有方向, 可以反弹)