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

2024-02-23 14:48

本文主要是介绍实现子弹, 可以指定发射位置, 发发射角度, 有方向, 可以反弹,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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);}}

这篇关于实现子弹, 可以指定发射位置, 发发射角度, 有方向, 可以反弹的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/738984

相关文章

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

PyCharm接入DeepSeek实现AI编程的操作流程

《PyCharm接入DeepSeek实现AI编程的操作流程》DeepSeek是一家专注于人工智能技术研发的公司,致力于开发高性能、低成本的AI模型,接下来,我们把DeepSeek接入到PyCharm中... 目录引言效果演示创建API key在PyCharm中下载Continue插件配置Continue引言

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

使用Python合并 Excel单元格指定行列或单元格范围

《使用Python合并Excel单元格指定行列或单元格范围》合并Excel单元格是Excel数据处理和表格设计中的一项常用操作,本文将介绍如何通过Python合并Excel中的指定行列或单... 目录python Excel库安装Python合并Excel 中的指定行Python合并Excel 中的指定列P

基于Go语言实现一个压测工具

《基于Go语言实现一个压测工具》这篇文章主要为大家详细介绍了基于Go语言实现一个简单的压测工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录整体架构通用数据处理模块Http请求响应数据处理Curl参数解析处理客户端模块Http客户端处理Grpc客户端处理Websocket客户端