您的位置:首页 > 移动开发 > Cocos引擎

使用CocosSharp制作一个游戏 - CocosSharp中文教程

2014-12-07 18:03 423 查看

注:本教程翻译自官方《Walkthrough - Building a game with CocosSharp》,官方教程有很多地方说的不够详细,或者代码不全,导致无法继续,本人在看了GoneBananas项目代码后,对本教程进行了部分修改,但当前只涉及Android方面,iOS因没有环境验证代码,暂未修改。


本人博客地址:http://fengyu.name

原文链接:http://fengyu.name/?cat=game&id=295

相关资源:


离线PDF文档:Download PDF

示例代码:Gone Bananas

相关链接:Cocos Sharp Samples,Cocos Sharp repo


本教程介绍了如何使用CocosSharp制作一个游戏,本教程将向你介绍如何使用CocosSharp制作一个游戏,CocosSharp是一个使用C#语言进行跨平台游戏开发的框架。

本教程展示了如何从建立一个项目到制作一个完整的游戏,以及CocosSharp中的各种概念,和如何使用它们创建游戏。CocosSharp概念包括:
sprites
(精灵),
actions
(动作),
sounds
(声音),
layers
(层),
parallas
(视差),
particle system
(粒子系统),
scenes
(场景)以及
physics
(物理)。

当你学习完后,你将已创建你的第一个CocosSharp游戏。

获得 CocosSharp

CocosSharp作为NuGet的一个PCL(Portable Class Library便携类库)存在,CocosSharp的源代码可以在GitHub上找到。

制作向导

在本教程中,你将建立一个名字叫做GoneBananas的游戏,这个游戏的目标是让猴子在屏幕上移动,并尽可能多的接到屏幕上向下掉的香蕉。游戏截图如下所示:

在本教程中我们将创建一个iOS及Android游戏。当然,CocosSharp也同样可以在许多其他平台良好运行。查看CocosSharp repo获得支持平台的完整列表。

本教程的第一部分,我们将介绍一些基本的操作,使用sprites,并让其显示在屏幕中。然后,我们将会介绍更多的高级概念以及它们如何使用,例如粒子系统与物理引擎。

创建项目

使用
iOS->iPhone Empty Project
模板,创建一个名为
GoneBananas
的新项目。

创建了项目之后,我们在解决方案中添加CocosSharp以及相关依赖文件。我们可以**从NuGet添加CocosSharp包**。

同样,使用
Android->Ice Cream Sandwich Application
模板新建一个名为
GoneBananasAndroid
的Android项目,并**添加CocosSharp的NuGet包**。

我们将使用一个共享项目来实现游戏逻辑,所以游戏逻辑将是跨平台可重用的。在解决方案中新建一个名为
GonesBananasShared
的共享项目。

之后,我们将资源文件夹复制到
GoneBananas
项目目录中,Android下,需要复制到
Assets
目录中。资源文件夹包含资源文件,例如字体、图片和声音。

上述工作结束后,我们创建游戏的准备工作就都完成了。

创建应用程序委托类Application Delegate

首先,我们需要创建CCApplicationDelegate的子类,CCApplicationDelegate的概念类似iOS中的UIApplicationDelegate。

这是事件用于控制程序的生命周期,大纲如下:

ApplicationDidFinishLaunching – 程序启动后执行

ApplicationDidEnterBackground – 当程序进入后台时,停止所有运行中的动画及音频

ApplicationWillEnterForeground – 当游戏恢复前台时,恢复所有因进入后台状态而被停止的动画及音频。

我们先将整个
content
文件夹复制到
GoneBananas
项目目录中,Android下,需要复制到
Assets
目录中。这个文件夹包含了资源文件,例如字体、图片和声音。

使用下面的代码新增一个名为
GoneBananasApplicationDelegate
的类:

using CocosDenshion;
using CocosSharp;

