cocos2dx 植物大战僵尸 7 向日葵和双生向日葵

news/2025/3/5 2:16:30/
这个是向日葵的贴图,由于只找到了这样的资源图片,也就这样用了,不过这样做好处还是有的。使用遮罩让这两个向日葵只显示一个,当处于正常动画时显示上方的图片,当要生成太阳时就显示下方的图片。(同双生向日葵)

至于遮罩,则如图所示,只是显示一部分,不过需要注意的是,设置遮罩后还需要设置回默认遮罩。
有一个帖子
http://blog.csdn.net/wei_hehe/article/details/38057649
接下来看看实现吧
class SunFlower : public Plant
{
public:enum class State{Normal,ProducingSun,};
protected:State m_curState;//当前状态float m_elapsed;//当前流逝时间float m_produceSunDuration;//产出阳光 持续时间
public:SunFlower();~SunFlower();static SunFlower*create(const string&plantName);bool init(const string&plantName);virtual void update(float dt);virtual void visit();
protected:virtual void productSun();//产生太阳回调函数
};
//---------------------------------TwinSunflower------------------
class TwinSunflower : public SunFlower
{
public:TwinSunflower();~TwinSunflower();static TwinSunflower*create(const string&plantName);bool init(const string&plantName);
protected:virtual void productSun();
};
向日葵和双生向日葵非常相似,只不过向日葵一次产生一个太阳,而双生向日葵一次产生两个太阳,所以让双生向日葵继承自向日葵
bool SunFlower::init(const string&plantName)
{m_plantName = plantName;//设置正常动画auto animationName = plantName;auto animation = AnimationCache::getInstance()->getAnimation(animationName);//设置贴图auto firstFrame = animation->getFrames().front()->getSpriteFrame();m_pSprite = Sprite::createWithSpriteFrame(firstFrame);//设置位置auto size = m_pSprite->getContentSize();size.height /= 2.f;m_pSprite->setPosition(Point::ZERO);m_pSprite->setAnchorPoint(Point::ZERO);this->setContentSize(size);this->addChild(m_pSprite);//设置运行动画Animate*animate = Animate::create(animation);this->getSprite()->runAction(animate);m_produceSunDuration = 2.f;return true;
}
注意这个init函数,是需要一个字符串的,这样是为了避免硬编码,虽然知道这个是向日葵。
然后获取Animation,之后再设置贴图,然后设置内部的精灵位置和锚点,并且运行正常动画
还有一个就是m_produceSunDuration,这个是产生太阳动画的持续时间。
void SunFlower::update(float dt)
{//当前处于正常状态if (m_curState == State::Normal){m_elapsed += dt;//切换状态if (m_elapsed >= this->getColdDownTime() - m_produceSunDuration){m_elapsed = 0.f;m_curState = State::ProducingSun;//改变内部精灵位置auto pos = m_pSprite->getPosition();auto size = this->getContentSize();m_pSprite->setPosition(pos - Point(0.f,size.height));}}else if (m_curState == State::ProducingSun){m_elapsed += dt;//在1s后产生太阳if (m_elapsed >= m_produceSunDuration){this->productSun();m_elapsed = 0.f;m_curState = State::Normal;//改变内部精灵位置auto pos = m_pSprite->getPosition();auto size = this->getContentSize();m_pSprite->setPosition(pos + Point(0.f,size.height));}}
}
至于遮罩则是重写visit即可,但由于我使用的是SDL,所以会和cocos2dx不同,下面代码供参考
void SunFlower::visit()
{auto size = this->getContentSize();auto pos = this->getWorldPosition();SDL_Rect rect = {int(pos.x),int(pos.y),(int)size.width,(int)size.height};Director::getInstance()->getRenderer()->renderSetClipRect(&rect);Node::visit();Director::getInstance()->getRenderer()->renderSetClipRect(nullptr);
}
然后就是产生阳光方法
void SunFlower::productSun()
{//设置一个动作Size size = this->getContentSize(); auto x = RANDOM(0.f,size.width);auto y = RANDOM(0.f,size.height);JumpBy*jump = JumpBy::create(0.8f,Point(x,y),-size.height,1);auto pos = this->getPosition();auto realPos = this->getParent()->convertToWorldSpace(pos);m_pDelegate->makeSun(25,jump,realPos);
}
双生向日葵内部回调的也是这个,只不过循环两次
void TwinSunflower::productSun()
{//调用两次for (auto i = 0;i < 2;i++){SunFlower::productSun();}
}
然后就是植物层有一个产生植物方法
Plant*PlantLayer::makePlant(const string&name)
{Plant*plant = nullptr;if (name == SUNFLOWER_NAME){plant = m_pPlantFactory->createSunFlower(name);}else if (name == TWINSUNFLOWER_NAME){plant = m_pPlantFactory->createTwinSunflower(name);}if (plant != nullptr){m_plants.push_back(plant);}return plant;
}
然后就是植物工厂的实现
SunFlower*PlantFactory::createSunFlower(const string&plantName)
{SunFlower*plant = SunFlower::create(plantName);//设置属性plant->setCollisionType(CollisionType::Plant);plant->setColdDownTime(24.f);plant->setHitPoint(300);//初始化刚体plant->initBody();plant->addFixturesToBody(plantName);return plant;
}TwinSunflower*PlantFactory::createTwinSunflower(const string&plantName)
{TwinSunflower*plant = TwinSunflower::create(plantName);//设置属性plant->setCollisionType(CollisionType::Plant);plant->setColdDownTime(24.f);plant->setHitPoint(300);//初始化刚体plant->initBody();plant->addFixturesToBody(plantName);return plant;
}
创建植物,设置属性,设置冷却时间,设置血量,之后再初始化刚体
bool GameScene::tryPlanting(Card*card,Terrain*terrain)
{bool bRet = false;auto topPlant = terrain->getTopPlant();//获取必要物品auto necessaryItem = card->getNecessoryItem();//升级植物if (!necessaryItem.empty() && topPlant != nullptr&& topPlant->getPlantName() == necessaryItem){//创建新植物auto newPlant = m_pPlantLayer->makePlant(card->getCardName());newPlant->setActiveTime(card->getActiveTime());newPlant->setPosition(terrain->getPosition());newPlant->setDelegate(this);//添加到entity layer 场景auto entityLayer = this->getEntityLayer();entityLayer->addChild(newPlant);terrain->setInnerPlant(newPlant);//移除旧植物m_pPlantLayer->removePlant(topPlant);topPlant->removeFromParent();bRet = true;}//...
这里实现了植物的升级。
如图,后面则是box2d的调试绘图


http://www.ppmy.cn/news/713269.html

相关文章

针对“扫雷“和“植物大战僵尸“游戏,分析,扫描,阳光值,植物,金币,僵尸的分析逆向

《软件逆向分析》 2022年9月 目录 {#目录 .TOC-Heading} [一、实验工具介绍 3](#一实验工具介绍) [二、针对"扫雷"游戏 3](#二针对扫雷游戏) [2.1分析"初级"、"中级"和"高级"的棋盘内存地址范围 3](#分析初级中级和高级的棋盘内存…

python将视频分解为图片+将图片合成为视频

系列文章目录 文章目录 系列文章目录前言一、python视频拆分图片合成(源码一)1.python视频拆分1.python图片合成 二、python视频拆分图片合成(源码二)三、python视频拆分(源码三)总结 前言 一、python视频拆分图片合成(源码一) 1.python视频拆分 import cv2def video2frame(v…

Linux向日葵同步剪贴板,Windows远程桌面可双向复制粘贴图片了,向日葵客户端9.0.3更新...

无人值守远程桌面调取文档时&#xff0c;有时可能只是一段文字&#xff0c;我们都需要通过邮箱或两端都登录社交应用来传输&#xff0c;然而首先你得有两个社交账户&#xff0c;或者另一端得有人接收文件。懒癌症晚期者和高效人士要问了&#xff0c;有没有更方便的办法&#xf…

Linux向日葵同步剪贴板,远程桌面可双向复制粘贴图片 向日葵客户端9.0.3更新

即使是通过ctrlc、ctrlv的快捷方式亦可将被控电脑中的文本复制粘贴到本地。向日葵是一款阳光的控制手机或电脑的远程软件&#xff0c;主要功能是远程桌面、远程文件、远程摄像头、远程开机等。支持Windows、Mac、Linux、Android、iOS等主流系统相互穿越控制。 (手机通过向日葵控…

微服务架构介绍及SpringCloudAlibaba组件介绍

单体架构vs微服务架构 单机架构 什么是单体架构 一个归档包&#xff08;例如war格式&#xff09;包含了应用所有功能的应用程序&#xff0c;我们通常称之为单体应用。架构单体应用的方法论&#xff0c;我们称之为单体应用架构。&#xff08;就是一个war包打天下&#xff09;…

北京邦银汇通征信牌照为何先挂牌后又注销哪

2023年6月27日&#xff0c;中国人民银行营业管理部发布《关于注销北京邦银汇通征信有限公司企业征信业务经营备案的公告》&#xff0c;显示北京邦银汇通征信有限公司因业务调整&#xff0c;申请注销企业征信业务经营备案&#xff0c;并由营管部于日前注销。 笔者转发了该注销信…

使用python 定时发送微信信息给喜欢的人

原创 使用python 定时发送微信信息给喜欢的人 2019-11-08 16:34:18 冒牌技术小哥 阅读数 171 文章标签&#xff1a; python 更多 分类专栏&#xff1a; python 版权声明&#xff1a;本文为博主原创文章&#xff0c;遵循 CC 4.0 BY-SA 版权协议&#xff0c;转载请附上原文出处…

个人微信号发送zabbix告警信息

之前使用邮件和短信发送zabbix告警信息&#xff0c;但告警信息无法实时查看或者无法发送&#xff0c;故障无法及时通知运维人员。 后来使用第三方微信接口发送信息&#xff0c;愉快地用了一年多&#xff0c;突然收费了。 zabbix告警一直是我的痛点&#xff0c;近期发现一个基…