-
cocos2dx 以子弹飞行为例解说拖尾效果类CCMotionStreak
在游戏开发中,有时会须要在某个游戏对象上的运动轨迹上实现渐隐效果。比方子弹的运动轨迹,假设不借助引擎的帮助,这样的效果则须要通过大量的图片来实现。而Cocos2D-x的拖动渐隐效果类CCMotionStreak就帮助我们实现这个效果。以下是子弹飞行火焰拖尾的效果实现。。
-
#include "HelloWorldScene.h"
-
#include "SimpleAudioEngine.h"
-
-
using namespace cocos2d;
-
using namespace CocosDenshion;
-
-
CCScene* HelloWorld::scene()
-
{
-
CCScene *scene = CCScene::create();
-
HelloWorld *layer = HelloWorld::create();
-
scene->addChild(layer);
-
return scene;
-
}
-
bool HelloWorld::init()
-
{
-
if ( !CCLayer::init() )
-
{
-
return false;
-
}
-
size = CCDirector::sharedDirector()->getWinSize();
-
-
CCLayerColor* background = CCLayerColor::create(ccc4(255, 200, 255, 255), size.width, size.height);
-
this->addChild(background);
-
-
-
bulltArr = CCArray::create();
-
CC_SAFE_RETAIN(bulltArr);
-
-
streakArr = CCArray::create();
-
CC_SAFE_RETAIN(streakArr);
-
-
scheduleUpdate();
-
-
return true;
-
}
-
-
void HelloWorld::update(float delta)
-
{
-
for (int i = 0; i < bulltArr->count(); i++) {
-
CCSprite* bullt = (CCSprite*)bulltArr->objectAtIndex(i);
-
bullt->setPositionY(bullt->getPositionY()+2);
-
-
CCMotionStreak* streak = (CCMotionStreak*)streakArr->objectAtIndex(i);
-
-
streak->setPosition(bullt->getPosition());
-
}
-
-
for (int i = 0; i < bulltArr->count(); i++) {
-
CCSprite* bullt = (CCSprite*)bulltArr->objectAtIndex(i);
-
-
if (bullt->getPositionY() >= size.height+bullt->getContentSize().height) {
-
this->removeChild(bullt);
-
bulltArr->removeObject(bullt);
-
-
CCSprite* streak = (CCSprite*)streakArr->objectAtIndex(i);
-
this->removeChild(streak);
-
streakArr->removeObject(streak);
-
-
break;
-
}
-
}
-
}
-
-
-
-
void HelloWorld::createBullt(CCPoint startPoint)
-
{
-
CCSprite* bullt = CCSprite::create("bullet3.png");
-
bullt->setPosition(startPoint);
-
this->addChild(bullt,2);
-
bulltArr->addObject(bullt);
-
-
-
CCMotionStreak* streak = CCMotionStreak::create(0.8, 10, 10, ccRED, "bullet3.png");
-
streak->setPosition(startPoint);
-
this->addChild(streak,1);
-
streakArr->addObject(streak);
-
}
-
-
bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
-
{
-
createBullt(pTouch->getLocation());
-
return true;
-
}
-
-
void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent){}
-
void HelloWorld::ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent){}
-
void HelloWorld::onEnter()
-
{
-
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
-
CCLayer::onEnter();
-
}
-
void HelloWorld::onExit()
-
{
-
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
-
CCLayer::onExit();
-
}
-
-
HelloWorld::~HelloWorld()
-
{
-
CC_SAFE_RELEASE(bulltArr);
-
CC_SAFE_RELEASE(streakArr);
-
}
-
相关阅读:
设置VS2017背景图片
NuGet的简单使用
C#6.0,C#7.0新特性
openFileDialog的Filter属性设置
C# 获取当前路径7种方法
正则表达式总结
IDEA设置switch/case代码块自动补齐
CentOS7使用yum安装RabbitMQ
vue react 路由history模式刷新404问题解决方案
@Component, @Repository, @Service的区别
-
原文地址:https://www.cnblogs.com/zfyouxi/p/3875951.html
Copyright © 2020-2023
润新知