画面右側は問題、操作画面(上下左右)、左側でねこが動きます。現段階では、操作ボタン(button1…うえ、button2…した、button3…ひだり、button4…みぎ)を押したら猫が一マスずつ移動します。また、障害物である犬の位置に到達すると初期位置に戻るためのダイアログが発生し、家に到達すると「せいかい!」のダイアログが発生します。さらに、ボタンを押した際、プログラムの流れの欄に矢印マーク(↑↓→←)が順番に表示されます。
ここでは、猫の位置を管理するために変数xと変数yを使用しています。
実現したいことは、猫をうえ、した、ひだり、みぎのボタンを押した際に動かすのではなく、「スタートボタン」を押した際に、「プログラムの流れ」欄に表示された通りに猫を動かし、ダイアログを発生させることです。
データベースを用いる必要はあるのか…
java
1public class MainActivity extends AppCompatActivity implements Animator.AnimatorListener { 2 final static float TRANSLATE_X = 310.0f; // 1回の操作でX軸方向にいくつ動かすか 3 final static float TRANSLATE_Y = 310.0f; // 1回の操作でY軸方向にいくつ動かすか 4 5 // 盤面上の開始座標を初期値として設定する 6 private int x = 0; 7 private int y = 0; 8 private String text; 9 private ArrayList<Integer> arrayList ; 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 Button btn1 = (Button) findViewById(R.id.button1); // うえ 17 Button btn2 = (Button) findViewById(R.id.button2); // した 18 Button btn3 = (Button) findViewById(R.id.button3); // ひだり 19 Button btn4 = (Button) findViewById(R.id.button4); // みぎ 20 Button btn5 = (Button) findViewById(R.id.button5); // やりなおし 21 Button btn6 = (Button) findViewById(R.id.button6); // スタート 22 23 text = ""; 24 final TextView result = (TextView) findViewById(R.id.result); 25 result.setText(""); 26 27 arrayList = new ArrayList<>(); 28 29 30 btn1.setOnClickListener(new View.OnClickListener() { 31 @Override 32 public void onClick(View v) { 33 arrayList.add(upAnimation(); 34 // 「うえ」を押したときの処理 35 if (y > -3) { 36 TextView result = (TextView) findViewById(R.id.result); 37 text += "<font color=#98fb98>↑</font>"; 38 result.setText(Html.fromHtml(text)); 39 } 40 } 41 }); 42 btn2.setOnClickListener(new View.OnClickListener() { 43 @Override 44 public void onClick(View v) { 45 arrayList.add(downAnimation(); 46 // 「した」を押したときの処理 47 if (y < 0) { 48 TextView result = (TextView) findViewById(R.id.result); 49 text += "<font color=#fffacd>↓</font>"; 50 result.setText(Html.fromHtml(text)); 51 } 52 } 53 }); 54 btn3.setOnClickListener(new View.OnClickListener() { 55 @Override 56 public void onClick(View v) { 57 arrayList.add(leftAnimation(); 58 // 「ひだり」を押したときの処理 59 if (x > 0) { 60 TextView result = (TextView) findViewById(R.id.result); 61 text += "<font color=#ffc0cb>←</font>"; 62 result.setText(Html.fromHtml(text)); 63 } 64 } 65 }); 66 btn4.setOnClickListener(new View.OnClickListener() { 67 @Override 68 public void onClick(View v) { 69 arrayList.add(rightAnimation(); 70 // 「みぎ」を押したときの処理 71 if (x < 2) { 72 TextView result = (TextView) findViewById(R.id.result); 73 text += "<font color=#afeeee>→</font>"; 74 result.setText(Html.fromHtml(text)); 75 } 76 } 77 }); 78 btn5.setOnClickListener(new View.OnClickListener() { 79 @Override 80 public void onClick(View v) { 81 text = ""; 82 TextView result = (TextView) findViewById(R.id.result); 83 result.setText(""); 84 } 85 }); 86 btn6.setOnClickListener(new View.OnClickListener(){ 87 @Override 88 public void onClick(View v) { 89 for(int i=0; i< arrayList.size(); i++){ 90 } 91 } 92 }); 93 } 94 95 private void animationImageView(String property, float start, float end) { 96 ImageView imageView = (ImageView) findViewById(MyCat); 97 ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, property, start, end); 98 animator.setDuration(1000); 99 animator.addListener(this); 100 animator.start(); 101 } 102 103 private void animationImageReset() { 104 final ImageView MyCat = (ImageView) findViewById(R.id.MyCat); 105 ObjectAnimator.ofFloat(MyCat, "translationX", 0.0f, 0.0f).start(); 106 ObjectAnimator.ofFloat(MyCat, "translationY", 0.0f, 0.0f).start(); 107 } 108 109 private void encountDog() { 110 if (x == 1 && y == -2) { 111 AlertDialog.Builder dl = new AlertDialog.Builder(MainActivity.this); 112 dl.setTitle("ざんねん!!"); 113 dl.setMessage("いぬはしょうがいぶつだよ"); 114 dl.setPositiveButton("OK", new DialogInterface.OnClickListener() { 115 @Override 116 public void onClick(DialogInterface dialog, int which) { 117 x = 0; 118 y = 0; 119 animationImageReset(); 120 121 text = ""; 122 TextView result = (TextView) findViewById(R.id.result); 123 result.setText(""); 124 } 125 }); 126 dl.show(); 127 } 128 if (x == 2 && y == -3) { 129 AlertDialog.Builder dl = new AlertDialog.Builder(MainActivity.this); 130 dl.setTitle("せいかい!!"); 131 dl.setPositiveButton("こたえ", new DialogInterface.OnClickListener() { 132 @Override 133 public void onClick(DialogInterface dialog, int which) { 134 AlertDialog.Builder dl = new AlertDialog.Builder(MainActivity.this); 135 dl.setTitle("こたえ"); 136 dl.setMessage("①→→↑↑↑②↑↑↑→→③→↑→↑↑など"); 137 dl.setPositiveButton("つぎのもんだいへすすむ", null); 138 dl.show(); 139 } 140 }); 141 dl.setNegativeButton("つぎのもんだいへすすむ", null); //ボタン 142 dl.show(); 143 } 144 } 145 @Override 146 public void onAnimationStart(Animator animation) { 147 } 148 149 @Override 150 public void onAnimationEnd(Animator animation) { 151 // アニメーションが終了するとここが実行される 152 } 153 154 @Override 155 public void onAnimationCancel(Animator animation) { 156 } 157 158 @Override 159 public void onAnimationRepeat(Animator animation) { 160 } 161 162 private void upAnimation(){ 163 // 「うえ」を押したときの処理 164 if (y > -3) { 165 float start = TRANSLATE_Y * y; 166 float end = start - TRANSLATE_Y; 167 animationImageView("translationY", start, end); 168 y--; 169 encountDog(); 170 } 171 } 172 private void downAnimation(){ 173 // 「した」を押したときの処理 174 if (y < 0) { 175 float start = TRANSLATE_Y * y; 176 float end = start + TRANSLATE_Y; 177 animationImageView("translationY", start, end); 178 y++; 179 encountDog(); 180 } 181 } 182 private void leftAnimation(){ 183 // 「ひだり」を押したときの処理 184 if (x > 0) { 185 float start = TRANSLATE_X * x; 186 float end = start - TRANSLATE_X; 187 animationImageView("translationX", start, end); 188 x--; 189 encountDog(); 190 } 191 } 192 private void rightAnimation(){ 193 // 「みぎ」を押したときの処理 194 if (x < 2) { 195 float start = TRANSLATE_X * x; 196 float end = start + TRANSLATE_X; 197 animationImageView("translationX", start, end); 198 x++; 199 encountDog(); 200 } 201 } 202}
回答2件
あなたの回答
tips
プレビュー