namespace GoneBananas
{
public class GoneBananasApplicationDelegate : CCApplicationDelegate
{

public override void ApplicationDidFinishLaunching (CCApplication application, CCWindow mainWindow)
{
//关闭多重采样
application.PreferMultiSampling = false;
//默认文件目录
application.ContentRootDirectory = "Content";
//设置屏幕显示方式(横屏或竖屏)
mainWindow.SupportedDisplayOrientations = CCDisplayOrientation.Portrait;
//添加文件搜索目录
application.ContentSearchPaths.Add ("hd");
//音频引擎加载声音文件
CCSimpleAudioEngine.SharedEngine.PreloadEffect ("Sounds/tap");
//创建场景
CCScene scene = GameStartLayer.GameStartLayerScene (mainWindow);
//运行场景
mainWindow.RunWithScene (scene);
}

public override void ApplicationDidEnterBackground (CCApplication application)
{
// stop all of the animation actions that are running.
application.Paused = true;

// if you use SimpleAudioEngine, your music must be paused
CCSimpleAudioEngine.SharedEngine.PauseBackgroundMusic ();
}

public override void ApplicationWillEnterForeground (CCApplication application)
{
application.Paused = false;

// if you use SimpleAudioEngine, your background music track must resume here.
CCSimpleAudioEngine.SharedEngine.ResumeBackgroundMusic ();
}
}
}

GoneBananasApplicationDelegate
ApplicationFinishedLaunching
方法中,我们设置了
application.ContentRootDirectory
content
文件夹,并对
mainWindow
进行了初始化,使其默认以竖屏状态运行,之后预加载了一个音频特效,并创建了场景实例已经调用他。但当前我们并没有创建
GameStartLayer
类,这个会在之后创建。

CCApplication类

CCApplicaiton类用来开始游戏,我们需要在不同平台项目中都创建它。这是唯一在各种平台具体项目中都需要的代码。

在iOS中,使用以下代码替换AppDelegade内容:

using System;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
using CocosSharp;

namespace GoneBananas
{
[Register ("AppDelegate")]
public partial class AppDelegate : UIApplicationDelegate
{
public override void FinishedLaunching (UIApplication app)
{
var application = new CCApplication ();
application.ApplicationDelegate = new GoneBananasApplicationDelegate ();
application.StartGame ();
}
}
}

同样,对于Android,使用以下代码替换MainActivity的内容:

using System;
using Android.App;
using Android.Content;
using Android.Content.PM;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using CocosSharp;
using Microsoft.Xna.Framework;
using GoneBananas;

namespace GoneBananasAndroid
{
[Activity(
Label = "GoneBananas",
AlwaysRetainTaskState = true,
Icon = "@drawable/ic_launcher",
Theme = "@android:style/Theme.NoTitleBar",
ScreenOrientation = ScreenOrientation.Portrait,
LaunchMode = LaunchMode.SingleInstance,
MainLauncher = true,
ConfigurationChanges =  ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden)
]
public class MainActivity : AndroidGameActivity
{
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

var application = new CCApplication();
application.ApplicationDelegate = new GoneBananasApplicationDelegate();
SetContentView(application.AndroidContentView);
application.StartGame();
}
}
}

创建第一个场景

之前在
GoneBananasApplicationDelegate
类中,我们使用下面的代码创建第一个场景的实例:

CCScene scene = GameStartLayer.GameStartLayerScene (mainWindow);

CocosSharp使用在
CCScene
类中实现的scenes场景,用来管理游戏中各种各样的逻辑。每一个scene场景都包含layers层,而layer层用来显示在scene中的用户界面。每一个layer层都用来返回他的父场景。例如,在游戏中,我们创建三个层:

GameStartLayer – 提供引导界面,点击后将开始游戏

GameLayer – 游戏内容层

GameOverLayer –游戏结束时显示的界面,点击后将重新开始游戏

我们使用以下代码新建一个
GameStartLayer
类:

using System;
using System.Collections.Generic;
using CocosSharp;

namespace GoneBananas
{
public class GameStartLayer : CCLayerColor
{
public GameStartLayer () : base ()
{
var touchListener = new CCEventListenerTouchAllAtOnce ();
touchListener.OnTouchesEnded = (touches, ccevent) => Window.DefaultDirector.ReplaceScene (GameLayer.GameScene (Window));

AddEventListener (touchListener, this);

Color = CCColor3B.Black;
Opacity = 255;
}

protected override void AddedToScene ()
{
base.AddedToScene ();

Scene.SceneResolutionPolicy = CCSceneResolutionPolicy.ShowAll;

var label = new CCLabelTtf ("Tap Screen to Go Bananas!", "arial", 22) {
Position = VisibleBoundsWorldspace.Center,
Color = CCColor3B.Green,
HorizontalAlignment = CCTextAlignment.Center,
VerticalAlignment = CCVerticalTextAlignment.Center,
AnchorPoint = CCPoint.AnchorMiddle,
Dimensions = ContentSize
};

AddChild (label);
}

public static CCScene GameStartLayerScene (CCWindow mainWindow)
{
var scene = new CCScene (mainWindow);
var layer = new GameStartLayer ();

scene.AddChild (layer);

return scene;
}
}
}

