今天讲捕鱼游戏里鱼的部分,也是比较关键的一部分。
一步一步来,先分析游戏,首先鱼是从屏幕外生成,然后它有个移动的动作,而且分为不同的类型。
然后就可以进入代码部分,首先设置一个计时器,控制时间生成鱼,而鱼分种类,所以就用到随机函数,随机输出一个值,传入switch语句,从而生成不同的鱼。
void GameScene::xiaoyuupdate(float dt){
Size visibleSize = Director::getInstance()->getVisibleSize();
int s = rand() % 7; //随机的s用于传入switch语句
auto move = MoveTo::create(8, Vec2(visibleSize.width + 650, rand() % 768)); //下面做两个移动的动作move1和move
auto move1 = MoveTo::create(15, Vec2(-650, rand() % 768));
switch (s){
case 0:
xiaoyu = Sprite::createWithSpriteFrameName("wugui1.png");
xiaoyu->setPosition(Vec2(-xiaoyu->getContentSize().width / 2, rand() % 768));
playanim(0);
xiaoyu->runAction(move);
p = 2;
break;
case 1:
xiaoyu = Sprite::createWithSpriteFrameName("huangyu1.png");
xiaoyu->setPosition(Vec2(-xiaoyu->getContentSize().width / 2, rand() % 768));
playanim(1);
xiaoyu->runAction(move);
p = 1;
break;
case 2:
xiaoyu = Sprite::createWithSpriteFrameName("dly01.png");
xiaoyu->setPosition(Vec2(visibleSize.width + xiaoyu->getContentSize().width / 2, rand() % 768));
playanim(2);
xiaoyu->runAction(move1);
p = 2;
break;
case 3:
xiaoyu = Sprite::createWithSpriteFrameName("dm01.png");
xiaoyu->setPosition(Vec2(visibleSize.width + xiaoyu->getContentSize().width / 2, rand() % 768));
playanim(3);
p = 2;
xiaoyu->runAction(move1);
break;
default:
break;
}
addChild(xiaoyu);
}
void GameScene::playanim(int x){
Size visibleSize = Director::getInstance()->getVisibleSize();
if (x == 0){
Animation* animation1 = Animation::create();
animation1->setDelayPerUnit(0.4f);
animation1->addSpriteFrame(frameCache->getSpriteFrameByName("wugui1.png"));
animation1->addSpriteFrame(frameCache->getSpriteFrameByName("wugui2.png"));
animation1->addSpriteFrame(frameCache->getSpriteFrameByName("wugui3.png"));
animation1->addSpriteFrame(frameCache->getSpriteFrameByName("wugui2.png"));
animation1->addSpriteFrame(frameCache->getSpriteFrameByName("wugui1.png"));
animation1->addSpriteFrame(frameCache->getSpriteFrameByName("wugui2.png"));
animation1->addSpriteFrame(frameCache->getSpriteFrameByName("wugui3.png"));
animation1->addSpriteFrame(frameCache->getSpriteFrameByName("wugui2.png"));
animation1->addSpriteFrame(frameCache->getSpriteFrameByName("wugui1.png"));
Animate* animate1 = Animate::create(animation1);
xiaoyu->runAction(RepeatForever::create(animate1));
}
else if (x == 1){
Animation* animation2 = Animation::create();
animation2->setDelayPerUnit(0.1f);
animation2->addSpriteFrame(frameCache->getSpriteFrameByName("huangyu1.png"));
animation2->addSpriteFrame(frameCache->getSpriteFrameByName("huangyu2.png"));
animation2->addSpriteFrame(frameCache->getSpriteFrameByName("huangyu3.png"));
animation2->addSpriteFrame(frameCache->getSpriteFrameByName("huangyu2.png"));
animation2->addSpriteFrame(frameCache->getSpriteFrameByName("huangyu1.png"));
animation2->addSpriteFrame(frameCache->getSpriteFrameByName("huangyu2.png"));
animation2->addSpriteFrame(frameCache->getSpriteFrameByName("huangyu3.png"));
animation2->addSpriteFrame(frameCache->getSpriteFrameByName("huangyu2.png"));
animation2->addSpriteFrame(frameCache->getSpriteFrameByName("huangyu1.png"));
Animate* animate2 = Animate::create(animation2);
xiaoyu->runAction(RepeatForever::create(animate2));
}
else if (x == 2){
Animation* animation3 = Animation::create();
animation3->setDelayPerUnit(0.1f);
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly01.png"));
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly02.png"));
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly03.png"));
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly04.png"));
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly05.png"));
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly06.png"));
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly07.png"));
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly08.png"));
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly09.png"));
animation3->addSpriteFrame(frameCache1->getSpriteFrameByName("dly10.png"));
Animate* animate3 = Animate::create(animation3);
xiaoyu->runAction(RepeatForever::create(animate3));
}
else if (x == 3){
Animation* animation5 = Animation::create();
animation5->setDelayPerUnit(0.1f);
animation5->addSpriteFrame(frameCache1->getSpriteFrameByName("dm01.png"));
animation5->addSpriteFrame(frameCache1->getSpriteFrameByName("dm02.png"));
animation5->addSpriteFrame(frameCache1->getSpriteFrameByName("dm03.png"));
animation5->addSpriteFrame(frameCache1->getSpriteFrameByName("dm04.png"));
animation5->addSpriteFrame(frameCache1->getSpriteFrameByName("dm05.png"));
animation5->addSpriteFrame(frameCache1->getSpriteFrameByName("dm06.png"));
animation5->addSpriteFrame(frameCache1->getSpriteFrameByName("dm07.png"));
animation5->addSpriteFrame(frameCache1->getSpriteFrameByName("dm08.png"));
Animate* animate5 = Animate::create(animation5);
xiaoyu->runAction(RepeatForever::create(animate5));
}
fishVec.pushBack(xiaoyu); //将鱼放进数组
}
那我就不讲序列帧动画了,可以去看前面的博客,最后放进数组也是用于优化游戏。