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

来源:互联网 发布:淘宝与天猫是一家的吗 编辑:程序博客网 时间:2024/06/10 20:25

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


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<angleUpLeft){float width =  getPositionX();float heigth = width/tan((launchAngle) *2*3.1415/360);//长不变, 算高destinationPosition.x = 0;destinationPosition.y = getPositionY()-heigth;//得到目的地位置float lenght = sqrt(width*width+heigth*heigth);speed = CCRANDOM_0_1() *200+500;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,launchAngle);//旋转动画FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);runAction(task);}  else if(angleUpLeft<=angle &&angle<angleUpRight){float heigth = visibleSize.height - getPositionY();float width = heigth*tan(launchAngle *2*3.1415/360);destinationPosition.x = getPositionX()+width;destinationPosition.y = visibleSize.height;directionY = -1;float lenght = sqrt(width*width+heigth*heigth);speed = CCRANDOM_0_1() *200+500;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,launchAngle);FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);runAction(task);}else if(angleUpRight<=angle &&angle<angleDownRight){float width = visibleSize.width - getPositionX();float heigth = width*tan((launchAngle-90) *2*3.1415/360);destinationPosition.x = visibleSize.width;destinationPosition.y = getPositionY()-heigth;float lenght = sqrt(width*width+heigth*heigth);speed = CCRANDOM_0_1() *200+500;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,launchAngle);FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);runAction(task);}else{float heigth = getPositionY();float width = heigth*tan((launchAngle-180) *2*3.1415/360);destinationPosition.x = getPositionX()-width;destinationPosition.y = 0;float lenght = sqrt(width*width+heigth*heigth);directionY = 1;speed = 500;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,launchAngle);FiniteTimeAction* task = Sequence::create(actionRotateTo, actionMoveTo,funcall, NULL);runAction(task);}}


0 0