Cocos2d-x3.1SpriteBatchNode使用

来源:互联网 发布:全国软件开发人员 编辑:程序博客网 时间:2024/05/16 06:12
bool NewSpriteBatch::init(){    auto bRet = false;        do{        CC_BREAK_IF(!Layer::init());        auto touchListener = EventListenerTouchAllAtOnce::create();//多点触摸        touchListener->onTouchesEnded = CC_CALLBACK_2(NewSpriteBatch::onTouchesEnded, this);//设置触摸结束后的回调函数        _eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);//设置事件监听        auto BatchNode = SpriteBatchNode::create("grossini_dance_atlas.png");//创建SpriteBatchNode对象        addChild(BatchNode,0,kTagSpriteBatchNode);//添加SpriteBatchNode对象                bRet = true;    }while(0);    return bRet;}void NewSpriteBatch::onTouchesEnded(const std::vector<Touch *> &touches, cocos2d::Event *event){    for(auto &touch : touches)    {        auto location = touch->getLocation();//获取触摸位置        addNewSpriteWithCoords(location);//在触摸位置处添加精灵    }}void NewSpriteBatch::addNewSpriteWithCoords(cocos2d::Vec2 p){    auto BatchNode = static_cast<SpriteBatchNode*>(getChildByTag(kTagSpriteBatchNode));//获取SpriteBatchNode对象        int idx = (int)(CCRANDOM_0_1() * 1400 / 100);//CCRANDOM_0_1是0到1之间的随机数    log("CCRANDOM_0_1 = %lf",CCRANDOM_0_1());    int x = (idx%5) * 85;//设置x轴坐标    int y = (idx/5) * 121;//设置y轴坐标    log("idx5 = %d",idx%5);    log("idx/5 = %d",idx/5);    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.80)        action = TintBy::create(2, 0, -255, -255);    else        action = FadeOut::create(2);        auto action_back = action->reverse();    auto seq = Sequence::create(action,action_back, NULL);//动作序列    sprite->runAction(RepeatForever::create(seq));//执行动作}


class NewSpriteBatch : public Layer{public:    CREATE_FUNC(NewSpriteBatch);    virtual bool init();        void onTouchesEnded(const std::vector<Touch*>& touches, Event* event);//触摸事件    void addNewSpriteWithCoords(Vec2 p);//增加新精灵};

以上代码是SpriteBatchNode的使用实例

0 0
原创粉丝点击