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

FlappyBird开发总结(七)—— GameOver计分板

2015-03-06 16:02 190 查看


这是一个新的Scene,就是小鸟死亡后跳转的界面,小子不才,只会用NGUI做出这样子,对应关系我画出来了,这是三个拖黑的物体我是放在Again按钮的下面,也就是作为它的子物体,这个只是一个布局,你放哪都一样。

好了,在这之前我们好歹得让小鸟死亡吧,怎么死亡呢?

当小鸟在发生以下事件就触发死亡:

1、碰撞地面

2、碰撞水管

3、貌似没了。。。。。

好了,怎么做呢?

首先,请创建一个脚本 ColiderFloor.cs,也就是当掉到地面上时,我们让小鸟死亡,当然,你得把脚本放到每个背景的地面上啊,地面也得有碰撞器哦,代码如下:

using UnityEngine;
using System.Collections;

public class ColliderFloor : MonoBehaviour {

void OnCollisionEnter(Collision gameObject)
{

if (gameObject.gameObject.tag == "Player")
{
audio.Play();//播放碰撞的声源
GameManager.gameInstance.currentGameState = (int)GameManager.GameState.GAMEOVER;//将游戏状态设为GAMEOVER
}
}

}


好了,地面的设置完成,接下来就是每个背景的上下两个水管了,新建脚本ColiderPipe.cs,然后放到各个上下两个水管物体下(确定你的水管有碰撞器哦),

脚本代码:

using UnityEngine;
using System.Collections;

public class ColliderPipe : MonoBehaviour {
public AudioSource hit;//撞击时播放的声源
public AudioSource die;//小鸟哦死亡的声源
private float  delayTime = 0.5f;
void OnCollisionEnter(Collision gameObject)
{

if(gameObject.gameObject.tag =="Player")
{

StartCoroutine(RetuenTheState());//喜闻乐见的协程函数

}
}

IEnumerator RetuenTheState()
{
hit.Play();//先播放撞击的声源
GameManager.gameInstance.currentGameState = (int)GameManager.GameState.GAMEOVER;//设置当前游戏状态为GAMEOVER状态
yield return new WaitForSeconds(delayTime);//等待0.5秒
die.Play();//等待0.5秒后播放小鸟死亡声源
}
}


喔了,各种死亡情况的代码都写好了,接下来回到GameManager.cs脚本,还记得我们之前几行在Update()中没解释的代码么?就是它了:

void Update
{
if(currentGameState==(int)GameState.GAMEOVER)//当小鸟死亡,也就是当前状态是GAMEOVER时执行以下代码
{

RemenberScores.currentScore = currentScores;
StartCoroutine("ChangeScene");

}
}
IEnumerator ChangeScene()
{
yield return new WaitForSeconds(delayTime);//延迟delayTime秒
Application.LoadLevel("MenuScene");//延迟后跳转到计分面板的场景

}


好吧,问题来了

RemenberScores.currentScore = currentScores;


这是干嘛的?

试想一下,我们当前计分的是GameManager.cs中的

public int currentScores=0;


然而我们怎样把这个值传到下个场景中的Labl中去?这就是我们需要上面代码的意思了,当然,不仅仅是它,嘿嘿,只有它当然会报错。

这里就是需要一个单例类RemenberScores.cs了,下面贴代码

using UnityEngine;
using System.Collections;

public class RemenberScores {

public static RemenberScores scoresInstance;

public static float currentScore;

private GameObject scoreTitle;
void Awake()
{
scoresInstance = this;
}
void Update()
{
//当按返回时结束游戏
if (Input.GetKey(KeyCode.Escape))
{
Application.Quit();
}
}
}


这个脚本不要放到任何物体下哦,这样我们就能在静态存储池用currentScore这个变量存储当前的分数咯。so,现在我们就来到计分板的场景吧,我的场景名是MenuScene。

这里我创建了一个脚本MenuManager.cs,直接拖到了这个场景的根物体上,代码如下:

using UnityEngine;
using System.Collections;

public class MenuManager : MonoBehaviour {
public UILabel currentScores;
public UILabel bestScores;
public static MenuManager menuInstance;
private float currentScore;
void Awake(){
menuInstance = this;
currentScore =RemenberScores.currentScore;
UpdateTheScores(currentScore);
}
public void UpdateTheScores(float currentScore)
{

float bestScore = PlayerPrefs.GetFloat("score", 0);
if (currentScore > bestScore)
{
bestScore = currentScore;
GameObject.Find("Title").GetComponent<UILabel>().text = "Good Score!";
}
else
{
GameObject.Find("Title").GetComponent<UILabel>().text = "No Good!";
}
PlayerPrefs.SetFloat("score",bestScore);

currentScores.text = currentScore + "";
bestScores.text = bestScore + "";

}
}


左看右看没什么难点,关于PlyerPrefs.SetFloat(),以及 PlayerPrefs.SetFloat(),给大家一个大神的链接吧 /article/1479879.html

好吧,现在分数也记录了,场景也能跳转了。。。

你们会说我瞎了吧,那么大三个按钮还没写。。。好吧。。。我贴上来。。这里提一下,MainMenu的场景在下一篇来写,主要是用来控制游戏难度的,说白了就是控制小鸟的速度。。。

按钮MANINMENU的脚本:

using UnityEngine;
using System.Collections;

public class LoadMainMenuScene : MonoBehaviour {

public void LoadMainMenu()
{
Application.LoadLevel("MainScene");
}

}


按钮AGAIN的脚本

using UnityEngine;
using System.Collections;

public class ChangeToStartGameScene : MonoBehaviour {

public void GoGame()
{
Application.LoadLevel("StartGame");
}

}


按钮EXIT的脚本

using UnityEngine;
using System.Collections;

public class EXIT : MonoBehaviour {

public void GoOut()
{
Application.Quit();
}
}


好吧,别打我。。。我是用NGUI写的,菜鸟表示只会这样。。。好了。。。这章OVER了,计算板实现完毕。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: