您的位置:首页 > 其它

4.1.7 实战 精灵类及其相关类的使用

2015-04-22 07:05 253 查看
部分代码源自SpriteTest.cpp中的Sprite1类的addNewSpriteWithCoords函数

void SpriteBatchNode1::addNewSpriteWithCoords(Vec2 p)
{
auto BatchNode = static_cast<SpriteBatchNode*>( getChildByTag(kTagSpriteBatchNode) );
//
随机产生精灵
int idx = CCRANDOM_0_1() * 1400 / 100;
int x = (idx%5) * 85;
int y = (idx/5) * 121;

//
创建精灵
auto sprite = Sprite::createWithTexture(BatchNode->getTexture(), Rect(x,y,85,121));
BatchNode->addChild(sprite);
//
位置
sprite->setPosition( Vec2( p.x, p.y) );
//
创建动作
ActionInterval* action;
float random = CCRANDOM_0_1();
//
缩放
if( random < 0.20 )
action = ScaleBy::create(3, 2);
//
旋转
else if(random < 0.40)
action = RotateBy::create(3, 360);
//
闪烁
else if( random < 0.60)
action = Blink::create(1, 3);
//
else if( random < 0.8 )
action = TintBy::create(2, 0, -255, -255);
//
淡出
else
action = FadeOut::create(2);

auto action_back = action->reverse();
//
动作序列
auto seq = Sequence::create(action, action_back, nullptr);
//
运行动画
sprite->runAction( RepeatForever::create(seq));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: