一、创建文件·

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() % - 1.0f;
auto shiftX = (rand() % + )*direction;
auto shiftY = (rand() % + )*direction; setSpeedX(shiftX == ? : shiftX);
setSpeedY(shiftY == ? : 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), + rand() % ); //调用基类函数~
}

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的搭建的更多相关文章

  1. cocos2dx游戏开发——捕鱼达人mini版学习笔记(二)——MainMenu的搭建

    一.创建文件~ MainMenuScene.h   MainMenuScene.cpp   MainMenuLayer.h   MainMenuLayer.cpp 那个场景的搭建就不多说了,那个我的打 ...

  2. 《Cocos2d-x游戏开发实战精解》学习笔记4--实战一个简单的钢琴

    上一节学习了使用Cocos2d-x播放音乐的方法,但是那种方法一般只适合于播放较大的音乐,而一般比较短小的音乐(如游戏中的打斗.按键音效等)则要通过playEffect来播放.本节使用该方法以及之前学 ...

  3. 《Cocos2d-x游戏开发实战精解》学习笔记3--在Cocos2d-x中播放声音

    <Cocos2d-x游戏开发实战精解>学习笔记1--在Cocos2d中显示图像 <Cocos2d-x游戏开发实战精解>学习笔记2--在Cocos2d-x中显示一行文字 之前的内 ...

  4. 《Cocos2d-x游戏开发实战精解》学习笔记1--在Cocos2d中显示图像

    Cocos2d-x中的图像是通过精灵类来显示的.在Cocos2d-x中游戏中的每一个角色.怪物.道具都可以理解成是一个精灵,游戏背景作为一种特殊的单位将其理解成是一个精灵也没有什么不妥.在源文件本章目 ...

  5. cocos2dx游戏开发——别踩白块学习笔记(二)——经典模式的实现

    一.创建GameScene以及GameLayer 就是简单创建一个Scene而已,在此就不多说啦~,可以参照我的打飞机的学习笔记(2). 二.添加一个开始栏 很简单,就是调用Block中的create ...

  6. 《Cocos2d-x游戏开发实战精解》学习笔记2--在Cocos2d-x中显示一行文字

    在Cocos2d-x中要显示文字就需要用到Label控件.在3.x版本的Cocos2d中,舍弃了之前版本所使用的LabelTTF.LabelAtlas.LabelBMFont 3个用于显示文字的类,而 ...

  7. cocos2dx游戏开发——别踩白块学习笔记(一)——Block类

    一.Block类介绍 当然啦,Block类在这个游戏里就是必需品= =,因为整体都是由这个搞出来的,所以我们可以把游戏需要实现的功能都放在这里. 主要有下面这些功能(经典模式): 1.创建一个Bloc ...

  8. Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇

    懒骨头(http://blog.csdn.net/iamlazybone QQ:124774397 ) 写下这些东西的同时 旁边放了两部电影 周星驰的<还魂夜> 甄子丹的<特殊身份& ...

  9. cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发

     cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发 的产生 视持续更新中.... 视频存放地址例如以下:http://ipd.pps.tv/user/1058663622     ...

随机推荐

  1. C# 三种实现抖屏的方式

    //int a = -2; //this.BringToFront(); //for (int i = 0; i < 20; i++) //{ // a = -a; // this.Locati ...

  2. PHP雪花背景验证码

    ValidateCode.class.php 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ...

  3. HDOJ 1848 Fibonacci again and again

    Fibonacci again and again Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Jav ...

  4. Brackets(区间dp)

    Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3624   Accepted: 1879 Descript ...

  5. 发现Select等注入语句自动跳转Code

    CODE区域: <?php $str = $_GET["keyword"]; $str00 = strtolower($str); //strtolower 变为小写函数 $ ...

  6. HDU 1069&&HDU 1087 (DP 最长序列之和)

    H - Super Jumping! Jumping! Jumping! Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format: ...

  7. Floyd算法 及其运用

    #include<stdio.h> ][]; ][]; void floyd(int n) { ;k<=n;k++) { ;i<=n;i++) { ;j<=n;j++) ...

  8. 如何在Linux上实现文件系统的自动检查和修复?

    Linux文件系统有可能在各种各样的情况下受到损坏,比如系统崩溃.突然断电.磁盘断开,或者文件节点 (i-node)不小心被覆盖等等,因此需要定期检查文件系统,而说到检查和修复Linux文件系统,fs ...

  9. 1.7 逆序数与归并排序[inversion pairs by merge sort]

    [本文链接] http://www.cnblogs.com/hellogiser/p/inversion-pairs-by-merge-sort.html [题目] 编程之美1.7光影切割问题可以进一 ...

  10. 更改Apache默认网站根目录

    Apache服务器网站根目录配置是个比较基本的操作,之前经常用,现在记一下笔记 打开Apache的配置文件,一般在Apache安装目录下的conf/httpd.conf配置文件中修改, 找到 Docu ...