首页 > 代码库 > cocos2dx游戏开发——捕鱼达人mini版学习笔记(一)——FishAchor的搭建
cocos2dx游戏开发——捕鱼达人mini版学习笔记(一)——FishAchor的搭建
一、创建文件·
FishAchor.h还有FishAchor.cpp。
主要就是创建每种鱼的类,方便以后的取用~,很多是重复性的操作,然后我们是mini版,暂时也就加入大概6钟鱼就好= =,然后我们现在就来搭建~。
二、鱼的基类
1、定义~
class FishActor : public Sprite //继承精灵类,然后作为各种鱼的基类,有最基本的属性{public: enum class FishActorType //首先在这里需要得知,鱼的类型 { SmallFish, AngelFish, Croaker, Amphiprion, Bream, MarlinsFish, }; /** Speed property of the fishes */ CC_SYNTHESIZE(float, speedX, SpeedX); //速度~ CC_SYNTHESIZE(float, speedY, SpeedY); FishActorType fishType; //鱼的类型 /** Create the fish by their types */ static FishActor* createWithType(FishActorType fishType); //创建方法~ /** Play the death animation */ virutal Animate* playDeathAnimation(); //鱼死亡时的动画~ /** Update the fish movement */ void updateFishMovement(float dt); //鱼出现的动画~ /** Activate the fish movement */ virutal void activateFishMovement(); //激活protected: CC_SYNTHESIZE(float, fishScore, FishScore); //鱼的得分~};
这个主要是作为一个大的接口类,方便用同一个接口,创建不同的鱼,节约重复性的代码操作~。具体的实现呢~
2、实现~
(1)创建方法~
FishActor* FishActor::createWithType(FishActorType fishType){ FishActor *fish=nullptr; //创建一个空指针 //Create the fishes by the fish type switch (fishType) //根据类型进行创建 { case FishActorType::SmallFish: fish = SmallFishActor::create(); break; case FishActorType::AngelFish: fish = AngelFishActor::create(); break; case FishActorType::Croaker: fish = CroakerActor::create(); break; case FishActorType::Amphiprion: fish = AmphiprionActor::create(); break; case FishActorType::Bream: fish = BreamActor::create(); break; case FishActorType::MarlinsFish: fish = MarlinsFishActor::create(); break; default: break; } return fish;}
(2)鱼移动的movement
void FishActor::updateFishMovement(float delta){ auto direction = rand() % 3 - 1.0f; auto shiftX = (rand() % 5 + 1)*direction; auto shiftY = (rand() % 5 + 1)*direction; setSpeedX(shiftX == 0 ? 1 : shiftX); setSpeedY(shiftY == 0 ? 1 : shiftY); auto rotation = -atan2(shiftY, shiftX); this->setRotation(rotation*180.0f / 3.14f + 180.0f);}
3、其中一种鱼的创建~
(1)类的声明~
class AngelFishActor : public FishActor{public: bool init(); CREATE_FUNC(AngelFishActor); Animate* playDeathAnimation(); //每个鱼敲掉的动画不一样~ void activateFishMovement();};
(2)具体实现
1、init()
bool SmallFishActor::init(){ FishActor::init(); //一般不会不成功的= =,是继承Sprite里面的创建~ setSpeedX(1.0f); //设置速度~ setSpeedY(1.0f); setFishScore(1.0f); 设置得分~ fishType = FishActorType::SmallFish; //Read the swimming animations textures auto fishes = Vector<SpriteFrame*>();//动态数组容器 fishes.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("SmallFish_actor_001.png")); fishes.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("SmallFish_actor_002.png")); fishes.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("SmallFish_actor_003.png")); fishes.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("SmallFish_actor_004.png")); //Create swimming animation auto fishAnimation = Animation::createWithSpriteFrames(fishes, 0.1); auto fishAnimate = Animate::create(fishAnimation); //Run the swiming action forever runAction(RepeatForever::create(fishAnimate)); return true;}
2、鱼移动的动画创建
void SmallFishActor::activateFishMovement(){ schedule(schedule_selector(FishActor::updateFishMovement), 3 + rand() % 2); //调用基类函数~}
3、死掉的动画~
Animate* SmallFishActor::playDeathAnimation(){ //Read the death anmtions textures auto deathFrames = Vector<SpriteFrame*>(); //创建一个数组 deathFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("SmallFish_death_001.png")); deathFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("SmallFish_death_002.png")); deathFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("SmallFish_death_003.png")); deathFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("SmallFish_death_004.png")); deathFrames.pushBack(SpriteFrameCache::getInstance()->getSpriteFrameByName("SmallFish_death_005.png")); //Create the death anmation auto deathAnimation = Animation::createWithSpriteFrames(deathFrames, 0.1);//设置播放的时间间隔~ auto deathAnimate = Animate::create(deathAnimation); 返回创建好的动画~
return deathAnimate;}
三、七说八说~
图片资源已经上传的github~:https://github.com/Wenne/FishingMini
cocos2dx游戏开发——捕鱼达人mini版学习笔记(一)——FishAchor的搭建
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。