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 ...
随机推荐
- ios框架中UIResponder的职责链设计模式应用
今天有空,就把UIResponder的职责链图上传一下 如果不理解职责链设计模式的朋友,请参考:http://www.cnblogs.com/langtianya/p/4060941.html
- 【整理】Angularjs 监听ng-repeat onfinishrender事件
http://stackoverflow.com/questions/15207788/calling-a-function-when-ng-repeat-has-finished http://ww ...
- 使用Fabric进行crash收集统计
主要是帮助自己记一下地址. 1 申请Crashlytics服务:http://try.crashlytics.com 2 下载Fabric客户端,帮助集成Crashlytics到自己的项目中:http ...
- JS获取汉字首字母
//获取 汉字首字母 function makePy(str) { if (typeof (str) != "string") throw new Error(-1, " ...
- [Effective JavaScript 笔记] 第4条:原始类型优于封闭对象
js有5种原始值类型:布尔值.数字.字符串.null和undefined. 用typeof检测一下: typeof true; //"boolean" typeof 2; //&q ...
- Xcode 5.0.2 下载地址
下载地址:http://adcdownload.apple.com/Developer_Tools/xcode_5.0.2/xcode_5.0.2.dmg command_line_tools_os_ ...
- django程序报错CSRF verification failed. Request aborted.
django程序的html页面中form的method='post'的时候报错 Forbidden (403) CSRF verification failed. Request aborted.He ...
- [BZOJ3872][Poi2014]Ant colony
[BZOJ3872][Poi2014]Ant colony 试题描述 There is an entrance to the ant hill in every chamber with only o ...
- Linux Apache 怎么修改工作模式
Apache默认为prefork模式,主要是考虑到稳定性的原因. 要切换到worker模式,则需要登录到linux上,进行如下操作: 进入/usr/sbin目录 cd /usr/sbin 将当前的pr ...
- 关于C语言的printf输出问题
前端面试的时候老总居然问这个问题,有点震惊…… #include <stdio.h> #include <stdlib.h> void main() { ; printf(&q ...