我正在Unity中制作一个简单的俄罗斯方块游戏。一切正常,但我无法清除线条,也不知道为什么。
在我的代码中,我检查了完整的行。如果是完整的行,则应清除该行,并且所有块都应向下移动一行。不幸的是,我的代码无法正常工作。
public Vector3 rotationPoint;
private float previousTime;
public float fallTime = 0.8f;
public static int height = 20;
public static int width = 10;
private static Transform[,] grid = new Transform[width, height];
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.LeftArrow)) {
transform.position += new Vector3 (-1, 0, 0);
if (!ValidMove ())
transform.position -= new Vector3 (-1, 0, 0);
} else if (Input.GetKeyDown (KeyCode.RightArrow)) {
transform.position += new Vector3 (1, 0, 0);
if (!ValidMove ())
transform.position -= new Vector3 (1, 0, 0);
} else if (Input.GetKeyDown (KeyCode.UpArrow)) {
transform.RotateAround (transform.TransformPoint(rotationPoint), new Vector3 (0, 0, 1), 90);
if (!ValidMove ())
transform.RotateAround (transform.TransformPoint(rotationPoint), new Vector3 (0, 0, 1), -90);
}
if (Time.time - previousTime > (Input.GetKey (KeyCode.DownArrow) ? fallTime / 10 : fallTime)) {
transform.position += new Vector3 (0, -1, 0);
if (!ValidMove ()){
transform.position -= new Vector3 (0, -1, 0);
AddToGrid ();
this.enabled = false;
FindObjectOfType<SpawnTetromino> ().NewTetromino ();
}
previousTime = Time.time;
}
}
void CheckForLines(){
for (int i = height-1; i >= 0; i--){
if(HasLine(i)){
DeleteLine(i);
RowDown(i);
}
}
}
bool HasLine(int i){
for(int j = 0; j< width; j++){
if(grid[j, i] == null)
return false;
}
return true;
}
void DeleteLine(int i){
for (int j = 0; j < width; j++){
Destroy(grid[j, i].gameObject);
grid[j, i] = null;
}
}
void RowDown(int i){
for (int y = i; y < height; y++){
for (int j = 0; j < width; j++){
if(grid[j,y] != null){
grid[j, y - 1] = grid[j,y];
grid[j, y] = null;
grid[j, y - 1].transform.position -= new Vector3(0, 1, 0);
}
}
}
}
void AddToGrid(){
foreach (Transform children in transform) {
int roundedX = Mathf.RoundToInt (children.transform.position.x);
int roundedY = Mathf.RoundToInt (children.transform.position.y);
grid [roundedX, roundedY] = children;
}
}
bool ValidMove(){
foreach (Transform children in transform) {
int roundedX = Mathf.RoundToInt (children.transform.position.x);
int roundedY = Mathf.RoundToInt (children.transform.position.y);
if (roundedX < 0 || roundedX >= width || roundedY < 0 || roundedY >= height) {
return false;
}
if (grid [roundedX, roundedY] != null)
return false;
}
return true;
}
提前谢谢。
答案 0 :(得分:-1)
将CheckForLines()添加到Update方法的末尾。当前您没有使用它