(6) cocos2d-x发射子弹

    我要实现的功能很简单,就是点击屏幕上的任意点,能够从屏幕中间发射出一颗子弹,子弹要能飞到屏幕外。

    我使用了CCMoveTo这个action,它需要一个目的点,目的点应该是我点击的任一点与屏幕中间点连线上屏幕外的一点。我定义屏幕外20的距离为终点,就是说,子弹将飞到20之外,子弹在这个位置上,是看不到的。

    根据直线的函数Y = kX + b,我们需要求出k和b的值,我们知道两个点,起点(屏幕中间点),直线上的一点(鼠标按下的那个点),这样我们就可以求出k和b,这样就确定了这条直线。然后,我们根据预先设定,只要到屏幕外20就应该停止运动了,这个20是指X坐标和Y坐标只要有一个到了20就应该马上停止,要不,就有可能跑到很远很远的地方去了。这样,我就可以用CCMoveTo实现发射子弹效果了。我将它写成一个函数,在以后的项目中,就能直接使用了,代码如下:

[cpp]  view plain copy
  1. CCPoint HelloWorld::GetTargetPointOutOfWorld(CCPoint ptStart, CCPoint ptEnd, int nXOutOfWorld, int nYOutOfWorld)  
  2. {  
  3.     // Y = kX + b  
  4.     float fK = 1.0;  
  5.     float fb = 0.0;  
  6.     if (ptStart.x != ptEnd.x)  
  7.     {  
  8.         fK = (float)(ptStart.y - ptEnd.y) / (ptStart.x - ptEnd.x);    // 求出K  
  9.     }  
  10.     fb = ptStart.y - ptStart.x * fK;    // 求出b  
  11.   
  12.     // 求该直线在屏幕外的点  
  13.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  14.     float   fY = ptStart.y > ptEnd.y ? -nYOutOfWorld : size.height + nYOutOfWorld;  
  15.     float   fX = 1.0;  
  16.     if (fK != 0)  
  17.     {  
  18.         fX = (fY - fb) / fK;    // 这个fX可能非常大,或者非常小  
  19.     }  
  20.   
  21.     if (ptStart.x == ptEnd.x)    // 应该沿Y轴运动  
  22.     {  
  23.         fX = ptStart.x;  
  24.         fY = ptStart.y > ptEnd.y ? -nXOutOfWorld : size.height + nYOutOfWorld;  
  25.     }  
  26.     else if (ptEnd.y == ptStart.y)    // 应该沿X轴运动  
  27.     {  
  28.         fX = ptStart.x > ptEnd.x ? -nXOutOfWorld : size.width + nXOutOfWorld;  
  29.         fY = ptStart.y;  
  30.     }  
  31.     else if (fX > size.width + nXOutOfWorld)   // 重新计算fX和fY  
  32.     {  
  33.         fX = size.width + nXOutOfWorld;  
  34.         fY = fK * fX + fb;  
  35.     }  
  36.     else if (fX < -nXOutOfWorld)   // 重新计算fX和fY  
  37.     {  
  38.         fX = -nXOutOfWorld;  
  39.         fY = fK * fX + fb;  
  40.     }  
  41.   
  42.     return ccp(fX, fY);  
  43. }  

    使用是这样的:

[cpp]  view plain copy
  1. bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
  2. {  
  3.     CCSprite* pSprite = CCSprite::create("bullet.png");        // 加载子弹图片  
  4.     CCSize size = CCDirector::sharedDirector()->getWinSize();  
  5.     this->addChild(pSprite);  
  6.     pSprite->setPosition(ccp(size.width / 2, size.height / 2));  // 设置子弹的发射位置  
  7.     const int OUT_OF_WORLD = 20;  
  8.     CCPoint ptOutOfWorld = GetTargetPointOutOfWorld(ccp(size.width / 2, size.height / 2), pTouch->getLocation(), OUT_OF_WORLD, OUT_OF_WORLD);    // 获取屏幕外的一个点  
  9.     CCAction* pAction = CCMoveTo::create(0.5f, ptOutOfWorld);  
  10.     pSprite->runAction(pAction);    // 发射子弹  
  11.   
  12.     return true;  
  13. }  

     要实现屏幕触摸,你还需要在init函数中注册一下可触摸:

[cpp]  view plain copy
  1. this->setTouchEnabled(true);  
  2. CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);  
   

    看了一些博文,很多都没有指出如何打斜线,有的就是打垂直线或者水平线,希望我的这篇能帮到有需要的童鞋~~~~

    【转自 http://blog.csdn.net/dssdss123/article/details/12295475 】

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值