画面右側は問題、操作画面(上下左右)、左側でねこが動きます。
実現したいことは、ねこ(MyCat)が犬(inu)の位置に到達したらエラーのダイアログを表示させたいです。
ここでは、座標で指定するのか、ボタンを→↑↑もしくは↑↑→などどと押した場合に表示させるのか、どういった方法が良いのでしょうか。
java
1 2public class MainActivity extends AppCompatActivity { 3 final static float TRANSLATE_X = 300.0f; // 1回の操作でX軸方向にいくつ動かすか 4 final static float TRANSLATE_Y = 300.0f; // 1回の操作でY軸方向にいくつ動かすか 5 6 // 盤面上の開始座標を初期値として設定する 7 private int x = 0; 8 private int y = 0; 9 10 11 @Override 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 15 16 Button button1 = (Button) findViewById(R.id.button1); // うえ 17 Button button2 = (Button) findViewById(R.id.button2); // した 18 Button button3 = (Button) findViewById(R.id.button3); // ひだり 19 Button button4 = (Button) findViewById(R.id.button4); // みぎ 20 Button button5 = (Button) findViewById(R.id.button5); // やりなおし 21 Button button6 = (Button) findViewById(R.id.button6); // こたえあわせ 22 23 button1.setOnClickListener(new View.OnClickListener() { 24 @Override 25 public void onClick(View v) { 26 // 「うえ」を押したときの処理 27 if (y > -3) { 28 float start = TRANSLATE_Y * y; 29 float end = start - TRANSLATE_Y; 30 animationImageView("translationY", start, end); 31 y--; 32 } 33 } 34 }); 35 button2.setOnClickListener(new View.OnClickListener() { 36 @Override 37 public void onClick(View v) { 38 // 「した」を押したときの処理 39 if (y < 0) { 40 float start = TRANSLATE_Y * y; 41 float end = start + TRANSLATE_Y; 42 animationImageView("translationY", start, end); 43 y++; 44 } 45 } 46 }); 47 button3.setOnClickListener(new View.OnClickListener() { 48 @Override 49 public void onClick(View v) { 50 // 「ひだり」を押したときの処理 51 if (x > 0) { 52 float start = TRANSLATE_X * x; 53 float end = start - TRANSLATE_X; 54 animationImageView("translationX", start, end); 55 x--; 56 } 57 } 58 }); 59 button4.setOnClickListener(new View.OnClickListener() { 60 @Override 61 public void onClick(View v) { 62 // 「みぎ」を押したときの処理 63 if (x < 2) { 64 float start = TRANSLATE_X * x; 65 float end = start + TRANSLATE_X; 66 animationImageView("translationX", start, end); 67 x++; 68 } 69 } 70 }); 71 button5.setOnClickListener(new View.OnClickListener() { 72 @Override 73 public void onClick(View v) { 74 x = 0; 75 y = 0; 76 animationImageReset(); 77 } 78 }); 79 80 } 81 82 private void animationImageView(String property, float start, float end) { 83 ImageView imageView = (ImageView) findViewById(MyCat); 84 ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, property, start, end); 85 animator.setDuration(1000); 86 animator.start(); 87 } 88 89 private void animationImageReset() { 90 final ImageView MyCat = (ImageView) findViewById(R.id.MyCat); 91 ObjectAnimator.ofFloat(MyCat, "translationX", 0.0f, 0.0f).start(); 92 ObjectAnimator.ofFloat(MyCat, "translationY", 0.0f, 0.0f).start(); 93 } 94}
java
1public class MyView extends View { 2 public MyView(Context context) { 3 super(context); 4 } 5 6 public MyView(Context context, AttributeSet attrs) { 7 super(context, attrs); 8 } 9 10 Paint paint = new Paint(); 11 12 Bitmap house = BitmapFactory.decodeResource(getResources(), R.drawable.house); 13 Bitmap inu = BitmapFactory.decodeResource(getResources(), R.drawable.inu); 14 15 16 @Override 17 protected void onDraw(Canvas canvas) { 18 paint.setColor(Color.argb(255, 0, 0, 0)); 19 20 paint.setStyle(Paint.Style.STROKE); 21 canvas.drawRect(50, 50, 1000, 1300, paint); 22 canvas.drawLine(367, 50, 367, 1300, paint); 23 canvas.drawLine(683, 50, 683, 1300, paint); 24 canvas.drawLine(50, (float) 362.5, 1000, (float) 362.5, paint); 25 canvas.drawLine(50, 675, 1000, 675, paint); 26 canvas.drawLine(50, (float) 987.5, 1000, (float) 987.5, paint); 27 28 canvas.drawBitmap(house, 700, 100, paint); 29 canvas.drawBitmap(inu, 380, 380, paint); 30 31 } 32}
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/11/02 11:49
2017/11/02 12:03
2017/11/02 12:05
2017/11/02 12:30
2017/11/02 12:49 編集
2017/11/02 13:08
2017/11/06 07:18
2017/11/06 07:52 編集
2017/11/06 07:50