在上一篇中。我们实现了游戏的開始界面,接下来要实现游戏的主界面。主界面包括地图、我方飞机、敌机等

先来实现我方飞机

我方飞机具有哪些属性呢? 飞机要具有生命值、要有动画效果(尾部喷气),飞机不可以飞出边界。所以要进行边界检測,当飞机生命值为0时。飞机会爆炸。然后被移除。

.h文件

//飞机动画
Animate* planeFly(); //边界检測
void borderCheck(float dt); //飞机爆炸
void blowUp(); //移除飞机
void removePlane(); //获取生命值
int getAlive(); //设定生命值
void loseAlive(); // 更新生命值
void updateAlive(int alive);

这个变量在create()函数中初始化,方便其它层调用我方飞机的相关数据

static MyPlane* instancePlane;	//飞机实例

我方飞机的生命值直接在这里显示、更新,不受控制器的控制

private:
int m_alive;
Label* aliveItem1;
Label* aliveItem2;

.cpp文件

/*
************************************************************************
*
* MyPlane.cpp
* 杜星飞 2015年8月13日
* 描写叙述: 包括飞机的属性、功能等
*
************************************************************************
*/ #include "MyPlane.h"
#include "SimpleAudioEngine.h" MyPlane::MyPlane() :m_alive(5)
{ }
MyPlane::~MyPlane()
{ } MyPlane* MyPlane::instancePlane = NULL; MyPlane* MyPlane::create()
{
MyPlane* m_plane = NULL;
do
{
m_plane = new MyPlane();
CC_BREAK_IF(!m_plane); if (m_plane && m_plane->init())
{
m_plane->autorelease();
instancePlane = m_plane;
}
else
CC_SAFE_DELETE(m_plane);
} while (0); return m_plane;
} //飞机动画
Animate* MyPlane::planeFly()
{
Vector<SpriteFrame *> vector;
for (int i = 0; i < 2; i++)
{
auto frameName = __String::createWithFormat("chinaFly%d.png", i + 1);
auto temSpriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName->getCString());
vector.pushBack(temSpriteFrame);
}
//设置不断播放飞机的动画
auto animation = Animation::createWithSpriteFrames(vector, 0.2f, -1);
auto animate = Animate::create(animation); return animate;
} bool MyPlane::init()
{
if(!Layer::init())
return false; Size winSize = Director::getInstance()->getWinSize(); //加入飞机
auto m_planeSprite = Sprite::createWithSpriteFrameName("chinaFly1.png");
m_planeSprite->setPosition(Point(winSize.width / 2, m_planeSprite->getContentSize().height / 2));
m_planeSprite->setTag(AIRPLANE);
this->addChild(m_planeSprite);
m_planeSprite->runAction(this->planeFly()); // 飞机触摸
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true); //吞噬触摸事件 //对触摸事件的监听过程直接写在这里
listener->onTouchBegan = [](Touch* touch, Event *event)
{
auto target = static_cast<Sprite*>(event->getCurrentTarget()); Point locationInNode = target->convertToNodeSpace(touch->getLocation());
Size s = target->getContentSize();
Rect rect = Rect(0, 0, s.width, s.height); if (rect.containsPoint(locationInNode))
return true;
else
return false;
}; listener->onTouchMoved = [](Touch* touch, Event *event)
{
auto target = static_cast<Sprite*>(event->getCurrentTarget());
target->setPosition(target->getPosition() + touch->getDelta());
}; listener->onTouchEnded = [](Touch* touch, Event* event)
{
}; //将触摸监听加入到eventDispacher中去
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, m_planeSprite); //初始化生命值 //设置标签 并 获取中文文本
auto dictionary = Dictionary::createWithContentsOfFile("fonts/AboutMe.xml"); aliveItem1 = Label::createWithTTF(
(((__String*)(dictionary->objectForKey("alive"))))->getCString(),
"fonts/DFPShaoNvW5-GB.ttf",
25);
aliveItem1->setPosition(Point(winSize.width/8, winSize.height-aliveItem1->getContentSize().height));
aliveItem1->setColor(Color3B(255, 0, 0));
this->addChild(aliveItem1); aliveItem2 = Label::createWithTTF(
"5",
"fonts/DFPShaoNvW5-GB.ttf",
25);
aliveItem2->setPosition(Point(aliveItem1->getPositionX()*2, winSize.height - aliveItem1->getContentSize().height));
aliveItem2->setColor(Color3B(255, 0, 0));
this->addChild(aliveItem2); // 开启边界检測
this->schedule(schedule_selector(MyPlane::borderCheck)); return true;
} //边界检測
void MyPlane::borderCheck(float dt)
{
//进行边界推断,不可超出屏幕
Point location = this->getChildByTag(AIRPLANE)->getPosition();
Size winSize = Director::getInstance()->getWinSize(); // 返回的就是这个矩形的大小
Size planeSize = this->getChildByTag(AIRPLANE)->getContentSize(); if (location.x<planeSize.width / 2)
location.x = planeSize.width / 2; if (location.x>winSize.width - planeSize.width / 2)
location.x = winSize.width - planeSize.width / 2; if (location.y<planeSize.height / 2)
location.y = planeSize.height / 2; if (location.y>winSize.height - planeSize.height / 2)
location.y = winSize.height - planeSize.height / 2; this->getChildByTag(AIRPLANE)->setPosition(location);
} //飞机爆炸
void MyPlane::blowUp()
{
this->unscheduleAllSelectors(); // 停止飞机的全部行动 //载入飞机爆炸动画 音效
if (CocosDenshion::SimpleAudioEngine::getInstance()->isBackgroundMusicPlaying())
{
CocosDenshion::SimpleAudioEngine::getInstance()->playEffect("sound/chinaDown.mp3");
} Vector<SpriteFrame*> planeBlowUp;
for (int i = 0; i < 4; i++)
{
auto planeName = __String::createWithFormat("china1_down%d.png", i + 1);
auto tempBlowUp = SpriteFrameCache::getInstance()->getSpriteFrameByName(
planeName->getCString());
planeBlowUp.pushBack(tempBlowUp);
} Animation* animation = Animation::createWithSpriteFrames(planeBlowUp, 0.2f);
Animate* animate = Animate::create(animation);
CallFunc* m_removePlane = CallFunc::create(this, callfunc_selector(MyPlane::removePlane));
Sequence* sequence = Sequence::create(animate, m_removePlane, NULL); // 停止一切的飞机动作
this->getChildByTag(AIRPLANE)->stopAllActions(); this->getChildByTag(AIRPLANE)->runAction(sequence);
} //移除飞机
void MyPlane::removePlane()
{
// 移除飞机精灵 true子节点上的全部执行行为和回调将清理
this->removeChildByTag(AIRPLANE, true);
} //获取生命值
int MyPlane::getAlive()
{
return m_alive;
} //设定生命值
void MyPlane::loseAlive()
{
--m_alive;
updateAlive(m_alive);
} // 更新生命值
void MyPlane::updateAlive(int alive)
{
if (alive >= 0)
{
CCString* strAlive = CCString::createWithFormat("%d", alive);
aliveItem2->setString(strAlive->getCString());
aliveItem2->setColor(Color3B(rand_0_1() * 255, rand_0_1() * 255, rand_0_1() * 255));
}
}

