質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Q&A

解決済

1回答

1678閲覧

Unity2Dで消えたパズルの列をカウントしたい

sumikko6210

総合スコア138

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

0グッド

0クリップ

投稿2017/09/09 08:37

編集2017/09/12 05:41

Unity2Dのパズルを作成しており、横一列に揃って消えた回数をカウントしたいのですが
うまくカウントできず困っています。

Count.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4using UnityEngine.UI; 5 6public class Count : MonoBehaviour { 7 8 public Text RowCount; 9 public int deleteRowsCount = 0; 10 11 bool deleteFullRows() 12 { 13 for (int y = 0; y < Grid.h; ++y) 14 { 15 if (!Grid.isRowFull(y)) 16 { 17 return false; 18 } 19 } 20 return true; 21 } 22 // Use this for initialization 23 void Start () { 24 RowCount.text = "消えた列:" + deleteRowsCount; 25 } 26 public void countUp() { 27 if (deleteFullRows()) 28 { 29 ++deleteRowsCount; 30 RowCount.text = "消えた列:" + deleteRowsCount; 31 } 32 } 33} 34

Group.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Group : MonoBehaviour 6{ 7 private Vector3 screenPoint; 8 private Vector3 offset; 9 private Count count; 10 11 //座標が範囲内であるか,Gridに値があるかどうかを判定 12 bool isValidGridPos() 13 { 14 foreach (Transform child in transform) 15 { 16 Vector2 v = Grid.roundVec2(child.position); 17 18 if (!Grid.insideBorder(v)) 19 return false; 20 21 if (Grid.grid[(int)v.x, (int)v.y] != null && 22 Grid.grid[(int)v.x, (int)v.y].parent != transform) 23 return false; 24 } 25 return true; 26 } 27 28 void updateGrid() 29 { 30 for (int y = 0; y < Grid.h; ++y) 31 for (int x = 0; x < Grid.w; ++x) 32 if (Grid.grid[x, y] != null) 33 if (Grid.grid[x, y].parent == transform) 34 Grid.grid[x, y] = null; 35 36 foreach (Transform child in transform) 37 { 38 Vector2 v = Grid.roundVec2(child.position); 39 Grid.grid[(int)v.x, (int)v.y] = child; 40 } 41 } 42 43 //オブジェクトをクリックする 44 void OnMouseDown() 45 { 46 // マウスカーソルは、スクリーン座標なので、 47 // 対象のオブジェクトもスクリーン座標に変換してから計算する。 48 49 // このオブジェクトの位置(transform.position)をスクリーン座標に変換。 50 screenPoint = Camera.main.WorldToScreenPoint(transform.position); 51 // ワールド座標上の、マウスカーソルと、対象の位置の差分。 52 offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 53 54 55 } 56 57 //オブジェクトをドラッグする 58 void OnMouseDrag() 59 { 60 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 61 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 62 transform.position = currentPosition; 63 } 64 65 //オブジェクトをドロップする 66 void OnMouseUp() 67 { 68 Vector3 currentScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z); 69 Vector3 currentPosition = Camera.main.ScreenToWorldPoint(currentScreenPoint) + this.offset; 70 71 //currentPositionが範囲内であるか,Gridに値があるかどうかを判定 72 if (isValidGridPos()) 73 { 74 transform.position = Grid.roundVec2(currentPosition); 75 //修正 76 count.countUp(); 77 //横列と縦列を消去 78 Grid.deleteFullRows(); 79 Grid.deleteFullLines(); 80 81 //指定範囲内ではブロックの当たり判定を無くす 82 BoxCollider2D collider = GetComponent<BoxCollider2D>(); 83 collider.enabled = false; 84 85 //次のブロックを呼び出す 86 FindObjectOfType<Spawner>().spawnNext(); 87 88 89 } 90 else 91 { 92 //Spawnerオブジェクトの座標を取る 93 transform.position = GameObject.Find("Spawner").transform.position; 94 } 95 96 } 97 98 99 // Use this for initialization 100 void Start() 101 { 102 count = FindObjectOfType<Count>(); 103 } 104 105 // Update is called once per frame 106 void Update() 107 { 108 if (isValidGridPos()) 109 { 110 // It's valid. Update grid. 111 updateGrid(); 112 } 113 114 115 116 117 } 118} 119

Grid.cs

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class Grid: MonoBehaviour { 6 public static int w = 8; 7 public static int h = 8; 8 public static Transform[,] grid = new Transform[w, h]; 9 10 //座標を整数にする 11 public static Vector2 roundVec2(Vector2 v) 12 { 13 return new Vector2(Mathf.Round(v.x), 14 Mathf.Round(v.y)); 15 } 16 17 //指定範囲内かどうかを判定 18 public static bool insideBorder(Vector2 pos) 19 { 20 return ((int)pos.x >= 0 && 21 (int)pos.x < w && 22 (int)pos.y >= 0 && 23 (int)pos.y < h); 24 } 25 26 //横列を消去 27 public static void deleteRow(int y) 28 { 29 for(int x = 0; x < w; ++x) 30 { 31 Destroy(grid[x, y].gameObject); 32 grid[x, y] = null; 33 } 34 } 35 36 //縦列を消去 37 public static void deleteLine(int x) 38 { 39 for (int y = 0; y < h; ++y) 40 { 41 Destroy(grid[x, y].gameObject); 42 grid[x, y] = null; 43 } 44 } 45 46 //横列が埋まっているかどうかを判定 47 public static bool isRowFull(int y) 48 { 49 for (int x = 0; x < w; ++x) 50 if (grid[x, y] == null) 51 return false; 52 return true; 53 } 54 55 //縦列が埋まっているかどうかを判定 56 public static bool isLineFull(int x) 57 { 58 for (int y = 0; y < h; ++y) 59 if (grid[x, y] == null) 60 return false; 61 return true; 62 } 63 64 65 public static void deleteFullRows() 66 { 67 for(int y = 0; y < h; ++y){ 68 if (isRowFull(y)) 69 { 70 deleteRow(y); 71 } 72 } 73 } 74 75 public static void deleteFullLines() 76 { 77 for (int x = 0; x < w; ++x){ 78 if (isLineFull(x)) 79 { 80 deleteLine(x); 81 } 82 } 83 } 84} 85

よろしくお願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

C#

1 // Use this for initialization 2 void Start () { 3 RowCount.text = "消えた列:" + deleteRowsCount; 4 }

以上の処理ですが、Start()だけだと、オブジェクト生成時(ゲーム開始時?)にしか実行されないため、「消えた列:0」としか表示されないと思います。
以下のように、deleteRowsCountが変更されるたびに再度実行すると直ると思います。

C#

1 // Update is called once per frame 2 void Update () { 3 if (deleteFullRows()) 4 { 5 ++deleteRowsCount; 6 RowCount.text = "消えた列:" + deleteRowsCount; //追加 7 } 8 }

追記:
修正案です。Countは1つしか存在しない前提で書きます。

Count.cs

C#

1 public static bool deleteFullRows() 2 { 3 // ↓ 元に戻す 4 bool deleteFullRows() 5 { 6 7 // …(中略) 8 9 void Update () { 10 // ↓ 外部から実行するように変更 11 public void countUp() {

Group.cs

C#

1public class Group : MonoBehaviour 2{ 3 private Vector3 screenPoint; 4 private Vector3 offset; 5// ↓ 変数追加 6public class Group : MonoBehaviour 7{ 8 private Vector3 screenPoint; 9 private Vector3 offset; 10 private Count count; 11 12 // …(中略) 13 14 transform.position = Grid.roundVec2(currentPosition); 15 //追加 16 Count.deleteFullRows(); 17 //横列と縦列を消去 18 Grid.deleteFullRows(); 19 Grid.deleteFullLines(); 20 // ↓ deleteFullRows()ではなく、countUp()を呼び出す 21 transform.position = Grid.roundVec2(currentPosition); 22 //追加 23 count.countUp(); 24 //横列と縦列を消去 25 Grid.deleteFullRows(); 26 Grid.deleteFullLines(); 27 28 // …(中略) 29 30 void Start() 31 { 32 33 } 34 // ↓ Countを取得 35 void Start() 36 { 37 count = FindObjectOfType<Count>(); 38 }

追記2:

Count.cs

C#

1 bool deleteFullRows() 2 { 3 for (int y = 0; y < Grid.h; ++y) 4 { 5 if (!Grid.isRowFull(y)) 6 { 7 return false; 8 } 9 } 10 return true; 11 }

以上のソースを、以下のようにしてみてください。

C#

1 bool deleteFullRows() 2 { 3 for (int y = 0; y < Grid.h; ++y) 4 { 5 if (Grid.isRowFull(y)) 6 { 7 return true; 8 } 9 } 10 return false; 11 }

投稿2017/09/09 10:31

編集2017/09/12 06:09
fiveHundred

総合スコア9778

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

sumikko6210

2017/09/09 10:39

ご回答ありがとうございます。 教えていただいた通りコードを修正しましたが、変化なしでした・・・
fiveHundred

2017/09/09 10:47

だとしたら、deleteFullRows()でチェックする前にブロックの消去が行われてしまっているような気がします。 ブロックの消去を行っている箇所のソースがあると、分かるかもしれません。
sumikko6210

2017/09/09 10:53

ブロックの消去を行っているGroup.csを追加いたしました。 確かにブロックが先に消えてしまっている気がします。 確認します
sumikko6210

2017/09/11 08:31 編集

返信遅くなって申し訳ありません。 ブロックの消去より早く処理するためにGroup.csに Count.deleteFullRows(); //横列と縦列を消去 Grid.deleteFullRows(); Grid.deleteFullLines(); と変更したのですが変化なしでした・・・。
fiveHundred

2017/09/11 13:14

deleteFullRows()には加算の処理はないのでは? 追記に修正案を書きましたので、確認してください。
sumikko6210

2017/09/12 05:54

何度も申し訳ございません。 いただいた修正案を元に書き直しましたが変化なしでした。 質問欄のコードも書き直しました。確認お願いします。
fiveHundred

2017/09/12 06:10

さらに追記を行いました。確認をお願いします。
sumikko6210

2017/09/12 06:20

無事にカウントすることができました! ご丁寧な対応ありがとうございました! ベストアンサーにさせていただきます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問