<1>初始化plist文件,使用plist里面的资源
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("xxx.plist");
CCSprite* sprite = CCSprite::createWithSpriteFrameName("xxx.png");
其实有时,还需要这样加载:
//CCSpriteFrameCache(精灵帧缓存)主要用来存放CCSpriteFrame,它没有提供特别的属性,而是提供一系列用于管理CCSpriteFrame的方法
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("sprite_sheet.plist","sprite_sheet.png"); _gameBatchNode = CCSpriteBatchNode::create("sprite_sheet.png", 200); this->addChild(_gameBatchNode, kMiddleground);
<2>定时器
schedule(schedule_selector(GameLayer::createFishTick), 1.0f);
<3>鱼的游动
void GameLayer::createOneFish(int type) { char buf[64]; int fishtype = (int)(1000 * CCRANDOM_0_1()) % 2; //type是同一种鱼的不同动作,fishtype是不同的鱼 sprintf(buf, "fish_%d_%d.png", fishtype, type); CCSprite* fish = CCSprite::createWithSpriteFrameName(buf); float x = (int)(1000 * CCRANDOM_0_1()) % 80; float y = 30 + (int)(1000 * CCRANDOM_0_1()) % 300; fish->setPosition(ccp(x, y)); this->addChild(fish); fishArray->addObject(fish); ///////////////////////////将创建的鱼加到数组中 /***让鱼游动起来***/ CCArray* arr = CCArray::create(); for(int i = 0; i < 8; i++) //取得8个动画帧存到数组中 { char fishbuf[64]; sprintf(fishbuf, "fish_%d_%d.png", fishtype, i); //找到图片转化为精灵帧 CCSpriteFrame* sf = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(fishbuf); //动画帧 CCAnimationFrame* af = new CCAnimationFrame(); af->initWithSpriteFrame(sf, 1, NULL); af->autorelease(); arr->addObject(af); } //创建一个动画 CCAnimation* animation = CCAnimation::create(arr, 0.2); CCAnimate* animateAction = CCAnimate::create(animation); CCRepeatForever* rf = CCRepeatForever::create(animateAction); // CCPoint destPoint(600, y); float dt = 5 + (int)(1000 * CCRANDOM_0_1()) % 5; //鱼沿Bezier曲线游动 ccBezierConfig config; config.endPosition = destPoint; config.controlPoint_1 = ccp(200, 100); config.controlPoint_2 = ccp(100, 300); CCBezierTo* move = CCBezierTo::create(dt, config); // CCCallFuncN* moveDone = CCCallFuncN::create(this, callfuncN_selector(GameLayer::moveFinish)); CCFiniteTimeAction* allActions = CCSequence::create(rf, move, moveDone, NULL); fish->runAction(allActions); }
鱼游动完移除
void GameLayer::moveFinish(cocos2d::CCNode* sender) { CCSprite* sprite = (CCSprite*)sender; sprite->removeFromParentAndCleanup(true); fishArray->removeObject(sprite); ////////////////////////////////////移除鱼 }
<3>发射子弹
void GameLayer::ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) { CCLog("GamerLayer::ccTouchesEnded called"); //得到触摸位置 CCTouch* oneTouch = (CCTouch*)pTouches->anyObject(); CCPoint glPoint = oneTouch->getLocation(); CCPoint layerPoint = convertToNodeSpace(glPoint); //发射子弹 CCSprite* bulletSprite = CCSprite::createWithSpriteFrameName("bullet.png"); bulletSprite->setPosition(ccp(250, 45)); this->addChild(bulletSprite); bulletArray->addObject(bulletSprite); //////////////////////////////////////////////////添加子弹 //在第一颗子弹刚创建完毕,还未发射时,设置一个定时器,每隔0.1秒进行一次碰撞检测 schedule(schedule_selector(GameLayer::isFishAndBullerCash), 0.1f); CCMoveTo* move = CCMoveTo::create(1.0f, layerPoint); //用1秒的时间完成从初始位置到触摸位置的动作 CCCallFuncN* moveFinish = CCCallFuncN::create(this, callfuncN_selector(GameLayer::bulletMoveFinish) ); CCFiniteTimeAction* allActions = CCSequence::create(move, moveFinish, NULL); bulletSprite->runAction(allActions); }
<4>移除子弹
void GameLayer::bulletMoveFinish(cocos2d::CCNode* sender) { //得到子弹精灵 CCSprite* sprite = (CCSprite*)sender; //得到子弹位置 CCPoint bulletPoint = sprite->getPosition(); //移除子弹 bulletArray->removeObject(sprite); ////////////////////////////////////移除子弹 sprite->removeFromParentAndCleanup(true); }
<5>碰撞检测
void GameLayer::isFishAndBullerCash(float dt) { CCObject* fishObj = NULL; CCObject* bulletObj = NULL; CCARRAY_FOREACH(fishArray, fishObj) { CCARRAY_FOREACH(bulletArray, bulletObj) { CCSprite* fishSprite = (CCSprite*)fishObj; CCSprite* bulletSprite = (CCSprite*)bulletObj; CCRect fishRect = fishSprite->boundingBox(); CCPoint bulletPoint = bulletSprite->getPosition(); if(fishRect.containsPoint(bulletPoint)) { bulletSprite->removeFromParentAndCleanup(true); //加载鱼网 CCSprite* netSprite = CCSprite::createWithSpriteFrameName("fishing_net.png"); netSprite->setScale(0.5); //缩放 netSprite->setPosition(bulletPoint); addChild(netSprite); //鱼网用0.1秒进行从0.5 到 1.0 的缩放动作 CCScaleTo* scale = CCScaleTo::create(0.1, 1.0); CCCallFuncN* scaleDone = CCCallFuncN::create(this, callfuncN_selector(GameLayer::scaleDone)); CCFiniteTimeAction* allActions = CCSequence::create(scale, scaleDone, NULL); netSprite->runAction(allActions); } } } }