[英]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.sprite
或O.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.