Cocos2d-x 利用CCSpriteBatchNode建立子弹池
我们要实现如何利用CCSpriteBatchNode建立对象,CCSpriteBatchNode的好处是只渲染一次。
我们先来问一下.h文件的代码:
using namespace cocos2d; class HelloWorld : public cocos2d::CCLayerColor { public: //需要添加的代码 //炮塔 CCSprite* player; CCSpriteBatchNode* _actors; //缓冲对象 CCSpriteFrameCache* cache; CCArray* arr; void ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent); int numProjectile; void addMonster(); // // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(HelloWorld); };
接下来我们来看看.cpp
bool HelloWorld::init() { bool pRet = false; do { CC_BREAK_IF(!CCLayerColor::initWithColor(ccc4(255, 255, 255, 255))); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); //将zzzz.plist文件读入到缓存中 这个缓冲是一个单例 CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("zzzz.plist"); //读相应的zzzz.pvr.ccz加入到缓冲中去 _actors = CCSpriteBatchNode::create("zzzz.pvr.ccz"); //优化锯齿效果 _actors->getTexture()->setAliasTexParameters(); //将CCSpriteBatchNode对象加入到当前层 this->addChild(_actors); cache = CCSpriteFrameCache::sharedSpriteFrameCache(); //从系统的缓存中加载图片 player = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("player2.png")); player->setPosition(ccp(player->getContentSize().width / 2 , winSize.height / 2)); _actors->addChild(player); this->setTouchEnabled(true); //初始化一个数组 以便存放子弹 arr = CCArray::create(); //因为要在其他方法中调用,所以要retain一下 arr->retain(); //初始化一个计数器,留给以后从数组中取子弹时用 numProjectile = 0; //初始化100个子弹 for (int i = 0; i <100; i++) { CCSprite* projectile = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("projectile2.png")); projectile->setPosition(ccp(30, 160)); //将初始化的子弹隐藏 projectile->setVisible(false); //加入到节点中 _actors->addChild(projectile); arr->addObject(projectile); } this->schedule(schedule_selector(HelloWorld::addMonster), 1); pRet = true; } while (0); return pRet; }
//点击屏幕发射子弹 这个是点击屏幕时触发的回调方法 void HelloWorld::ccTouchesBegan(cocos2d::CCSet *pTouches, cocos2d::CCEvent *pEvent) { //从数组中将子弹取出 CCSprite* projectile = (CCSprite* )arr->objectAtIndex(numProjectile); //将隐藏的子弹显示 projectile->setVisible(true); //没点一次计数器加一 numProjectile++; //当计数器大于初始化自动的数量时,重新置为0 if (numProjectile >= arr->count()) { numProjectile = 0; } CCMoveTo* move = CCMoveTo::create(2, ccp(530, 200)); projectile->runAction(move); }
//添加敌人的方法, 简单实现 未随机位置 void HelloWorld::addMonster() { CCSprite* monster = CCSprite::createWithSpriteFrame(cache->spriteFrameByName("monster.png")); monster->setPosition(ccp( 548, 200)); CCMoveTo* move = CCMoveTo::create(2, ccp(- monster->getPosition().x / 2, 200)); monster->runAction(move); _actors->addChild(monster); }
代码下载:http://pan.baidu.com/share/link?shareid=316277820&uk=3189484501
Hi,推荐文件给你 "子弹池例子.zip" http://vdisk.weibo.com/s/HJA_I
本文出自 “7087095” 博客,请务必保留此出处http://7097095.blog.51cto.com/7087095/1230379