这篇文章中,我们讲下渐隐效果,例如拖动某个精灵,后面有长长的尾巴。cocos2dx中提供了这样的一个类,CCMotionStreak。OK,我们将配合上篇文章中讲到的粒子,来实现一个效果,直接上代码:
cocos2d::CCParticleSystemQuad *particle;
particle=CCParticleSystemQuad::create("Particle.plist");
particle->setPosition(ccp(200, 200));
this->addChild(particle, 1);
cocos2d::CCMotionStreak *_streak;
//第一个参数是渐隐的时间,第二个参数是间隐片断的大小,第三个参数是贴图的宽高,第四个参数是颜色,最后一个参数是贴图的路径
_streak=CCMotionStreak::create(1, 16, 16, ccc3(255,255,0), "sprite.png");
_streak->setPosition(ccp(200, 200));
this->addChild(_streak, 1);
重写touchmove事件:
void HelloWorld::ccTouchesMoved(CCSet *touchs, CCEvent *event)
{
//获取触点指针容器中第一个元素
CCSetIterator it=touchs->begin();
//将其转化为触点信息
CCTouch *touch=(CCTouch *)(*it);
CCPoint touchLocation=touch->getLocation();
// touchLocation=CCDirector::sharedDirector()->convertToGL(touchLocation);
particle->setPosition(ccp(touchLocation.x, touchLocation.y));
_streak->setPosition(ccp(touchLocation.x, touchLocation.y));
}
OK,让我们看看效果: