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

来源:互联网 发布:高端男士护肤品知乎 编辑:程序博客网 时间:2024/06/05 00:55

部分代码源自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));

}



0 0
原创粉丝点击