cocos2dx 塔防游戏中射击动作笔记

      在使用cocos2dx 中很多时候会遇到射击子弹执行动作的状况,比如点击屏幕的一点子弹朝这个方向运动,其中子弹被回收时有两种情况,一种是攻击到敌人子弹被回收,另一种是子弹飞出屏幕外时子弹被回收,其中判断子弹飞出屏幕的那个点十分重要,今天就此封装一个函数找到飞出屏幕边界是碰撞的点以便在执行  移动到 – CCMoveTo 函数时设置对应点,封装函数如下:
//获取射击与边界碰撞结束点
CCPoint GetPoint(CCPoint heroPoint,CCPoint enemyPoint)
{
   float l,tag;
   CCPoint endPoint;
   CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
   //左下方
   if(heroPoint.x>enemyPoint.x&&heroPoint.y>enemyPoint.y)
      {
          tag=(heroPoint.y-enemyPoint.y)/(heroPoint.x-enemyPoint.x);
          l=heroPoint.y/tag;
          if(l<heroPoint.x)
           {
             endPoint.x=heroPoint.x-l;
             endPoint.y=0;
           }
          else
          {
                endPoint.x=0;
                endPoint.y=tag*(l-heroPoint.x); 
          }    
      }
//左上方
   else if(heroPoint.x>enemyPoint.x&&heroPoint.y<enemyPoint.y)
{
    tag=(enemyPoint.y-heroPoint.y)/(heroPoint.x-enemyPoint.x);
      l=(visibleSize.height-heroPoint.y)/tag;
          if(l<heroPoint.x)
           {
             endPoint.x=heroPoint.x-l;
             endPoint.y=visibleSize.height;
           }
          else
          {
                endPoint.x=0;
                endPoint.y=tag*heroPoint.x+heroPoint.y; 
          } 
}
//右下方
else if(heroPoint.x<enemyPoint.x&&heroPoint.y>enemyPoint.y)
{
   tag=(heroPoint.y-enemyPoint.y)/(enemyPoint.x-heroPoint.x);
   l=heroPoint.y/tag;
   if(l+heroPoint.x<=visibleSize.width)
   {
      endPoint.x=l+heroPoint.x;
      endPoint.y=0;}
else
{
endPoint.x=visibleSize.width;
endPoint.y=tag*(l+heroPoint.x-visibleSize.width);
}
}
//右上方
else if(heroPoint.x<enemyPoint.x&&heroPoint.y<enemyPoint.y)
{
tag=(enemyPoint.y-heroPoint.y)/(enemyPoint.x-heroPoint.x);
l=tag/(visibleSize.height-heroPoint.y);
  if(l<(visibleSize.width-heroPoint.x))
{
      endPoint.x=l+heroPoint.x;
      endPoint.y=visibleSize.height;
}
else
{
      endPoint.x=visibleSize.width;
      endPoint.y=visibleSize.height-tag*(l+heroPoint.x-visibleSize.width);
}
}//正左方
else if(heroPoint.x>enemyPoint.x&&heroPoint.y==enemyPoint.y)
{
      endPoint.x=0;
      endPoint.y=heroPoint.y;
}
//正右方
else if(heroPoint.x<=enemyPoint.x&&heroPoint.y==enemyPoint.y)
{
      endPoint.x=visibleSize.width;
      endPoint.y=heroPoint.y;
}
//正上方
else if(heroPoint.x==enemyPoint.x&&heroPoint.y<enemyPoint.y)
{
      endPoint.x=heroPoint.x;
      endPoint.y=visibleSize.height;
}
//正下方
else if(heroPoint.x==enemyPoint.x&&heroPoint.y>enemyPoint.y)
{
      endPoint.x=heroPoint.x;
      endPoint.y=0;
}

return endPoint;

}

连接地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值