更新生命值的函数仅仅用在我方飞机生命值降低是调用。

还有就是对于中文字符的处理

//设置标签 并 获取中文文本
auto dictionary = Dictionary::createWithContentsOfFile("fonts/AboutMe.xml");

能够在项目中加入一个XML文件

<?xml version="1.0" encoding="UTF-8"?>
<dict>
<key>play</key>
<string>開始游戏</string>
<key>score</key>
<string>得分:</string>
<key>alive</key>
<string>生命:</string>

通过对应的key来显示显示对应的中文。

还有就是,有些字体不支持中文的显示,比方
系统自带的 arial.ttf就不行,而DFPShaoNvW5-GB.ttf能够。

【cocos2d-x 3.7 飞机大战】 决战南海I (二) 我方飞机的实现的更多相关文章

  1. 微信小游戏 demo 飞机大战 代码分析 (二)(databus.js)

    微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  2. js 飞机大战

    完整文件及代码可以在网盘下载,下载链接为:https://pan.baidu.com/s/1hs7sBUs 密码: d83x 飞机大战css定义: <style> #container{ ...

  3. 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)

    微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...

  4. 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)

    微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...

  5. 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)

    微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...

  6. android:怎样用一天时间,写出“飞机大战”这种游戏!(无框架-SurfaceView绘制)

    序言作为一个android开发人员,时常想开发一个小游戏娱乐一下大家,今天就说说,我是怎么样一天写出一个简单的"飞机大战"的. 体验地址:http://www.wandoujia. ...

  7. JS 实现飞机大战

    这是JS版本的飞机大战,和C#版本的思路相同,就是语言上有差别,用来巩固知识.可以将代码直接引入到HTML中就可以看到效果 //编写背景对象 function Background(width,hei ...

  8. 【cocos2d-x 3.7 飞机大战】 决战南海I (十) 游戏主场景

    主场景要包括其它类的头文件 #include "cocos2d.h" #include "MyPlane.h" #include "Bullet.h& ...

  9. 【cocos2d-x 3.7 飞机大战】 决战南海I (四) 敌机管理

    敌方飞机应该不定时的出现,有自己的生命周期.运动轨迹.这个类用来管理敌机的产生.移动.爆炸.销毁等. 敌机管理类主要函数例如以下 //绑定控制器(更新分数) void bindController(C ...

