cocos2d-x的初步学习二十三之模仿微信打飞机二
发布日期:2022-02-08 18:03:28 浏览次数:33 分类:技术文章

本文共 7148 字,大约阅读时间需要 23 分钟。

在这篇文章中,我们将把剩下的功能完成,首先,我们加入换子弹或是炸弹的功能,它的原理大体是这样的,设置物品出现的时间间隔,会随机出现物品的种类,然后判断物品跟飞机所在区域,碰撞检测,在一起就算是吃到这个物品了,。。OK,下面上代码

首页新建一个类继承CCNode,

ChangeBullet.h

typedef enum{    propsTypeBomb = 4,    propsTypeBullet = 5}prosType;class ChangeBullet:public cocos2d::CCNode{public:        cocos2d::CCSprite *prop;            prosType bulletType;        void initWithType(prosType type);        void propAnimation();        static ChangeBullet* create(void);        CC_SYNTHESIZE_RETAIN(cocos2d::CCSprite*, _prop, Prop);        ChangeBullet();    ~ChangeBullet();};
ChangeBullet.cpp

ChangeBullet::~ChangeBullet(){}ChangeBullet::ChangeBullet(){}ChangeBullet* ChangeBullet::create(){    ChangeBullet * pRet = new ChangeBullet();    if (pRet && pRet->init())    {        pRet->autorelease();    }    else    {        CC_SAFE_DELETE(pRet);    }	return pRet;}void ChangeBullet::initWithType(prosType type){    bulletType=type;            CCString *string=CCString::createWithFormat("enemy%i_fly_1.png",type);    prop=CCSprite::createWithSpriteFrameName(string->getCString());        prop->setPosition(ccp((arc4random()%268 + 23)*2,632*2));    }//物品出现动画void ChangeBullet::propAnimation(){    CCMoveTo *ac1=CCMoveTo::create(1, ccp(prop->getPosition().x, 250*2));    CCMoveTo *ac2=CCMoveTo::create(0.4, ccp(prop->getPosition().x, 252*2));    CCMoveTo *ac3=CCMoveTo::create(1, ccp(prop->getPosition().x, 632*2));    CCMoveTo *ac4=CCMoveTo::create(2, ccp(prop->getPosition().x, -55*2));    prop->runAction(CCSequence::create(ac1,ac2,ac3,ac4,NULL));}
在上面中,我们定义了一个换物品的类,设置它的物品种类,威力更强的子弹和炸弹,并且构造一个物品出现动画的函数,这个动画函数很简单,都是CCMove构成。。。

GameScene.h

void addBulletTypeTip();        //空降物品时间计数    int propTime;        ChangeBullet *prop;        bool isVisible;    bool isBigBullet;    bool isChangeBullet;        //子弹持续时间    int bulletlastTime;        void bulletLastTime();
GameScene.cpp

//空降物void GameLayer::addBulletTypeTip(){    propTime++;        if (propTime>500)    {                prop=ChangeBullet::create();                prop->initWithType((prosType)(arc4random()%2 + 4));                this->addChild(prop->prop);                prop->propAnimation();                prop->retain();                propTime=0;                isVisible=true;                    }}void GameLayer::bulletLastTime(){    if (isBigBullet)    {        if (bulletlastTime > 0)        {            bulletlastTime --;        }        else        {            bulletlastTime = 1200;            isBigBullet = false;            isChangeBullet = true;        }    }}
然后在我们的update函数中

this->addBulletTypeTip();            this->bulletLastTime();

在collisionDetection函数中

//飞机跟空降物    if (isVisible==true)    {        CCRect playRect=playSprite->boundingBox();        CCRect propRect=prop->prop->boundingBox();                        if (playRect.intersectsRect(propRect))        {                        prop->prop->stopAllActions();            prop->prop->removeFromParentAndCleanup(true);                        isVisible=false;                        //换子弹            if (prop->bulletType==propsTypeBullet)            {               // CCLOG("--------22222");                                isBigBullet = true;                isChangeBullet = true;                            }            //炸弹            else            {                            for (int i=0; i
count(); i++) { EnemyPlane *enemyPlane=(EnemyPlane *)planeArray->objectAtIndex(i); //爆炸动画 this->enemyPlaneBlowupAnimation(enemyPlane); } planeArray->removeAllObjects(); } } }

OK。。。看下效果图

 

OK,到现在我们还没做自己飞机的碰撞,那么接下来我们在collisionDetection函数中,再加入一个判断,判断敌人的飞机跟我们自己的飞机是否碰撞了,上代码

CCRect playRec=playSprite->boundingBox();    playRec.origin.x += 25*2;    playRec.size.width -= 50*2;    playRec.origin.y -= 10*2;    playRec.size.height -= 10*2;        for (int i=0; i
count(); i++) { EnemyPlane *enemyPlane=(EnemyPlane *)planeArray->objectAtIndex(i); if (playRec.intersectsRect(enemyPlane->boundingBox())) { this->playerBlowupAnimation(); this->enemyPlaneBlowupAnimation(enemyPlane); this->gameOver(); planeArray->removeObject(enemyPlane); } }
//自己的飞机爆炸动画void GameLayer::playerBlowupAnimation(){    playSprite->stopAllActions();                CCArray *array=CCArray::create();        for (int i=1; i<=4; i++)    {                CCString *string=CCString::createWithFormat("hero_blowup_%i.png",i);        CCSpriteFrame *frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(string->getCString());                array->addObject(frame);            }        CCAnimation *animation=CCAnimation::createWithSpriteFrames(array,0.1);    CCAnimate *animate=CCAnimate::create(animation);        CCRepeatForever *ac1=CCRepeatForever::create(animate);        playSprite->runAction(ac1);        array->removeAllObjects();}
上面中,我们做了碰撞检测,一旦飞机跟敌人的飞机撞上,我们就认为游戏结束了,所以最后我们加上个游戏结束的画面

//游戏结束void GameLayer::gameOver(){    isGameOver=true;            this->gamePause();    CCLayerColor *gameoverLayer=CCLayerColor::create(ccc4(215, 221, 222, 255), 582, 544);            gameoverLayer->setPosition(ccp(wSize.width/2-582/2, 220));    this->addChild(gameoverLayer, 3);        CCLabelTTF *ttfLabel=CCLabelTTF::create("飞机大战分数", "MarkerFelt-Thin", 50);    ttfLabel->setPosition(ccp(gameoverLayer->getContentSize().width/2-ttfLabel->getContentSize().width/2, gameoverLayer->getContentSize().height-70));    ttfLabel->setColor(ccc3(0, 0, 0));    ttfLabel->setAnchorPoint(ccp(0, 0));    gameoverLayer->addChild(ttfLabel, 1);                CCLabelTTF *scLabel=CCLabelTTF::create(scoreLabel->getString(), "MarkerFelt-Thin", 44);    scLabel->setPosition(ccp(gameoverLayer->getContentSize().width/2-scLabel->getContentSize().width/2, gameoverLayer->getContentSize().height-250));    scLabel->setColor(ccc3(0, 0, 0));    scLabel->setAnchorPoint(ccp(0, 0));    gameoverLayer->addChild(scLabel, 1);                CCMenuItemFont *startItem=CCMenuItemFont::create("继续", this,menu_selector(GameLayer::restart));        startItem->setPosition(ccp(gameoverLayer->getContentSize().width/2, 60));    startItem->setFontSizeObj(50);    startItem->setFontNameObj("Georgia-Bold");    startItem->setColor(ccc3(0, 0, 0));        CCMenu *pMenu = CCMenu::create(startItem, NULL);        pMenu->setPosition(CCPointZero);        gameoverLayer->addChild(pMenu, 1);            }//游戏暂停void GameLayer::gamePause(){    if (isGameOver==false)    {                                    }    else    {                CCObject *object;                CCARRAY_FOREACH(this->getChildren(), object)        {                        CCNode *node=(CCNode *)object;                    node->stopAllActions();                }                            }}

OK.。。。到这里,基本上大体的功能有了,呵呵,貌似暂停还木有。。。。

下篇文章中,我们将来探究一下在ios平台上适配的问题,当然还有android等其他平台的适配,。。。。。。。。。

转载地址:https://blog.csdn.net/kuloveyouwei/article/details/11369883 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:cocos2d-x的初步学习二十二之模仿微信打飞机
下一篇:ios中uitableview显示两列或多列数据

发表评论

最新留言

很好
[***.229.124.182]2024年03月15日 08时24分22秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

iview 自定义时间选择器组件_Vue.js中使用iView日期选择器并设置开始时间结束时间校验功能... 2019-04-21
java 验证码校验_JavaWeb验证码校验功能代码实例 2019-04-21
java多线程初学者指南_Java多线程初学者指南(4):线程的生命周期 2019-04-21
java进程user是jenkins_java 学习:在java中启动其他应用,由jenkins想到的 2019-04-21
java添加资源文件_如何在eclipse中将资源文件夹添加到我的Java项目中 2019-04-21
java的三种修饰符_3分钟弄明白JAVA三大修饰符 2019-04-21
mysql source skip_redis mysql 中的跳表(skip list) 查找树(btree) 2019-04-21
java sun.org.mozilla_maven编译找不到符号 sun.org.mozilla.javascript.internal 2019-04-21
php curl 输出到文件,PHP 利用CURL(HTTP)实现服务器上传文件至另一服务器 2019-04-21
PHP字符串运算结果,PHP运算符(二)"字符串运算符"实例详解 2019-04-21
PHP实现 bcrypt,如何使php中的bcrypt和Java中的jbcrypt兼容 2019-04-21
php8安全,PHP八大安全函数解析 2019-04-21
php基础语法了解和熟悉的表现,PHP第二课 了解PHP的基本语法以及目录结构 2019-04-21
matlab中lag函数用法,MATLAB movavg函数用法 2019-04-21
matlab变形监测,基于matlab的变形监测数据处理与分析_毕业设计论文 2019-04-21
opencv matlab编程,在Matlab中调用OpenCV函数 | 学步园 2019-04-21
c语言文件wt,c语言,wt和rt中的t是什么意思 2019-04-21
c语言运行几进制,【C语言】求已知等式在几进制条件下成立 2019-04-21
电梯运行仿真c语言代码,电梯调度算法模拟(示例代码) 2019-04-21
android组件动态接收数据库,Android开发——fragment中数据传递与刷新UI(更改控件)... 2019-04-21