在游戏开发中,有时会须要在某个游戏对象上的运动轨迹上实现渐隐效果。比方子弹的运动轨迹,假设不借助引擎的帮助,这样的效果则须要通过大量的图片来实现。而Cocos2D-x的拖动渐隐效果类CCMotionStreak就帮助我们实现这个效果。以下是子弹飞行火焰拖尾的效果实现。。

  1. #include "HelloWorldScene.h"
  2. #include "SimpleAudioEngine.h"
  3. using namespace cocos2d;
  4. using namespace CocosDenshion;
  5. CCScene* HelloWorld::scene()
  6. {
  7. CCScene *scene = CCScene::create();
  8. HelloWorld *layer = HelloWorld::create();
  9. scene->addChild(layer);
  10. return scene;
  11. }
  12. bool HelloWorld::init()
  13. {
  14. if ( !CCLayer::init() )
  15. {
  16. return false;
  17. }
  18. size = CCDirector::sharedDirector()->getWinSize();
  19. CCLayerColor* background = CCLayerColor::create(ccc4(255, 200, 255, 255), size.width, size.height);
  20. this->addChild(background);
  21. //存储子弹节点
  22. bulltArr = CCArray::create();
  23. CC_SAFE_RETAIN(bulltArr);
  24. //存储 CCMotionStreak
  25. streakArr = CCArray::create();
  26. CC_SAFE_RETAIN(streakArr);
  27. scheduleUpdate();
  28. return true;
  29. }
  30. void HelloWorld::update(float delta)
  31. {
  32. for (int i = 0; i < bulltArr->count(); i++) {
  33. CCSprite* bullt = (CCSprite*)bulltArr->objectAtIndex(i);
  34. bullt->setPositionY(bullt->getPositionY()+2);
  35. CCMotionStreak* streak = (CCMotionStreak*)streakArr->objectAtIndex(i);
  36. //每次调用setPosition函数又一次设置对象位置时,“影子”将被创建而且慢慢渐隐  注:对于CCMotionStreak对象不可用getPosition()等方法。。
  37. streak->setPosition(bullt->getPosition());
  38. }
  39. for (int i = 0; i < bulltArr->count(); i++) {
  40. CCSprite* bullt = (CCSprite*)bulltArr->objectAtIndex(i);
  41. //删除超出屏幕节点
  42. if (bullt->getPositionY() >= size.height+bullt->getContentSize().height) {
  43. this->removeChild(bullt);
  44. bulltArr->removeObject(bullt);
  45. CCSprite* streak = (CCSprite*)streakArr->objectAtIndex(i);
  46. this->removeChild(streak);
  47. streakArr->removeObject(streak);
  48. break;
  49. }
  50. }
  51. }
  52. //拖尾效果类CCMotionStreak
  53. //创建 子弹节点 和 CCMotionStreak对象
  54. void HelloWorld::createBullt(CCPoint startPoint)
  55. {
  56. CCSprite* bullt = CCSprite::create("bullet3.png");
  57. bullt->setPosition(startPoint);
  58. this->addChild(bullt,2);
  59. bulltArr->addObject(bullt);
  60. //第一个參数是间隐的时间,第二个參数是间隐片断的大小,第三个參数是贴图的宽高,第四个參数是颜色值RGB,第五个參数是贴图的路径或者贴图对象
  61. CCMotionStreak* streak = CCMotionStreak::create(0.8, 10, 10, ccRED, "bullet3.png");
  62. streak->setPosition(startPoint);
  63. this->addChild(streak,1);
  64. streakArr->addObject(streak);
  65. }
  66. bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
  67. {
  68. createBullt(pTouch->getLocation());
  69. return true;
  70. }
  71. void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){}
  72. void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){}
  73. void HelloWorld::onEnter()
  74. {
  75. CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
  76. CCLayer::onEnter();
  77. }
  78. void HelloWorld::onExit()
  79. {
  80. CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
  81. CCLayer::onExit();
  82. }
  83. HelloWorld::~HelloWorld()
  84. {
  85. CC_SAFE_RELEASE(bulltArr);
  86. CC_SAFE_RELEASE(streakArr);
  87. }


很多其它0

cocos2dx 以子弹飞行为例解说拖尾效果类CCMotionStreak的更多相关文章

  1. cocos2dx 子弹飞作为一个例子来解释解酒效果类CCMotionStreak

    感谢点评与关注,欢迎转载与分享. 勤奋努力,持之以恒! 在游戏开发中,有时会须要在某个游戏对象上的运动轨迹上实现渐隐效果.比方子弹的运动轨迹,假设不借助引擎的帮助.这样的效果则须要通过大量的图片来实现 ...

  2. cocos2d-x 2.0 拖尾效果分析

    转自:http://game.dapps.net/gamedev/game-engine/7281.html 在Cocos2d-x中,拖尾效果有一个专门的类CCMotionStreak来实现.下面我们 ...

  3. [Cocos2d-x For WP8]MotionStreak拖尾效果

    拖尾效果是指在在游戏中,一个精灵在运动的过程中会留下一个短暂的轨迹效果,在游戏里面如打斗的特效往往会需要用到这种效果来给运动的增加绚丽的效果.那么在Cocos2D-x里面我们可以使用一种内置的拖动渐隐 ...

  4. 【转】Cocos2d-x 2.0 拖尾效果深入分析

    Cocos2d-x 2.0 拖尾效果深入分析 另:本章所用Cocos2d-x版本为: cocos2d-2.0-x-2.0.2@ Aug 30 2012 http://cn.cocos2d-x.org/ ...

  5. [Unity3d]向量的过度方法以及拖尾效果

    Vector3.RotateTowards() 用法 public static function RotateTowards(current: Vector3, target: Vector3, m ...

  6. 浅谈canvas中的拖尾效果

    引言 很早就想了解以下 canvas 中的拖尾效果(如彗星,烟花等效果)是怎么实现的,但是一直没有深入了解,正巧在 codepen 上看到一个 demo,代码简单,效果炫酷,故有此文. 什么黑科技 在 ...

  7. Cocos Creator 的实现拖尾效果

    在游戏中,有时会需要在某个游戏对象上加上移动后的轨迹若隐若现的效果.使得游戏的效果较好,比如游戏大招,刀光,法术,流星划痕之类. Cocos Creator提供了一种内置的拖尾渐隐效果的实现方法:组件 ...

  8. Unity3D-飞机拖尾效果

    1.插件准备 unity3d官网,Assert Store搜索Cartoon_airplane 插件 2.拖尾效果实现 飞机显示 拖尾组件设计 在airplane_02下 右键 Effects-Tra ...

  9. Unity 武器拖尾效果

    Pocket RPG Weapon Trails 武器拖尾效果 Asset Store地址:https://www.assetstore.unity3d.com/en/#!/content/2458 ...

随机推荐

  1. Phonegap之内存问题

    使用phonegap的拍照功能时,安卓机会出现崩溃现象,这一问题的原因也许是你的手机内存不足,实际上却不是phonegap的问题,它也是原生android apps的一个普遍问题. 产生这一问题是因为 ...

  2. REST内容协商注解

    @Produces注解: 用于定义方法的响应实体的数据类型.可以定义一个或多个,同时可以为每种类型定义质量因素,质量因素取值范围从0--1的小数值,默认为1. 示例: @Path("conn ...

  3. PS字体工具字体显示不出来

    显示一个小点:搜索了各种答案,扩大字号(最大72)更改前景色和背景色,最正确的解释就是分辨率太低,我发现分辨率是1,一般的设置成300,1分辨率情况下可以在图层那选择文字图层,然后按ctrl+t,拉大 ...

  4. 基于Spring Boot构建的Spring MVC快速入门

    原文地址:http://tianmaying.com/tutorial/spring-mvc-quickstart 环境准备 一个称手的文本编辑器(例如Vim.Emacs.Sublime Text)或 ...

  5. linux命令中,执行一个程序,后面加上&, 代表的意思是什么?

    后台执行.也就是执行这个程序的同时,你的终端同时还能够做其他的事情,如果不加这个符号,那么你执行这个程序后,你的终端只能等这个程序执行完成才能够继续执行其他的操作 . 如:启动etcd: ./etcd ...

  6. bzoj1232

    由题意知,最后要保留的边肯定都要被走过 来回一条边所花费的时间=2*边长+安慰边两端的牛所要花的时间和 总时间就等于所保留边来回的时间和+根节点时间: 不难想到做一下最小生成树即可 贪心可知,根一定选 ...

  7. Bear 實驗室: 什麼是Git flow ? 如何在SourceTree使用Git flow管理開發!

      http://www.takobear.tw/12/post/2014/02/bear-git-flow-sourcetreegit-flow.html     Bear 實驗室: 什麼是Git ...

  8. 数学类杂志SCI2013-2014影响因子

    ISSN Abbreviated Journal Title Full Title Category Subcategory Country total Cites IF        2013-20 ...

  9. Reason: Server is in single user mode. Only one administrator can connect at this time

    单击Start→All Programs→Microsoft SQL Server 2008→Configuration Tools→SQL Server Configuration Manager. ...

  10. Android Monkey

    adb shell "monkey -c android.intent.category.LAUNCHER -c android.intent.category.MONKEY -c andr ...