怎样制作基于cocos2d x视频教程-x的SLG游戏

4369人阅读
& & & & 在第一篇《》基础上,增加旋转炮塔功能,原文《》,在这里继续以Cocos2d-x进行实现。有关源码、资源等在文章下面给出了地址。
步骤如下:
1.使用上一篇的工程;
2.下载本游戏所需的资源,将资源放置&Resources&目录下:
删除旧的资源player.png和projectile.png;
3.在HelloWorldScene.cpp文件,init函数,修改创建玩家精灵:
CCSprite&*player&=&CCSprite::create(&player2.png&);
在ccTouchesEnded函数,修改创建子弹精灵:
CCSprite&*projectile&=&CCSprite::create(&projectile2.png&);
4.编译运行,可以看到炮塔发射出了子弹,但是有一点奇怪,射击的时候,炮塔并没有朝向那个方向,如下图所示:
5.接下去,就是让炮塔可以旋转射击。在HelloWorldScene.h文件中,添加如下代码:
cocos2d::CCSprite&*_
修改HelloWorldScene.cpp文件中的init函数,如下:
_player&=&CCSprite::create(&player2.png&);
_player-&setPosition(ccp(_player-&getContentSize().width&/&2,&winSize.height&/&2));
this-&addChild(_player);&
6.计算炮塔旋转的角度。看下面图:
数学的知识就是,tan(angle) = 对边 / 邻边,利用反正切angle = arctan(对边 / 邻边),这时计算出的是弧度,用CC_RADIANS_TO_DEGREES宏转换成角度。另外在数学中,逆时针为正,在Cocos2D-x中,顺时针为正,就如下图所示:
需要将最后计算出的角度乘以-1。在ccTouchesEnded函数里,添加如下代码在projectile精灵runAction之前:
float&angleRadians&=&atanf((float)offRealY&/&(float)offRealX);
float&angleDegrees&=&CC_RADIANS_TO_DEGREES(angleRadians);
float&cocosAngle&=&-1&*&angleD
_player-&setRotation(cocosAngle);
7.编译运行,这时就可以看到炮塔旋转射击了。如下图所示:
8.旋转再射击。炮塔的旋转是瞬间完成的,这不符合现实,需要让它有个动作移动炮塔的方向。在HelloWorldScene.h文件中,添加如下声明:
cocos2d::CCSprite&*_nextP
在构造函数里面,添加如下:
_player&=&NULL;
_nextProjectile&=&NULL;
修改ccTouchesEnded函数,并且添加finishShoot方法,代码如下:
void&HelloWorld::ccTouchesEnded(CCSet&*pTouches,&CCEvent&*pEvent)
&&&&if&(_nextProjectile&!=&NULL)
&&&&&&&&return;
&&&&CCTouch&*touch&=&(CCTouch*)pTouches-&anyObject();
&&&&CCPoint&location&=&this-&convertTouchToNodeSpace(touch);
&&&&CCSize&winSize&=&CCDirector::sharedDirector()-&getWinSize();
&&&&_nextProjectile&=&CCSprite::create(&projectile2.png&);
&&&&_nextProjectile-&retain();
&&&&_nextProjectile-&setPosition(ccp(20,&winSize.height&/&2));
&&&&CCPoint&offset&=&ccpSub(location,&_nextProjectile-&getPosition());
&&&&if&(offset.x&&=&0)
&&&&&&&&return;
&&&&int&realX&=&winSize.width&+&_nextProjectile-&getContentSize().width&/&2;
&&&&float&ratio&=&(float)offset.y&/&(float)offset.x;
&&&&int&realY&=&realX&*&ratio&+&_nextProjectile-&getPosition().y;
&&&&CCPoint&realDest&=&ccp(realX,&realY);
&&&&int&offRealX&=&realX&-&_nextProjectile-&getPosition().x;
&&&&int&offRealY&=&realY&-&_nextProjectile-&getPosition().y;
&&&&float&length&=&sqrtf(offRealX&*&offRealX&+&offRealY&*&offRealY);
&&&&float&velocity&=&<span style="color:#ff&/&1;
&&&&float&realMoveDuration&=&length&/&
&&&&float&angleRadians&=&atanf((float)offRealY&/&(float)offRealX);
&&&&float&angleDegrees&=&CC_RADIANS_TO_DEGREES(angleRadians);
&&&&float&cocosAngle&=&-1&*&angleD
&&&&float&rotateDegreesPerSecond&=&<span style="color:#ff&/&0.5;
&&&&float&degreesDiff&=&_player-&getRotation()&-&cocosA
&&&&float&rotateDuration&=&fabs(degreesDiff&/&rotateDegreesPerSecond);
&&&&_player-&runAction(CCSequence::create(CCRotateTo::create(rotateDuration,&cocosAngle),
&&&&&&&&CCCallFunc::create(this,&callfunc_selector(HelloWorld::finishShoot)),&NULL));
&&&&_nextProjectile-&runAction(CCSequence::create(CCMoveTo::create(realMoveDuration,&realDest),&
&&&&&&&&CCCallFuncN::create(this,&callfuncN_selector(HelloWorld::spriteMoveFinished)),&NULL));
&&&&_nextProjectile-&setTag(2);
&&&&CocosDenshion::SimpleAudioEngine::sharedEngine()-&playEffect(&pew-pew-lei.wav&);
void&HelloWorld::finishShoot()
&&&&this-&addChild(_nextProjectile);
&&&&_projectiles-&addObject(_nextProjectile);
&&&&_nextProjectile-&release();
&&&&_nextProjectile&=&NULL;
在函数开头检验_nextProjectile变量,如果非空表示炮塔正在旋转中。不把_nextProjectile立即加到场景中,等待旋转完毕再加入场景。炮塔旋转的速度,为半秒钟旋转半个圆,计算所旋转角度所需的时间。
9.编译运行,可以看到炮塔可以在旋转后进行射击了,如下图所示:
参考资料:
1.How To Make A Simple iPhone Game with Cocos2D 2.X Part 2
2.(译)如何使用cocos2d开发一个简单的iphone游戏:旋转炮塔。(第二部分)
非常感谢以上资料,本例子源代码附加资源下载地址:
如文章存在错误之处,欢迎指出,以便改正。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1427181次
积分:18194
积分:18194
排名:第148名
原创:359篇
转载:86篇
评论:2896条
联系方式:
(1)(2)(1)(3)(3)(3)(1)(1)(3)(1)(1)(8)(1)(3)(2)(2)(3)(2)(3)(2)(1)(1)(2)(3)(6)(1)(4)(3)(1)(3)(5)(5)(5)(5)(1)(3)(5)(4)(4)(4)(5)(5)(1)(13)(11)(7)(5)(2)(5)(4)(5)(2)(7)(14)(18)(23)(19)(5)(35)(22)(21)(8)(10)(42)(49)如何使用Cocos2d-x 3.0制作基于tilemap的游戏_百度知道
如何使用Cocos2d-x 3.0制作基于tilemap的游戏
提问者采纳
创建工程骨架让我们首先创建整个工程的骨架,这样可以确保今后我们需要的文件都包含进来了,并且能够跑起来。首先工程命名为TileGame。接下来,下载游戏资源文件。这个资源文件包里包含了以下内容:玩家sprite。这个图片和《如何用Cocos2d-x3.0制作一款简单的游戏》差不多。我使用cxfr这个工具制作的一些音效。我使用Garage Band制作的一些背景音乐。(查看这篇博文获得更多的信息)我们将会使用的tile集合--它实际上会和tile地图编辑器一块儿使用,但是,我想把它放在这里,余下的事情会变得更容易。一些额外的“特殊”的tile,我将会在后面加以说明。一旦你获得了这些资源,解压并把它拖到你的工程的“Resources”分组下面。(编者的话:上面的音频资源都被编者转成了mp3格式)如果一切顺利,所有的文件应该都在你的工程里了。是时候制作我们的地图了!使用Tile来制作地图Cocos2d-x支持使用Tile地图编辑器创建的TMX格式的地图。(建议大家在安装的时候选择英文,本教程的Tile采用英文的)下载完之后,直接双击运行。点击File&#92;New,然后会出现以下对话框:在 orientation部分,你可以选择Orthogonal。Layer format我们也选默认的 Base64(zlib compressed)。接下来,设置地图的大小。记住,这个大小是以tile为单位的,而不是以像素为单位。我们将创建一个尽量小的地图,因此选择50*50。最后,你指定每个tile的宽度和高度。你这里选择的宽度和高度要根据你的实际的tile图片的尺寸来做。这个教程使用的样例tile的尺寸是32*32,所以在上面的选项中选择32*32.接下来,我们把制作地图所需要的tile集合导入进来。点击菜单栏上面的“map”菜单,“New Tileset...”,然后会出现下面的窗口:为了获得图片,点击“Browse...”按钮,然后定位到工程的的Resources文件夹,选择 tmw_desert_spacing.png文件(我们刚才解压进去的),然后加到工程中去。它会基于文件名自动填充名称。然后把新图快 名称命名为“tmw_desert_spacing.png”.同时,设置下面的Tile spacing和Margin都为1。你可以保留宽度和高度为32*32,因为tile的实际大小也是这么多。至于margin和spacing,我还没找到任何好的文档解释如何设置这两个值,下面是我的个人看法:Margin就是当前的tile计算自身的像素的时候,它需要减去多少个像素(宽度和高度都包含在内)。(类比word、css的margin)Spacing 就是相邻两个tile之间的间隔(同时考虑宽度和高度)(类比word、css的spacing)  如果你看看 tmw_desert_spacing.png,你将会看见每一个tile都有一个像素的空白边界围绕着,这意味着我们需要把margin和spacing设置为1。一旦你选择ok,你将会看到Tilesets窗口中显示了一些tiles。现在,你可以制作地图了!在Tilesets小窗口,选择一个tile,然后再在地图上的任意位置单击,你就会看到你选中的tile出现在点中的地方了。因此,继续制作地图吧---充分发挥你的聪明才智!确保增加至少一对建筑物在地图上,因为后面我们需要一些东西来做碰撞。记住一些方便的快捷方式:你可以在Tileset拾取器中拖出一个方框,一次选取多个tile。你可以使用工具栏上的“Bucket Fill Tools”按钮(就是一个桶那个)来基于一个基准tile绘制整个地图。你可以使用“View&#92;Zoom In...”和“View&#92;Zoom out...”来放大和缩小地图。一旦你完成了地图的绘制工作,在Layers选项卡的层上面双击(现在可以说是“Layer1”),然后重命名为“Background”。然后点击“File&#92;Save”并且保存文件到你的工程的资源文件夹中,并且命名为“TileMap.tmx”。后面我们将会使用这个tmx来做一些有趣的事情,好了,让我们把地图加载到游戏中去吧!把tile地图添加到Cocos2d-x的场景中打开HelloWorldScene.h,然后添加一些成员变量:1234
cppprivate:
cocos2d::TMXTiledMap *_tileM
cocos2d::TMXLayer *_然后在HelloWorldScene.cpp文件中做如下修改:123456789101112131415161718
cpp// Replace the init method with the followingbool HelloWorld::init(){
if ( !Layer::init() )
std::string file = &TileMap.tmx&;
auto str = String::createWithContentsOfFile(FileUtils::getInstance()-&fullPathForFilename(file.c_str()).c_str());
_tileMap = TMXTiledMap::createWithXML(str-&getCString(),&&);
_background = _tileMap-&layerNamed(&Background&);
addChild(_tileMap, -1);
}这里,我们调用TMXTiledMap类的一些方法,把我们刚刚创建的地图文件加载进去。一些简明的TMXTiledMap的背景知识。它是一个Node,你可以设置它的位置和比例等。这个地图的孩子是一些层,而且提供了一个帮助函数可以让你通过层的名字得到层对象--我们上面就是通过这种方面获得地图背景的。每一个层都是一个SpriteSheet的子类,这里考虑了性能的原因--但是这也意味着每一个层只能有一个tile集。因此,我们这里做的所有这些,就是指向一个tile地图,然后保存背景层的引用,并且把tile地图加到HelloWorld层中。好了,就这么多!
其他类似问题
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁14774人阅读
&&&&& 本文实践自 Ray Wenderlich 的文章《》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.0.4进行学习和移植,前者是用Object-C所写,所以移植到Cocos2D-x会有些差异,比如某些函数、某些功能不能跟原文一样直接实现,需另转换方法实现。之前已经对Cocos2D-x的安装以及简单使用进行了介绍,这里不再介绍,直接进入主题。
步骤如下:
1.新建Cocos2d-win32工程,工程名为&SimpleGame&,去除&Box2D&选项,勾选&Simple Audio Engine in Cocos Denshion&选项;
2.编译运行,可以看到如下图所示:
3.下载本游戏所需的资源,将资源放置&Resources&目录下;
4.游戏需要一个白色的背景,最简单的方法是使用CCLayerColor,将HelloWorldScene.h文件&HelloWorld&类改为如下:&
class&HelloWorld&:&public&cocos2d::CCLayerColor
首先添加玩家,让玩家位于左边屏幕中间,将HelloWorldScene.cpp文件的init函数,改为如下:
bool&HelloWorld::init()
&&&&bool&bRet&=&false;
&&&&&&&&CC_BREAK_IF(!&CCLayerColor::initWithColor(ccc4(<span style="color:#ff,&<span style="color:#ff,&<span style="color:#ff,&<span style="color:#ff)));
&&&&&&&&CCSize&winSize&=&CCDirector::sharedDirector()-&getWinSize();
&&&&&&&&CCSprite&*player&=&CCSprite::create(&player.png&,&CCRectMake(0,&0,&27,&40));
&&&&&&&&player-&setPosition(ccp(player-&getContentSize().width&/&2,&winSize.height&/&2));
&&&&&&&&this-&addChild(player);&&&
&&&&&&&&bRet&=&true;
&&&&}&while&(0);
&&&&return&bR
5.编译运行,可以看到玩家精灵在白色背景上,如下图所示:
6.接下来添加怪物,并且让怪物可以移动,我们在屏幕右边创建怪物,建立动作让它们向左移动,增加addMonster方法,代码如下:
void&HelloWorld::addMonster()
&&&&CCSprite&*monster&=&CCSprite::create(&monster.png&);
&&&&CCSize&winSize&=&CCDirector::sharedDirector()-&getWinSize();
&&&&int&minY&=&monster-&getContentSize().height&/&2;
&&&&int&maxY&=&winSize.height&-&monster-&getContentSize().height&/&2;
&&&&int&rangeY&=&maxY&-&minY;
&&&&int&actualY&=&(rand()&%&rangeY)&&#43;&minY;
&&&&monster-&setPosition(ccp(winSize.width&&#43;&monster-&getContentSize().width&/&2,&actualY));
&&&&this-&addChild(monster);
&&&&int&minDuration&=&2.0;
&&&&int&maxDuration&=&4.0;
&&&&int&rangeDuration&=&maxDuration&-&minD
&&&&int&actualDuration&=&(rand()&%&rangeDuration)&&#43;&minD
&&&&CCMoveTo&*actionMove&=&CCMoveTo::create(actualDuration,&ccp(-monster-&getContentSize().width&/&2,&actualY));
&&&&CCCallFuncN&*actionMoveDone&=&CCCallFuncN::create(this,&callfuncN_selector(HelloWorld::spriteMoveFinished));
&&&&monster-&runAction(CCSequence::create(actionMove,&actionMoveDone,&NULL));
在右边屏幕以随机的位置添加怪物精灵,注意计算精灵的位置坐标,默认描点在中心,不要让怪物截断了。然后再以2~4秒的随机总时间,让怪物从右边移动到左边,移动出边界后,即回调函数spriteMoveFinished,进行删除精灵对象,增加的spriteMoveFinished方法如下:
void&HelloWorld::spriteMoveFinished(CCNode&*sender)
&&&&CCSprite&*sprite&=&(CCSprite*)
&&&&this-&removeChild(sprite,&true);
接下去就是定时创建怪物,在init函数返回之前,安装定时器,每秒执行一次,代码如下:
this-&schedule(schedule_selector(HelloWorld::gameLogic),&1.0);
增加gameLogic方法,代码如下:
void&HelloWorld::gameLogic(&float&dt&)
&&&&this-&addMonster();
7.编译运行,可以看到右边怪物定时增加,并且以不同的速度向左边移动,如下图所示:
8.接着让玩家可以射击子弹,当用户在屏幕点击时,就让玩家往点击的方向进行发送子弹,用户的屏幕点击点并不是子弹移动的最终地,借用原文的一张图片来说明:
要让层可以支持触摸,需要在init方法,添加如下代码:
this-&setTouchEnabled(true);
然后重载ccTouchesEnded方法,代码如下:
void&HelloWorld::ccTouchesEnded(CCSet&*pTouches,&CCEvent&*pEvent)
&&&&CCTouch&*touch&=&(CCTouch*)pTouches-&anyObject();
&&&&CCPoint&location&=&this-&convertTouchToNodeSpace(touch);
&&&&CCSize&winSize&=&CCDirector::sharedDirector()-&getWinSize();
&&&&CCSprite&*projectile&=&CCSprite::create(&projectile.png&);
&&&&projectile-&setPosition(ccp(20,&winSize.height&/&2));
&&&&CCPoint&offset&=&ccpSub(location,&projectile-&getPosition());
&&&&if&(offset.x&&=&0)
&&&&&&&&return;
&&&&this-&addChild(projectile);
&&&&int&realX&=&winSize.width&&#43;&projectile-&getContentSize().width&/&2;
&&&&float&ratio&=&(float)offset.y&/&(float)offset.x;
&&&&int&realY&=&realX&*&ratio&&#43;&projectile-&getPosition().y;
&&&&CCPoint&realDest&=&ccp(realX,&realY);
&&&&int&offRealX&=&realX&-&projectile-&getPosition().x;
&&&&int&offRealY&=&realY&-&projectile-&getPosition().y;
&&&&float&length&=&sqrtf(offRealX&*&offRealX&&#43;&offRealY&*&offRealY);
&&&&float&velocity&=&<span style="color:#ff&/&1;
&&&&float&realMoveDuration&=&length&/&
&&&&projectile-&runAction(CCSequence::create(CCMoveTo::create(realMoveDuration,&realDest),&
&&&&&&&&CCCallFuncN::create(this,&callfuncN_selector(HelloWorld::spriteMoveFinished)),&NULL));
首先,得到触摸点,然后创建子弹精灵,算出触摸点与子弹初始位置之差,若触摸点在初始位置的前方(即玩家前方),则添加子弹到层上。以同比例方法,计算出子弹飞向屏幕右边的最终坐标。然后再用勾股定理计算飞行长度,假定速度为每秒480像素,则计算出飞行总时间。之后就是让子弹执行给定的飞行动作,以及之后的删除自身调用。
9.编译运行,往屏幕点击,可以看到子弹发射出去,如下图所示:
10.当子弹碰到怪物时,怪物被消灭,子弹消失,即碰撞检测。需要在场景中跟踪目标和子弹,在HelloWorldScene.h声明如下:
cocos2d::CCArray&*_
cocos2d::CCArray&*_
在构造函数和析构函数,添加如下:
HelloWorld::HelloWorld()
&&&&_monsters&=&NULL;
&&&&_projectiles&=&NULL;
HelloWorld::~HelloWorld()
&&&&if&(_monsters)
&&&&&&&&_monsters-&release();
&&&&&&&&_monsters&=&NULL;
&&&&if&(_projectiles)
&&&&&&&&_projectiles-&release();
&&&&&&&&_projectiles&=&NULL;
然后在init函数中初始化这两个数组:
this-&_monsters&=&CCArray::create();
this-&_monsters-&retain();
this-&_projectiles&=&CCArray::create();
this-&_projectiles-&retain();
修改addMonster函数,为怪物精灵添加标签,并加入到数组,代码如下:
monster-&setTag(1);
_monsters-&addObject(monster);
修改ccTouchesEnded函数,为子弹精灵添加标签,并加入到数组,代码如下:
projectile-&setTag(2);
_projectiles-&addObject(projectile);
然后修改spriteMoveFinished函数,增加如下代码:
if&(sprite-&getTag()&==&1)
&&&&_monsters-&removeObject(sprite);
else&if&(sprite-&getTag()&==&2)
&&&&_projectiles-&removeObject(sprite);
添加如下方法:
void&HelloWorld::update(float&dt)
&&&&CCArray&*projectilesToDelete&=&CCArray::create();
&&&&CCObject&*pObject&=&NULL;
&&&&CCObject&*pObject2&=&NULL;&
&&&&CCARRAY_FOREACH(_projectiles,&pObject)
&&&&&&&&CCSprite&*projectile&=&(CCSprite*)pO
&&&&&&&&CCArray&*monstersToDelete&=&CCArray::create();
&&&&&&&&CCARRAY_FOREACH(_monsters,&pObject2)
&&&&&&&&&&&&CCSprite&*monster&=&(CCSprite*)pObject2;
&&&&&&&&&&&&if&(CCRect::CCRectIntersectsRect(projectile-&boundingBox(),&monster-&boundingBox()))
&&&&&&&&&&&&{
&&&&&&&&&&&&&&&&monstersToDelete-&addObject(monster);
&&&&&&&&&&&&}&&&&&&&&&&&
&&&&&&&&CCARRAY_FOREACH(monstersToDelete,&pObject2)
&&&&&&&&&&&&CCSprite&*monster&=&(CCSprite*)pObject2;
&&&&&&&&&&&&_monsters-&removeObject(monster);
&&&&&&&&&&&&this-&removeChild(monster,&true);
&&&&&&&&if&(monstersToDelete-&count()&&&0)
&&&&&&&&&&&&projectilesToDelete-&addObject(projectile);
&&&&&&&&monstersToDelete-&release();
&&&&CCARRAY_FOREACH(projectilesToDelete,&pObject)
&&&&&&&&CCSprite&*projectile&=&(CCSprite*)pO
&&&&&&&&_projectiles-&removeObject(projectile);
&&&&&&&&this-&removeChild(projectile,&true);
&&&&projectilesToDelete-&release();
遍历子弹数组,计算每一个子弹所可能遇到的怪物,用它们各自的边界框进行交叉检测,检测到交叉,则将怪物对象放入ToDelete(待删除)数组,不能在遍历的时候删除一个对象。若是子弹遇到了怪物,也需要放入ToDelete(待删除)数组。然后从场景和数组中移动掉。同样,也在init函数,安装定时器,代码如下:
this-&schedule(schedule_selector(HelloWorld::update));
11.编译运行,这时当子弹和怪物碰撞时,它们就会消失;
12.添加音效,在init函数添加背景音乐,代码如下:
CocosDenshion::SimpleAudioEngine::sharedEngine()-&playBackgroundMusic(&background-music-aac.wav&);
在ccTouchesEnded函数,添加子弹音效,代码如下:
CocosDenshion::SimpleAudioEngine::sharedEngine()-&playEffect(&pew-pew-lei.wav&);
13.接下来,创建一个新的场景,来指示&You Win&或者&You Lose&。右键 工程,&Add&→&Class...&→&C&#43;&#43;&→&Add&,&Base class&为CCLayerColor,&Class name&为GameOverLayer,如下图所示:
GameOverLayer.h文件代码为:
#pragma&once
#include&&cocos2d.h&
class&GameOverLayer&:
&&&&public&cocos2d::CCLayerColor
&&&&GameOverLayer(void);
&&&&~GameOverLayer(void);
&&&&bool&initWithWon(bool&won);
&&&&static&cocos2d::CCScene*&sceneWithWon(bool&won);
&&&&static&GameOverLayer*&createWithWon(bool&won);
&&&&void&gameOverDone();
GameOverLayer.cpp文件代码为:
#include&&GameOverLayer.h&
#include&&HelloWorldScene.h&
using&namespace&cocos2d;
GameOverLayer::GameOverLayer(void)
GameOverLayer::~GameOverLayer(void)
GameOverLayer*&GameOverLayer::createWithWon(bool&won)
&&&&GameOverLayer&*pRet&=&new&GameOverLayer();
&&&&if&(pRet&&&&pRet-&initWithWon(won))
&&&&&&&&pRet-&autorelease();
&&&&&&&&return&pR
&&&&&&&&CC_SAFE_DELETE(pRet);
&&&&&&&&return&NULL;
bool&GameOverLayer::initWithWon(bool&won)
&&&&bool&bRet&=&false;
&&&&&&&&CC_BREAK_IF(!&CCLayerColor::initWithColor(ccc4(<span style="color:#ff,&<span style="color:#ff,&<span style="color:#ff,&<span style="color:#ff)));
&&&&&&&&char&*
&&&&&&&&if&(won)
&&&&&&&&&&&&message&=&&You&Won!&;
&&&&&&&&}&
&&&&&&&&else
&&&&&&&&&&&&message&=&&You&Lose&:[&;
&&&&&&&&CCSize&winSize&=&CCDirector::sharedDirector()-&getWinSize();
&&&&&&&&CCLabelTTF&*label&=&CCLabelTTF::create(message,&&Arial&,&32);
&&&&&&&&label-&setColor(ccc3(0,&0,&0));
&&&&&&&&label-&setPosition(ccp(winSize.width&/&2,&winSize.height&/&2));
&&&&&&&&this-&addChild(label);
&&&&&&&&this-&runAction(CCSequence::create(CCDelayTime::create(3),&
&&&&&&&&&&&&CCCallFunc::create(this,&callfunc_selector(GameOverLayer::gameOverDone)),
&&&&&&&&&&&&NULL));
&&&&&&&&bRet&=&true;
&&&&}&while&(0);
&&&&return&bR
cocos2d::CCScene*&GameOverLayer::sceneWithWon(bool&won)
&&&&CCScene&*&scene&=&NULL;
&&&&&&&&scene&=&CCScene::create();
&&&&&&&&CC_BREAK_IF(!&scene);
&&&&&&&&GameOverLayer&*layer&=&GameOverLayer::createWithWon(won);
&&&&&&&&CC_BREAK_IF(!&layer);
&&&&&&&&scene-&addChild(layer);
&&&&}&while&(0);
&&&&return&
void&GameOverLayer::gameOverDone()
&&&&CCDirector::sharedDirector()-&replaceScene(HelloWorld::scene());
游戏结束时,切换到以上所建的场景,场景上的层显示一个文本,在3秒之后返回到HelloWorld场景中。
14.最后,为游戏添加一些游戏逻辑。记录玩家消灭怪物的数量,进而决定该玩家输赢。在HelloWorldScene.h文件中,添加如下:
int&_monstersD
在HelloWorldScene.cpp文件,HelloWorld()构造函数,添加如下代码:
_monstersDestroyed&=&0;
添加头文件引用:
#include&&GameOverLayer.h&
在update定时函数中,monstersToDelete循环removeChild(monster, true)的后面添加被消灭怪物的计数,并判断胜利条件,代码如下:
_monstersDestroyed&#43;&#43;;
if&(_monstersDestroyed&&&30)
&&&&CCScene&*gameOverScene&=&GameOverLayer::sceneWithWon(true);
&&&&CCDirector::sharedDirector()-&replaceScene(gameOverScene);
最后为玩家添加失败条件,规定只要有一只怪物跑到左边屏幕里,则玩家失败,在spriteMoveFinished函数里,sprite-&getTag() == 1条件的后面,添加如下:
CCScene&*gameOverScene&=&GameOverLayer::sceneWithWon(false);
CCDirector::sharedDirector()-&replaceScene(gameOverScene);
14.编译并运行,到此已完成了一个简单的游戏,包含音效,并带有胜利和失败的结束。游戏效果如下:
参考资料:
1.How To Make A Simple iPhone Game with Cocos2D 2.X Tutorial
2.如何用Cocos2d来开发简单的IPhone游戏教程
3.Cocos2d Classic Tutorial Demo Revisit:(1)
非常感谢以上资料,本例子源代码附加资源下载地址:
如文章存在错误之处,欢迎指出,以便改正。
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1427182次
积分:18194
积分:18194
排名:第148名
原创:359篇
转载:86篇
评论:2896条
联系方式:
(1)(2)(1)(3)(3)(3)(1)(1)(3)(1)(1)(8)(1)(3)(2)(2)(3)(2)(3)(2)(1)(1)(2)(3)(6)(1)(4)(3)(1)(3)(5)(5)(5)(5)(1)(3)(5)(4)(4)(4)(5)(5)(1)(13)(11)(7)(5)(2)(5)(4)(5)(2)(7)(14)(18)(23)(19)(5)(35)(22)(21)(8)(10)(42)(49)

我要回帖

更多关于 cocos2d x下载 的文章

 

随机推荐