我们使用一个
CCLayerColor
作为基础类,所以我们可以直接设置层的背景颜色。层中显示一个文本框,并将在点击屏幕后切换到GameLayer层界面。这个文本框使用了我们之前复制到content文件夹中fonts文件夹下的arial字体。

下面的截图展示完成后的场景:

切换到GameLayer场景

调用Window.DefaultDirector.ReplaceScene,在当前场景与其他场景之间切换。使用以下代码来执行切换到GameLayer场景:

Window.DefaultDirector.ReplaceScene (GameLayer.GameScene (Window));

执行GameLayer场景

对于GameLayer,创建一个从CCLayerColor继承名为GameLayer的类。

我们在本教程中自始至终的使用一个多种类变量,让我们继续,并创建他们:

using System;
using System.Collections.Generic;
using CocosDenshion;
using CocosSharp;
using System.Linq;

using Box2D.Common;
using Box2D.Dynamics;
using Box2D.Collision.Shapes;

namespace GoneBananas
{
public class GameLayer : CCLayerColor
{
const float MONKEY_SPEED = 350.0f;
const float GAME_DURATION = 60.0f; // game ends after 60 seconds or when the monkey hits a ball, whichever comes first

// point to meter ratio for physics
const int PTM_RATIO = 32;

float elapsedTime;
CCSprite monkey;
List visibleBananas;
List hitBananas;

// monkey walking animation
CCAnimation walkAnim;
CCRepeatForever walkRepeat;
CCCallFuncN walkAnimStop = new CCCallFuncN (node => node.StopAllActions ());

// background sprite
CCSprite grass;

// particles
CCParticleSun sun;

// circle layered behind sun
CCDrawNode circleNode;

// parallax node for clouds
CCParallaxNode parallaxClouds;

// define our banana rotation action
CCRotateBy rotateBanana = new CCRotateBy (0.8f, 360);

// define our completion action to remove the banana once it hits the bottom of the screen
CCCallFuncN moveBananaComplete = new CCCallFuncN (node => node.RemoveFromParent ());

...
}
}

添加一个背景sprite精灵

一个sprite精灵,是一个已经使用addChild添加到layer节点中的CCSprite实例。

背景sprite精灵是从content文件夹中加载的一个文件。继续在
GameLayer
中添加以下代码用来创建背景:

using System;
using System.Collections.Generic;
using CocosDenshion;
using CocosSharp;
using System.Linq;

using Box2D.Common;
using Box2D.Dynamics;
using Box2D.Collision.Shapes;

namespace GoneBananas
{
public class GameLayer : CCLayerColor
{
...
void AddGrass ()
{
grass = new CCSprite ("grass");
AddChild (grass);
}
...
}
}

创建香蕉sprites精灵

每一个从屏幕上往下掉的香蕉都是通过一个图片文件创建的sprite精灵。香蕉图片文件被包含在一个sprite sheet精灵表中。一个精灵表是包含了多个图片文件的集合,他们会被更高效的加载。一个plist文件包含了具体图片的信息,例如香蕉,就是在sprite sheet精灵表中。

使用以下代码来创建一个名为
AddBanana
的方法,用来创建香蕉sprite并显示在层中,添加位置同上,与
AddGrass
处于同级:

void AddGrass ()
{
...
}

//以上为之前添加的AddGrass
//AddBanana与AddGrass处于同级

CCSprite AddBanana ()
{
var spriteSheet = new CCSpriteSheet ("animations/monkey.plist");
var banana = new CCSprite (spriteSheet.Frames.Find ((x) => x.TextureFilename.StartsWith ("Banana")));

var p = GetRandomPosition (banana.ContentSize);
banana.Position = p;
banana.Scale = 0.5f;

AddChild (banana);

var moveBanana = new CCMoveTo (5.0f, new CCPoint (banana.Position.X, 0));
banana.RunActions (moveBanana, moveBananaComplete);
banana.RepeatForever (rotateBanana);

return banana;
}