随机推荐

  1. axios统一拦截配置

    在vue项目中,和后台进行数据交互使用axios.要想统一处理所有的http请求和响应,就需要使用axios的拦截器.通过配置http response inteceptor 统一拦截后台的接口数据, ...

  2. XPATH怎么获取TITLE中有中文的标签

    定位 //*[@id="kkpager"]/div[1]/span[1]/a[@title="下一页"] 获取元素 txt4 = txt.xpath('//*[ ...

  3. windows下的ubuntu

    办公用Windows确实方便,但对于开发和学习还是用Linux比较好. 在Windows下安装Linux子系统 windows10中推出了Linux子系统,这个功能对开发和学习来说真的很好,非常方便. ...

  4. [luogu] P4155 [SCOI2015]国旗计划(贪心)

    P4155 [SCOI2015]国旗计划 题目描述 A 国正在开展一项伟大的计划 -- 国旗计划.这项计划的内容是边防战士手举国旗环绕边境线奔袭一圈.这项计划需要多名边防战士以接力的形式共同完成,为此 ...

  5. STM32 SPI 发送第一个数据不成功问题

    STM32的标准库,跟HAL库都是很实用的, 在使用SPI库的过程中一定要注意时序的问题. 我在调试SPI过程中,调试了两个IC,都是用HAL库, 第一个IC没出问题,第二个IC出现了第一次发送数据不 ...

  6. webpack的热更新

    webpack的热更新是如何做到的?说明其原理? webpack的热更新又称热替换(Hot Module Replacement),缩写为HMR. 这个机制可以做到不用刷新浏览器而将新变更的模块替换掉 ...

  7. ASP.NET-internat身份验证

    ASP.NET-internat身份验证默认在webconfig中配置的代码是这样的 <system.web> <compilation debug="true" ...

  8. HDU 4323 Contest 3

    编辑距离,经典的了.动态规划枚举即过. #include <iostream> #include <cstdio> #include <string.h> #inc ...

  9. 【iOS】UICollectionView自己定义Layout之蜂窝布局

    网上的UICollectionView的Layout布局,其cell的形状多为矩形和圆形. 本篇博文将正六边形作为cell的基本形状,为您展现独特的蜂窝布局效果及实现源代码. 帮助您让自己的App脱颖 ...

  10. Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七)

    如无书面授权,请勿转载 Larger Projects Until now, we have been looking at single plays in one playbook file. Th ...