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() % - 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的搭建的更多相关文章
- cocos2dx游戏开发——捕鱼达人mini版学习笔记(二)——MainMenu的搭建
一.创建文件~ MainMenuScene.h MainMenuScene.cpp MainMenuLayer.h MainMenuLayer.cpp 那个场景的搭建就不多说了,那个我的打 ...
- 《Cocos2d-x游戏开发实战精解》学习笔记4--实战一个简单的钢琴
上一节学习了使用Cocos2d-x播放音乐的方法,但是那种方法一般只适合于播放较大的音乐,而一般比较短小的音乐(如游戏中的打斗.按键音效等)则要通过playEffect来播放.本节使用该方法以及之前学 ...
- 《Cocos2d-x游戏开发实战精解》学习笔记3--在Cocos2d-x中播放声音
<Cocos2d-x游戏开发实战精解>学习笔记1--在Cocos2d中显示图像 <Cocos2d-x游戏开发实战精解>学习笔记2--在Cocos2d-x中显示一行文字 之前的内 ...
- 《Cocos2d-x游戏开发实战精解》学习笔记1--在Cocos2d中显示图像
Cocos2d-x中的图像是通过精灵类来显示的.在Cocos2d-x中游戏中的每一个角色.怪物.道具都可以理解成是一个精灵,游戏背景作为一种特殊的单位将其理解成是一个精灵也没有什么不妥.在源文件本章目 ...
- cocos2dx游戏开发——别踩白块学习笔记(二)——经典模式的实现
一.创建GameScene以及GameLayer 就是简单创建一个Scene而已,在此就不多说啦~,可以参照我的打飞机的学习笔记(2). 二.添加一个开始栏 很简单,就是调用Block中的create ...
- 《Cocos2d-x游戏开发实战精解》学习笔记2--在Cocos2d-x中显示一行文字
在Cocos2d-x中要显示文字就需要用到Label控件.在3.x版本的Cocos2d中,舍弃了之前版本所使用的LabelTTF.LabelAtlas.LabelBMFont 3个用于显示文字的类,而 ...
- cocos2dx游戏开发——别踩白块学习笔记(一)——Block类
一.Block类介绍 当然啦,Block类在这个游戏里就是必需品= =,因为整体都是由这个搞出来的,所以我们可以把游戏需要实现的功能都放在这里. 主要有下面这些功能(经典模式): 1.创建一个Bloc ...
- Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇
懒骨头(http://blog.csdn.net/iamlazybone QQ:124774397 ) 写下这些东西的同时 旁边放了两部电影 周星驰的<还魂夜> 甄子丹的<特殊身份& ...
- cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发
cocos2d-x游戏开发实战原创视频讲座系列1之2048游戏开发 的产生 视持续更新中.... 视频存放地址例如以下:http://ipd.pps.tv/user/1058663622 ...
随机推荐
- Java-优秀博客推荐
一. TCP/IP Socket 兰亭风雨的专栏: http://blog.csdn.net/ns_code 二. NIO 并发编程网-Java NIO系列教程:http://ifeve.com/ch ...
- 操作sqlserver数据库常用的三个方法
1. ADO.NET -> 连接字符串,常用的两种方式: server=计算机名或ip\实例名;database=数据库名;uid=sa;pwd=密码; server=计算机名或ip\实例名;d ...
- PHP访问数据,增删改
主页面 <h1>主页面</h1> <table width="100%" border="1" cellpadding=" ...
- 微信公众平台开发(十) 消息回复总结——用其xml模板
一.简介 微信公众平台提供了三种消息回复的格式,即文本回复.音乐回复和图文回复,在这一篇文章中,我们将对这三种消息回复的格式做一下简单讲解,然后封装成函数,以供读者使用. 二.思路分析 对于每一个PO ...
- JAVA-- M选N的组合算法
M选N的组合算法 只要每个数字出现一次就可以 举例 :也就是说123与321和213属于重复 只算一组 此算法已经排除了重复数据 应用--彩票的注数算法 本程序的思路是开一个数组b,其长度 ...
- 详解HttpURLConnection
请求响应流程 设置连接参数的方法 setAllowUserInteraction setDoInput setDoOutput setIfModifiedSince setUseCaches setD ...
- Java数据库ResultSet转json实现
现在有很多json相关的Java工具,如json-lib.gson等,它们可以直接把JavaBean转换成json格式. 在开发中,可能会从数据库中获取数据,希望直接转成json数组,中间不通过bea ...
- HDU 2955 Robberies 背包概率DP
A - Robberies Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- Android自动登录与记住密码
// 获取实例对象 sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE); rem_pw ...
- RTX登录其他系统
前台: <html> <head> <title>签名验证</title> <meta http-equiv="Content-Lang ...