这些代码在屏幕的一个随机x位置添加一个sprite精灵,让我们看看如何使每一个sprite精灵从屏幕上向下移动直到在屏幕底部消失。

使用Actions来控制香蕉

CocosSharp包含了各种
CCAction
类,用来在一个节点上执行不同的任务,包含了继承自
CCNode
CCSprite
实例sprite。

在这个示例中,我们将使用
CCMoveTo
动作来控制香蕉sprite,并在动画执行结束后使用
CCCallFuncN
移除精灵。

我们使用包含各种动作的
CCSequence
来完成它,他本身也是一个动作包含了不同的动作来继续执行。

将以下代码添加到
AddBanana
方法中,并放置在返回香蕉sprite之前:

CCSprite AddBanana ()
{
var spriteSheet = new CCSpriteSheet ("animations/monkey.plist");
var banana = new CCSprite (spriteSheet.Frames.Find ((x) => x.TextureFilename.StartsWith ("Banana")));

var p = GetRandomPosition (banana.ContentSize);
banana.Position = p;
banana.Scale = 0.5f;

AddChild (banana);

//这里是放置的位置
var moveBanana = new CCMoveTo (5.0f, new CCPoint (banana.Position.X, 0));
banana.RunActions (moveBanana, moveBananaComplete);
banana.RepeatForever (rotateBanana);

return banana;
}

添加猴子sprite

接下来,在
Gamelayer
中添加下面的构造代码:

public GameLayer ()
{
var touchListener = new CCEventListenerTouchAllAtOnce ();
touchListener.OnTouchesEnded = OnTouchesEnded;

Color = new CCColor3B (CCColor4B.White);
Opacity = 255;

visibleBananas = new List ();
hitBananas = new List ();

AddGrass ();
AddSun (); // we'll implement this later using a particle system
AddMonkey ();

}

我们现在要做的,就是让游戏用
visibleBananas
hitBananas
来管理得分及碰撞检测,猴子sprite同样被添加到sprite sheet精灵表中,仅仅猴子,我们就需要用一些图片来控制猴子的走动,继续在
GameLayer
中添加代码:

void AddMonkey ()
{
var spriteSheet = new CCSpriteSheet ("animations/monkey.plist");
var animationFrames = spriteSheet.Frames.FindAll ((x) => x.TextureFilename.StartsWith ("frame"));

walkAnim = new CCAnimation (animationFrames, 0.1f);
walkRepeat = new CCRepeatForever (new CCAnimate (walkAnim));
monkey = new CCSprite (animationFrames.First ()) { Name = "Monkey" };
monkey.Scale = 0.25f;

AddChild (monkey);
}

在点击后移动猴子sprite

用户点击屏幕来控制猴子sprite移动到当前点击的位置,我们重载了
TouchesEnded
并创建了另一个动作
MoveTo
GameLayer
中添加如下代码:

void OnTouchesEnded (List touches, CCEvent touchEvent)
{
monkey.StopAllActions ();

var location = touches [0].LocationOnScreen;
location = WorldToScreenspace (location);
float ds = CCPoint.Distance (monkey.Position, location);

var dt = ds / MONKEY_SPEED;

var moveMonkey = new CCMoveTo (dt, location);

monkey.RunAction (walkRepeat);
monkey.RunActions (moveMonkey, walkAnimStop);
}

碰撞检测

对于猴子和香蕉之间的碰撞检测,我们简单的为每一个sprite做一个bounding box弹力盒检测。我们同样保持跟踪香蕉被猴子碰撞的顺序,然后从layer中移除他们,并保持分数。

以下代码用来检查碰撞,添加到
GameLayer
中:

void CheckCollision ()
{
visibleBananas.ForEach (banana => {
bool hit = banana.BoundingBoxTransformedToParent.IntersectsRect (monkey.BoundingBoxTransformedToParent);
if (hit) {
hitBananas.Add (banana);
CCSimpleAudioEngine.SharedEngine.PlayEffect ("Sounds/tap");
Explode (banana.Position); // we'll implement this later using a particle systtem
banana.RemoveFromParent ();
}
});

hitBananas.ForEach (banana => visibleBananas.Remove (banana));
}

调度

在固定的重复间隔中执行代码,按顺序的添加香蕉、检查碰撞并测试游戏是否结束的逻辑,我们可以使用调度器方法,像一个timer一样的动作。

我们可以用我们之前创建的构造器来调度我们希望结束时执行的代码,仍然是在
GameLayer
中,代码放在
public GameLayer()
中:

...
namespace GoneBananas
{
public class GameLayer : CCLayerColor
{
...
public GameLayer ()
{
...
//这一段是需要添加的代码
Schedule (t => {
visibleBananas.Add (AddBanana ());
elapsedTime += t;
if (ShouldEndGame ()) {
EndGame ();
}
}, 1.0f);
Schedule (t => CheckCollision ());
...
}
...
}
}

播放一个声音特效

让我们在猴子和香蕉碰撞后播放一个声音特效。文件
tap.mp3
被放在
content/sounds
文件夹中。要播放一个声音特效,我们使用
CCSimpleAudioEngine
类,添加以下的代码到
TouchesEnded
方法的结尾:

void OnTouchesEnded (List touches, CCEvent touchEvent)
{
...
//需要添加的代码
CCSimpleAudioEngine.SharedEngine.PlayEffect ("Sounds/tap");
}

注:官方教程中,让将这段代码添加到
TouchesEnded
方法的结尾,但我再查看Demo源码时,发现其加在了碰撞检测
CheckCollision
事件中。

添加游戏结束逻辑

我们还需要一些方法来结束游戏。例子中,我们简单的在60秒后结束游戏:

bool ShouldEndGame ()
{
return elapsedTime > GAME_DURATION;
}

void EndGame ()
{
var gameOverScene = GameOverLayer.SceneWithScore (Window, hitBananas.Count);
var transitionToGameOver = new CCTransitionMoveInR (0.3f, gameOverScene);
Director.ReplaceScene (transitionToGameOver);
}

使用视差添加云朵

CocosSharp包含了一个CCParallaxNode,我们可以用来添加sprite并实现不同相对速度的视差效果。

添加以下来代码来实现猴子运动时,云朵与其产生的不同相对速度的视差效果:

void AddClouds ()
{
float h = VisibleBoundsWorldspace.Size.Height;

parallaxClouds = new CCParallaxNode {
Position = new CCPoint (0, h)
};

AddChild (parallaxClouds);

var cloud1 = new CCSprite ("cloud");
var cloud2 = new CCSprite ("cloud");
var cloud3 = new CCSprite ("cloud");

float yRatio1 = 1.0f;
float yRatio2 = 0.15f;
float yRatio3 = 0.5f;

parallaxClouds.AddChild (cloud1, 0, new CCPoint (1.0f, yRatio1), new CCPoint (100, -100 + h - (h * yRatio1)));
parallaxClouds.AddChild (cloud2, 0, new CCPoint (1.0f, yRatio2), new CCPoint (250, -200 + h - (h * yRatio2)));
parallaxClouds.AddChild (cloud3, 0, new CCPoint (1.0f, yRatio3), new CCPoint (400, -150 + h - (h * yRatio3)));
}

添加粒子系统

CocosSharp包含了一些粒子系统用来创建多样的、让人感兴趣的特效,例如fire火、smoke烟、sun太阳和explosion爆炸等等。

在GoneBananas中,让我们在猴子碰撞香蕉时添加一个爆炸效果:

void Explode (CCPoint pt)
{
var explosion = new CCParticleExplosion (pt);
explosion.TotalParticles = 10;
explosion.AutoRemoveOnFinish = true;
AddChild (explosion);
}

同样,在右上方我们添加一个太阳,并拥有一个微妙的特效。

void AddSun ()
{
circleNode = new CCDrawNode ();
circleNode.DrawSolidCircle (CCPoint.Zero, 30.0f, CCColor4B.Yellow);
AddChild (circleNode);

sun = new CCParticleSun (CCPoint.Zero);
sun.StartColor = new CCColor4F (CCColor3B.Red);
sun.EndColor = new CCColor4F (CCColor3B.Yellow);
AddChild (sun);
}

初始化场景

我们同样需要初始化一些我们已经实现的东西,例如初始化sprite的位置。我们可以用AddedToScene来实现这些,代码如下:

protected override void AddedToScene ()
{
base.AddedToScene ();

Scene.SceneResolutionPolicy = CCSceneResolutionPolicy.NoBorder;

grass.Position = VisibleBoundsWorldspace.Center;
monkey.Position = VisibleBoundsWorldspace.Center;

var b = VisibleBoundsWorldspace;
sun.Position = b.UpperRight.Offset (-100, -100);

circleNode.Position = sun.Position;

AddClouds ();
}

创建场景

我们需要提供一种途径让layer返回场景,就像我们之前做的GameStartLayer。添加下面的静态属性来完成这些工作:

public static CCScene GameScene (CCWindow mainWindow)
{
var scene = new CCScene (mainWindow);
var layer = new GameLayer ();

scene.AddChild (layer);

return scene;
}

添加物理效果

让我们来用physics来添加一个特性让游戏更真实一些。我们使用Box2D的C#接口,来添加一些乱跳的球到游戏中。它用来避免球碰撞到香蕉。

我们需要一对类变量,用来各自控制物理世界和球sprite:

...
namespace GoneBananas
{
...
public class GameLayer : CCLayerColor
{
...
// physics world
b2World world;

// balls sprite batch
CCSpriteBatchNode ballsBatch;
CCTexture2D ballTexture;
}
}

然后我们可以在构造器中添加代码为球sprite创建一些节点。代码放置在
public GameLayer ()
中:

...
namespace GoneBananas
{
public class GameLayer : CCLayerColor
{
...
public GameLayer ()
{
...
//需要添加的代码
// batch node for physics balls
ballsBatch = new CCSpriteBatchNode ("balls", 100);
ballTexture = ballsBatch.Texture;
AddChild (ballsBatch, 1, 1);
...
}
}
}

一个CCSpriteBatchNode同时渲染所有的sprites,更有效的利用GPU。接下来,添加下面的代码用来初始化物理世界以及添加球sprite:

void InitPhysics ()
{
CCSize s = Layer.VisibleBoundsWorldspace.Size;

var gravity = new b2Vec2 (0.0f, -10.0f);
world = new b2World (gravity);

world.SetAllowSleeping (true);
world.SetContinuousPhysics (true);

var def = new b2BodyDef ();
def.allowSleep = true;
def.position = b2Vec2.Zero;
def.type = b2BodyType.b2_staticBody;
b2Body groundBody = world.CreateBody (def);
groundBody.SetActive (true);

b2EdgeShape groundBox = new b2EdgeShape ();
groundBox.Set (b2Vec2.Zero, new b2Vec2 (s.Width / PTM_RATIO, 0));
b2FixtureDef fd = new b2FixtureDef ();
fd.shape = groundBox;
groundBody.CreateFixture (fd);
}

void AddBall ()
{
int idx = (CCRandom.Float_0_1 () > .5 ? 0 : 1);
int idy = (CCRandom.Float_0_1 () > .5 ? 0 : 1);
var sprite = new CCPhysicsSprite (ballTexture, new CCRect (32 * idx, 32 * idy, 32, 32), PTM_RATIO);

ballsBatch.AddChild (sprite);

CCPoint p = GetRandomPosition (sprite.ContentSize);

sprite.Position = new CCPoint (p.X, p.Y);

var def = new b2BodyDef ();
def.position = new b2Vec2 (p.X / PTM_RATIO, p.Y / PTM_RATIO);
def.type = b2BodyType.b2_dynamicBody;
b2Body body = world.CreateBody (def);

var circle = new b2CircleShape ();
circle.Radius = 0.5f;

var fd = new b2FixtureDef ();
fd.shape = circle;
fd.density = 1f;
fd.restitution = 0.85f;
fd.friction = 0.3f;
body.CreateFixture (fd);

sprite.PhysicsBody = body;

Console.WriteLine ("sprite batch node count = {0}", ballsBatch.ChildrenCount);
}

public override void OnEnter ()
{
base.OnEnter ();

InitPhysics ();
}

还需要添加一个碰撞检测在
CheckCollision
方法中,用来决定当猴子碰撞到球后的事件:

void CheckCollision ()
{
...
hitBananas.ForEach (banana => visibleBananas.Remove (banana));

int ballHitCount = ballsBatch.Children.Count (ball => ball.BoundingBoxTransformedToParent.IntersectsRect (monkey.BoundingBoxTransformedToParent));

if (ballHitCount > 0) {
EndGame ();
}
}

最终,我们需要在之前为了添加香蕉时创建的调度器中调用AddBall事件,也需要添加一个额外的调度器用来控制物理世界的步长,将下面的代码添加至
public GameLayer()
的末尾:

