一、创建文件·

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. SQL中CONVERT转化函数的用法

    格式:CONVERT(data_type,expression[,style])说明:此样式一般在时间类型(datetime,smalldatetime)与字符串类型(nchar,nvarchar,c ...

  2. CMS介绍

    CMS介绍 CMS是Content Management System的缩写,意为“内容管理系统”,它具有许多基于模板的优秀设计,可以加快网站开发的速度和减少开发的成本. CMS的功能不仅限于处理文本 ...

  3. IOS: 使用imageIO获取和修改图片的exif信息

    使用imageIO获取和修改图片的exif信息 一幅图片除了包含我们能看见的像素信息,背后还包含了拍摄时间,光圈大小,曝光等信息.UIImage类将这些细节信息都隐藏了起来,只提供我们关心的图片尺寸, ...

  4. JSON做下拉表格

    主页面,5-18j.php <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http ...

  5. 微信时代,"邮"你选择 腾讯企业邮箱推新玩法

    近日,腾讯企业邮箱在广州.北京.南京三地举办<微信时代,“邮”你选择>企业邮箱新方向客户见面会,同时也正式宣布将打通微信.“拥抱”移动办公,领航国内办公工具移动之“变”. 据了解,腾讯企业 ...

  6. HDU 4857 Couple doubi(找循环节)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4861 解题报告:桌子上有k个球 ,第i个球的价值wi = 1^i+2^i+...+(p-1)^i (m ...

  7. 关于DCMTK3.6.0源代码编译的总结

    1.DCMTK cmake出来的代码是一样的.MT和MD版本的区别在于DCMTK工程下的每个子工程的代码生成中的MT还是MD,只要修改成为相应的值就可以了. 2.依赖包的选择.依赖包必须与上面中所说的 ...

  8. 【OpenStack】OpenStack系列6之Sheepdog环境搭建

    准备 repo配置 yum clean all yum makecache yum install -y make automake autoconf gcc nss-devel wget git g ...

  9. JavaScript常用事件

    一般事件 事件 浏览器支持 描述 onClick HTML: 2 | 3 | 3.2 | 4   Browser: IE3 | N2 | O3 鼠标点击事件,多用在某个对象控制的范围内的鼠标点击 on ...

  10. ini 文件操作记要(1): 使用 TIniFile

    ini 文件操作记要(1): 使用 TIniFile unit Unit1; interface uses   Windows, Messages, SysUtils, Variants, Class ...