public class Model : MonoBehaviour {
public const int MAX_ROWS = 23;
public const int MAX_COLUMS = 10;
private Transform[,] map = new Transform[MAX_COLUMS,MAX_ROWS];
public bool IsValidMapPosition(Transform t) {
foreach (Transform child in t)
{
if (child.tag == "Block") continue;// 应该是if(child.tag !="Block") continue;
Vector2 pos = child.position.Round();
if (isInsideMap(pos) == false) return false;
if (map[(int)pos.x, (int)pos.y] != null) return false;
}
return true;
}
private bool isInsideMap(Vector2 pos)
{
return pos.x >= 0 && pos.x <= MAX_COLUMS && pos.y >= 0;
}
public void PlaceShape(Transform t)
{
foreach(Transform child in t){
if (child.tag != "Block") return;// 应该是 if(child.tag!="Block") continue;
Vector2 pos = child.position.Round();
map[(int)pos.x, (int)pos.y] = child;
}
}
}
---------------------------------------------------------------------------------------------------
public class Shape : MonoBehaviour {
private Ctrl ctrl;
private GameManager gameManager;
private bool isPause = false;
private float timer = 0;
private float stepTime = 0.2f;
void Update()
{
if (isPause) return;
timer += Time.deltaTime;
if (timer > stepTime) {
Fall();
}
}
public void Init(Color color,Ctrl ctrl,GameManager gameManager)
{
foreach (Transform t in transform)
{
if (t.tag == "Block")
{
t.GetComponent<SpriteRenderer>().color = color;
}
}
this.ctrl = ctrl;
this.gameManager = gameManager;
}
public void Fall() {
timer = 0;
Vector3 pos = transform.position;
pos.y -= 1;
transform.position = pos;
if (ctrl.model.IsValidMapPosition(this.transform) == false)
{
pos.y += 1;
transform.position = pos;
isPause = true;
//Debug.Log(isPause);
ctrl.model.PlaceShape(this.transform);
gameManager.FallDown();
}
}
}
---------------------------------------------------------------------------------------------------
我觉得应该就是这两个脚本哪里写的有问题,又不太能找的出来
如图,老师,我的俄罗斯方块在某些方块的地方会叠加起来,我也不知道问题在哪,老师能帮忙看看么。
对比了原项目的代码已经找到问题了,标注在上面了,如果有碰到类似的问题的同学可以借鉴下