...
namespace GoneBananas
{
public class GameLayer : CCLayerColor
{
...
public GameLayer ()
{
...
//需要添加的代码
Schedule (t => {
visibleBananas.Add (AddBanana ());
elapsedTime += t;
if (ShouldEndGame ()) {
EndGame ();
}
AddBall ();
}, 1.0f);

Schedule (t => {
world.Step (t, 8, 1);

foreach (CCPhysicsSprite sprite in ballsBatch.Children) {
if (sprite.Visible && sprite.PhysicsBody.Position.x < 0f || sprite.PhysicsBody.Position.x * PTM_RATIO > ContentSize.Width) {
world.DestroyBody (sprite.PhysicsBody);
sprite.Visible = false;
sprite.RemoveFromParent ();
} else {
sprite.UpdateTransformedSpriteTextureQuads ();
}
}
});
}
}
}

现在我们就可以运行游戏了。

实现GameOverLayer层

在GameLayer中我们添加了一些代码用来在游戏结束时切换到
GameOverLayer
场景。
GameOverLayer
GameStartLayer
非常的相似,主要的区别是已经获取到了用来显示的游戏分数的文本框的值。同样,当用户点击
GameOverLayer
后,将会开始一个新游戏。使用如下的代码添加一个名为
GameOverLayer
的类:

using System;
using System.Collections.Generic;
using CocosSharp;

namespace GoneBananas
{
public class GameOverLayer : CCLayerColor
{

string scoreMessage = string.Empty;

public GameOverLayer (int score) //: base(new CCSize (640, 1136))
{

var touchListener = new CCEventListenerTouchAllAtOnce ();
touchListener.OnTouchesEnded = (touches, ccevent) => Window.DefaultDirector.ReplaceScene (GameLayer.GameScene (Window));

AddEventListener (touchListener, this);

scoreMessage = String.Format ("Game Over. You collected {0} bananas!", score);

Color = new CCColor3B (CCColor4B.Black);

Opacity = 255;
}

public void AddMonkey ()
{
var spriteSheet = new CCSpriteSheet ("animations/monkey.plist");
var frame = spriteSheet.Frames.Find ((x) => x.TextureFilename.StartsWith ("frame"));

var monkey = new CCSprite (frame) {
Position = new CCPoint (VisibleBoundsWorldspace.Size.Center.X + 20, VisibleBoundsWorldspace.Size.Center.Y + 300),
Scale = 0.5f
};

AddChild (monkey);
}

protected override void AddedToScene ()
{
base.AddedToScene ();

Scene.SceneResolutionPolicy = CCSceneResolutionPolicy.ShowAll;

var scoreLabel = new CCLabelTtf (scoreMessage, "arial", 22) {
Position = new CCPoint (VisibleBoundsWorldspace.Size.Center.X, VisibleBoundsWorldspace.Size.Center.Y + 50),
Color = new CCColor3B (CCColor4B.Yellow),
HorizontalAlignment = CCTextAlignment.Center,
VerticalAlignment = CCVerticalTextAlignment.Center,
AnchorPoint = CCPoint.AnchorMiddle,
Dimensions = ContentSize
};

AddChild (scoreLabel);

var playAgainLabel = new CCLabelTtf ("Tap to Play Again", "arial", 22) {
Position = VisibleBoundsWorldspace.Size.Center,
Color = new CCColor3B (CCColor4B.Green),
HorizontalAlignment = CCTextAlignment.Center,
VerticalAlignment = CCVerticalTextAlignment.Center,
AnchorPoint = CCPoint.AnchorMiddle,
Dimensions = ContentSize
};

AddChild (playAgainLabel);

AddMonkey ();
}

public static CCScene SceneWithScore (CCWindow mainWindow, int score)
{
var scene = new CCScene (mainWindow);
var layer = new GameOverLayer (score);

scene.AddChild (layer);

return scene;
}
}
}

最终的GameOverLayer场景如下图所示:

你可以在模拟器或设备中运行游戏并玩一下。

现在恭喜你,你成功的使用CocosSharp创建了你的第一个游戏。

总结

在本篇教程中,我们使用CocosSharp创建了一个游戏。我们创建了一些场景用来显示游戏及控制游戏逻辑。在这个过程中,我们覆盖到了如何添加sprite并用actions控制他们、手势点击、添加声音特效和在场景中进行切换。我们还添加了粒子系统和视差效果用来包含一些令人感兴趣的可是特效,并将物理模拟让游戏变得更加接近真实。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: