为什么我得到2个输出?

时间:2016-07-24 15:19:41

标签: c# unity3d

这是整个脚本先生。 问题在于,当我释放时,会产生两个硬币而不是一个硬币。 谢谢你的帮助

using UnityEngine;
using System.Collections;

public class coinControler : MonoBehaviour {
int flag;
int x = 0;
float xScale = 0.3f;
float zScale = 0.3f;
float xInterval = 0.01f;
float zInterval = 0.01f;
public GameObject coinobj;
public GameObject coin0;

Vector3 scale;

// Use this for initialization
void Start (){
    //for 1st coin
    scale = coinobj.transform.localScale;
    scale.z = zScale;
    scale.x = xScale;
    coinobj.transform.localScale = scale;
    //for 2nd coin
    scale = coin1.transform.localScale;
    scale.z = zScale;
    scale.x = xScale;
}

// Update is called once per frame
void Update () {
    touchInput();
}


void touchInput() {
//interval of the coin growth
    if(x%20 == 0 && x >= 20)
    {
        xInterval += 0.01f;
        zInterval += 0.01f;
    }

    if (Input.touchCount > 0) 
    {
        Touch touch = Input.GetTouch(0);
        if (touch.phase == TouchPhase.Stationary || touch.phase == TouchPhase.Began)
        {
            //Change Scale  
            if (flag == 0)
            {
                scale.x += xInterval;
                scale.z += zInterval;
                coinobj.transform.localScale = scale;
                Debug.Log("......" + x + "........");
                x++;
            }
        }
    //the coin will drop after the user releases touch
        else 
    {

            //drop the coin
            coinobj.AddComponent<Rigidbody>();
            if (flag == 0)
            {
                coinSpawn0();
                Debug.Log("Drop Coin");
            } 
        }
    }
}
//Spawn function of the second coin
public void coinSpawn0()
{
    Vector3 coinPos = new Vector3(transform.position.x, transform.position.y, transform.position.z);
    Instantiate(coin0, coinPos, Quaternion.identity);
    scale = coin0.transform.localScale;
    scale.z = zScale;
    scale.x = xScale;
    coin0.transform.localScale = scale;
    xInterval = 0.01f;
    zInterval = 0.01f;
    flag = 1;
}
}

输出:

...... 42 ........ UnityEngine.Debug:日志(对象)

...... 42 ........ UnityEngine.Debug:日志(对象)

...... 43 ........ UnityEngine.Debug:日志(对象)

...... 43 ........ UnityEngine.Debug:日志(对象)

显示标志:1 UnityEngine.Debug:日志(对象)

显示标志:1 UnityEngine.Debug:日志(对象)

enter image description here

1 个答案:

答案 0 :(得分:0)

如果你写出TouchPhase,你应该知道更多。

我想,在触摸开始时,触摸将在同一位置经历两个阶段:

  1. 开始(在x = 42)
  2. 固定,因为你不动(仍然是x = 42)
  3. 但是,我并不清楚你想要达到的目标。