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
- Cocos2d-x3.1SpriteBatchNode使用
- Cocos2d-x3.1FileUtilsTest使用
- Cocos2d-x3.1粒子使用
- Cocos2d-x3.1中ListView的使用
- Cocos2d-x3.1自定义字体库的使用
- Cocos2d-x3.1中EditBox使用
- Cocos2d-x3.1ImageView的使用
- Cocos2d-x3.1Vector的使用
- Cocos2d-x3.1Map的使用
- Cocos2d-x3.1中DrawNode的使用
- Cocos2d-x3.1中的TableView的使用
- Cocos2d-x3.1 HttpClient封装与使用
- Cocos2d-js_____Sprite 和 SpriteBatchNode
- Cocos2d-x SpriteBatchNode
- cocos2d-x SpriteBatchNode
- cocos2d-x3.x使用rapidjson
- Cocos2d-x3.1中的Text控件的使用
- Cocos2d-x3.1中调度器的使用
- lfjglkadfjgdfskl;jlkgjdfklgjldakfgj
- Android特色开发之账户管理
- Twisted入门教程
- 1408111053-hd-最少拦截系统.cpp
- The Diophantine Equation hdu 3270
- Cocos2d-x3.1SpriteBatchNode使用
- Android Bitmap详细介绍
- web.xml的作用
- MySQL触发器
- android程序运行时总出现“Unfortunately,程序名 has stopped” 的问题
- Qt 调用动态库 插件
- ios 开发一些公用的头文件
- linux非管理员安装python yaml
- hduoj1171!【数学】