【问题标题】:how do i check if my value match with other like a pair matching game我如何检查我的价值是否与其他配对游戏匹配
【发布时间】:2021-01-11 08:20:26
【问题描述】:

所以我有一个有 9 个盒子的游戏,有 3 个不同的数字,每个数字在随机位置分布 3 个不同的盒子,每次滚动轮子,都会生成 1 个随机数,打开对应的。例如,如果我滚动 3 号,第 3 号盒子将打开......到目前为止我已经做到了,但现在我被困在如何检测它们是否与我之前打开的盒子匹配(意味着如果我打开 2 个相同的盒子)乘数)

//here is my box class

public class Safe 
{
    public int multiplier;
    public GameObject reward;
    
}

 //and here is my gamemanager class
public class GameManager : MonoBehaviour
{
    public Text[] mText;
    public GameObject[] openBox;
    private int a, b, c;
    public Safe[] safe;
    // Start is called before the first frame update
    void Start()
    {
        GenerateRandom()
    }

    // Update is called once per frame
    void Update()
    {
        // this is where i check, e.g if i roll number 5, box 5 will open..
        switch (WheelController.finalAngle)
        {
            case 0:
                openBox[0].SetActive(true);
                mText[1].gameObject.SetActive(true);
                safe[1].reward.gameObject.SetActive(true);
                break;
            case 40:
                openBox[1].SetActive(true);
                mText[2].gameObject.SetActive(true);
                safe[2].reward.gameObject.SetActive(true);
                break;
            case 80:
                openBox[2].SetActive(true);
                mText[3].gameObject.SetActive(true);
                safe[3].reward.gameObject.SetActive(true);
                break;
            case 120:
                openBox[3].SetActive(true);
                mText[4].gameObject.SetActive(true);
                safe[4].reward.gameObject.SetActive(true);
                break;
            case 160:
                openBox[4].SetActive(true);
                mText[5].gameObject.SetActive(true);
                safe[5].reward.gameObject.SetActive(true);
                break;
            case 200:
                openBox[5].SetActive(true);
                mText[6].gameObject.SetActive(true);
                safe[6].reward.gameObject.SetActive(true);
                break;
            case 240:
                openBox[6].SetActive(true);
                mText[7].gameObject.SetActive(true);
                safe[7].reward.gameObject.SetActive(true);
                break;
            case 280:
                openBox[7].SetActive(true);
                mText[8].gameObject.SetActive(true);
                safe[8].reward.gameObject.SetActive(true);
                break;
            case 320:
                openBox[8].SetActive(true);
                mText[0].gameObject.SetActive(true);
                safe[0].reward.gameObject.SetActive(true);
                break;

        }
        Matching();

    }
    void GenerateRandom() //this is where i generate random Array 
    {
       a = Random.Range(15, 21);
        b = Random.Range(15, 21);
        c = Random.Range(15, 21);
        while ((a == b || b == c || a == c))
        {
            a = Random.Range(15, 21);
            b = Random.Range(15, 21);
            c = Random.Range(15, 21);
        } 
        for (int i = 0; i < mText.Length; i++)
        {
            if (i % 3==0)
            {
                mText[i].text = "x"+ a.ToString();
                safe[i].multiplier = a;
            }
            if (i % 3 == 1)
            {
                mText[i].text = "x" + b.ToString();
                safe[i].multiplier = b;
            }
            if (i % 3 == 2)
            {
                mText[i].text = "x" + c.ToString();
                safe[i].multiplier = c;
            }
        }
    }
    void Matching()
    {
        // I WANT TO CHECK IF THEY MATCHING AND WIN THE GAME. 
    }
}

【问题讨论】:

    标签: c# unity3d 2d-games


    【解决方案1】:

    您将需要以某种方式缓存您打开的盒子,或者在它们自己的字段中进行第一次/第二次/第三次打开等等,或者在一个数组中。

    然后在您的匹配方法中,您可以根据您已经缓存的值检查刚刚打开的框。如果他们匹配,您就知道该玩家连续匹配了两个(或三个或任何您的游戏需要),此时您可以在玩家获胜时触发您想要执行的任何操作。

    如果您检查之前的号码并且没有匹配,您可以将最近的号码缓存为最后一个选择的号码,让玩家重新开始选择号码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      • 1970-01-01
      • 2017-01-18
      • 1970-01-01
      • 1970-01-01
      • 2010-12-08
      相关资源
      最近更新 更多