首先,这是我的剧本:
#pragma strict
var incorrect: AudioClip;
var correct: AudioClip;
var anything: GameObject;
var diamond: Sprite;
var circle: Sprite;
var triangle: Sprite;
var square: Sprite;
var number: int;
var AcceptInput: boolean = true;
static
var score: int = 1;
var guiScore: GUIText;
function Start() {
number = Random.Range(1, 4);
if (number == 1) {
anything.GetComponent(SpriteRenderer).sprite = diamond;
} else if (number == 2) {
anything.GetComponent(SpriteRenderer).sprite = circle;
} else if (number == 3) {
anything.GetComponent(SpriteRenderer).sprite = triangle;
} else {
anything.GetComponent(SpriteRenderer).sprite = square;
}
}
function Update() {
if (Input.GetMouseButtonDown(0)) {
if (AcceptInput) {
AcceptInput = false;
Debug.Log("Clicked");
if (anything.GetComponent(SpriteRenderer).sprite == diamond) {
audio.PlayOneShot(correct);
guiScore.text = "Score: " + score;
StartCoroutine("YieldTestEnumerator");
AcceptInput = true;
number = Random.Range(1, 4);
if (number == 1) {
anything.GetComponent(SpriteRenderer).sprite = diamond;
} else if (number == 2) {
anything.GetComponent(SpriteRenderer).sprite = circle;
} else if (number == 3) {
anything.GetComponent(SpriteRenderer).sprite = triangle;
} else {
anything.GetComponent(SpriteRenderer).sprite = square;
}
} else if (anything.GetComponent(SpriteRenderer).sprite == circle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
} else if (anything.GetComponent(SpriteRenderer).sprite == triangle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
} else if (anything.GetComponent(SpriteRenderer).sprite == square) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
}
}
}
}
function YieldTestEnumerator() {
yield WaitForSeconds(0.5);
}
你可以看到它是一款多选游戏。出于某些愚蠢和奇怪的原因,如果我在屏幕上按任意位置,它只会加载" GameOver"场景,即使我得到了正确的答案。为什么它会在屏幕上的任何地方检测到触摸?多数民众赞成这么奇怪。你们有没有想法如何解决这个问题?我不明白这一点。任何帮助将不胜感激。
答案 0 :(得分:0)
将对撞机2D附加到任何并使用
OnMouseDown()
{
var lSprite : Sprite ;
lSprite = GetComponent(SpriteRenderer).sprite ;
if (lSprite == diamond) {
audio.PlayOneShot(correct);
guiScore.text = "Score: " + score;
StartCoroutine("YieldTestEnumerator");
AcceptInput = true;
number = Random.Range(1, 4);
if (number == 1) {
lSprite = diamond;
} else if (number == 2) {
lSprite = circle;
} else if (number == 3) {
lSprite = triangle;
} else {
lSprite = square;
}
} else
if (lSprite == circle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
} else
if (lSprite == triangle) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
} else
if (lSprite == square) {
Debug.Log("Wrong Answer!");
Application.LoadLevel("GameOver");
audio.PlayOneShot(incorrect);
}
}