简体   繁体   English

如何在 Unity C# 中更改我的 TicTacToe 游戏中的移动?

[英]How to change move in my TicTacToe game in Unity C#?

I'm developing a TicTacToe game and faced with a some problem.我正在开发一款 TicTacToe 游戏,但遇到了一些问题。
After put the "X" I need to change changeMove to true in ChangeMove() function to put next move as "O" in another place.放置“X”后,我需要在ChangeMove() function 中将changeMove更改为true ,以便将下一步作为“O”放在另一个地方。 I need to check place.sprite == null too, because I don't want to change put move again.我也需要检查place.sprite == null ,因为我不想再次更改 put move。
In the If block I'm changing changeMove to apposite value but it isn't changing(I mean when I'm calling OnMouseDown method second time, I can't get into the else if block).If块中,我将 changeMove 更改为适当的值,但它没有改变(我的意思是当我第二次调用 OnMouseDown 方法时,我无法进入else if块)。 Here is my inspector of MainCamera :这是我的MainCamera检查员:

在此处输入图像描述

I created a script(PutMove.cs) that inherits all the methods and fields from LogicOfGame.cs besides private bool changeMove only for clickable squares to put move there:我创建了一个脚本(PutMove.cs),它继承了 LogicOfGame.cs 的所有方法和字段,除了private bool changeMove用于可点击的方块以将移动放在那里:
Here is my LogicOfGame.cs, PutMove.cs scripts and inspector of first square:这是我的 LogicOfGame.cs、PutMove.cs 脚本和第一个方块的检查器:
The square's inspector:广场巡视员:

在此处输入图像描述

LogicOfGame.cs: LogicOfGame.cs:

public class LogicOfGame : MonoBehaviour
{
    public SpriteRenderer place;
    private bool changeMove;
    public Image X;
    public Image O;

    public void ChangeMove() 
    {
        if (!changeMove && place.sprite == null)
        {
            place.sprite = X.sprite;
            changeMove = !changeMove;
        }

        else if (changeMove && place.sprite == null)
        {
            place.sprite = O.sprite;
            changeMove = !changeMove;
        }
    }
{

PutMove.cs: PutMove.cs:

public class PutMove : LogicOfGame
{
    private void OnMouseDown()
    {
        ChangeMove();
    }
}   

I also tried to change changeMove value in another method and call it in OnMouseDown() method, but isn't helped我还尝试在另一种方法中更改changeMove值并在OnMouseDown()方法中调用它,但没有帮助

public bool ChangeMove(bool changeMove) 
{
    return !changeMove;
}

public class PutMove : LogicOfGame
{
    private void OnMouseDown()
    {
        //renamed the ChangeMove()
        //And left only place.sprite = X.sprite;
        PutMove();     
        changeMove = ChangeMove(changeMove);
    }
} 

How can I change value of changeMove at last?最后如何更改changeMove的值?

Set a breakpoint and step through the code.设置断点并逐步执行代码。 When you get to the if/else conditions, is place.sprite == null actually true ?当你遇到if/else条件时, place.sprite == null真的是true吗? I don't know Unity, but it seems to me this would only be true the first time the method is called.我不了解 Unity,但在我看来,这只有在第一次调用该方法时才是true的。 After that, it's set to X.sprite or O.sprite , so subsequent calls would have no affect since the conditions would evaluate to false .之后,它被设置为X.spriteO.sprite ,因此后续调用不会有任何影响,因为条件将评估为false

If this is the case, then removing the null check should resolve the issue:如果是这种情况,则删除null检查应该可以解决问题:

public void ChangeMove() 
{
    if (changeMove) place.sprite = O.sprite;
    else place.sprite = X.sprite;
    changeMove = !changeMove;
}

You may even be able to get rid of the changeMove variable and just examine the value of place.sprite :您甚至可以去掉changeMove变量,只检查place.sprite的值:

public void ChangeMove() 
{
    if (place.sprite == O.sprite) place.sprite = X.sprite;
    else place.sprite = O.sprite;
}

ChangeMove() is only true once, as you set the value of place.sprite as its called the first time, and then it can never be changed again. ChangeMove()只有一次为真,因为您将place.sprite的值设置为第一次调用,然后它就再也不能改变了。

As Rufus L says, remove the place.sprite == null condition and there shouldn't be an issue, as it looks like the function (as is) only handles the very first move only.正如 Rufus L 所说,删除place.sprite == null条件应该没有问题,因为看起来 function(按原样)只处理第一步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM