質問編集履歴

3

修正

2016/05/10 11:31

投稿

masaakitsuyoshi
masaakitsuyoshi

スコア102

test CHANGED
File without changes
test CHANGED
@@ -1,60 +1,386 @@
1
- ブロック崩しをandroidで作っています。
2
-
3
-
4
-
5
- ボールを弾くバーをdrawRectした短形で作りました。
6
-
7
-
8
-
9
- onTouchでそのバーを、滑らかに座標移動させたいです。
10
-
11
-
12
-
13
- 現在、タッチした座標にそのまま移動してしまっています。
14
-
15
-
16
-
17
- ように記述すればいいでしょうか?
18
-
19
-
20
-
21
-
22
-
23
- 追記
24
-
25
- ボール動きように滑らかにッチた位置に移動させたい
26
-
27
- 現在はタッチし位置に瞬間移動してしまって
28
-
29
-
30
-
31
- x座標を徐々加減ればい?
32
-
33
-
1
+ ブロック崩しを作っています。
2
+
3
+
4
+
5
+ クラス
6
+
7
+ Main
8
+
9
+ Ball
10
+
11
+ Bar
12
+
13
+ Block
14
+
15
+
16
+
17
+ Ball移動スピード
18
+
19
+ vx =15
20
+
21
+ vy=15
22
+
23
+
24
+
25
+ Barクラスののインスンスを利用てdrawrect()して短形を作成
26
+
27
+ onTouchでdrawrectされ短形を移動させた
28
+
29
+
30
+
31
+ タッチしたx座標にdrawrectされたバーが遷移るようにした
32
+
33
+ (Ballの移動スピードと同じにしたい)
34
+
35
+
36
+
37
+
38
+
39
+ bar.x //drawrectしたバーのx座標
40
+
41
+ touchX// (onTouchを利用)タップされた際のx座標
42
+
43
+
44
+
45
+ 現時点で bar.x = touchX しているため
46
+
47
+ drawrectされたバーが、瞬間移動している。
48
+
49
+
50
+
51
+ ※バーが消えないようにしたい。
52
+
53
+ [https://www.youtube.com/watch?v=bMGebET8hHs](https://www.youtube.com/watch?v=bMGebET8hHs)
54
+
55
+ 参考
56
+
57
+ [http://2byteresearch.blogspot.jp/2011/04/android_16.html](http://2byteresearch.blogspot.jp/2011/04/android_16.html)
58
+
59
+
60
+
61
+ 瞬間移動しているので、上の動画のように瞬間移動しないで移動するようにしたい
62
+
63
+
64
+
65
+ Main.java ontouch
66
+
67
+ ```
68
+
69
+ @Override
70
+
71
+ public boolean onTouchEvent(MotionEvent event) {
72
+
73
+
74
+
75
+ int x =(int)event.getRawX();//タッチした座標
76
+
77
+ int y=(int)event.getRawY();
78
+
79
+ touchAction=event.getAction();
80
+
81
+
82
+
83
+ switch (touchAction) {
84
+
85
+ case MotionEvent.ACTION_DOWN:
86
+
87
+ int screenX = x;
88
+
89
+ int screenY = y;
90
+
91
+
92
+
93
+ break;
94
+
95
+ case MotionEvent.ACTION_MOVE:
96
+
97
+ int diffX = bar.x - screenX;//差分
98
+
99
+ int diffY = bar.y - screenY;
100
+
101
+ diffX/2;
102
+
103
+ diffY/2;
104
+
105
+
106
+
107
+
108
+
109
+ break;
110
+
111
+ case MotionEvent.ACTION_UP:
112
+
113
+ break;
114
+
115
+
116
+
117
+ }
118
+
119
+
120
+
121
+ return super.onTouchEvent(event);
122
+
123
+ }
124
+
125
+
126
+
127
+
128
+
129
+ ```
130
+
131
+ Ball.java
132
+
133
+ ```Ball.java
134
+
135
+ public Ball(int _x, int _y, int width, int height) {
136
+
137
+ //初期座標をセット
138
+
139
+ x = (float) _x;
140
+
141
+ y = (float) _y;
142
+
143
+ //ビューの幅と高さをセット
144
+
145
+ view_w = width;
146
+
147
+ view_h = height;
148
+
149
+ vx = 15;//ボールの移動速度
150
+
151
+ vy = 15;
152
+
153
+ }
154
+
155
+ ```
156
+
157
+ Main.javaほぼ全文
34
158
 
35
159
  ```ここに言語を入力
36
160
 
161
+ package com.example.blockbreak;
162
+
163
+
164
+
165
+ import android.content.Context;
166
+
167
+
168
+
169
+
170
+
171
+ public class MainActivity extends AppCompatActivity {
172
+
173
+
174
+
175
+
176
+
177
+
178
+
37
- @Override
179
+ @Override
180
+
181
+ protected void onCreate(Bundle savedInstanceState) {
182
+
183
+ super.onCreate(savedInstanceState);
184
+
185
+ //カスタムビュークラスのインスタンスをコンテントにセット
186
+
187
+ setContentView(new CustomView(this));
188
+
189
+
190
+
191
+ Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
192
+
193
+ setSupportActionBar(toolbar);
194
+
195
+
196
+
197
+ }
198
+
199
+
200
+
201
+ class CustomView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
202
+
203
+
204
+
205
+
206
+
207
+
208
+
209
+ Block[] block,block2,block3,block4,block5,block6,block7,block8,block9,block10;
210
+
211
+ Bar bar; //バー
212
+
213
+ int view_w, view_h; // SurfaceViewの幅と高さ
214
+
215
+ Ball ball;
216
+
217
+ int num; //カウンターiの最大値変数 画面サイズによって表示個数を変えるため
218
+
219
+
220
+
221
+ //Blockの情報
222
+
223
+ int blockWidth = 40;//ブロックの半分の横の長さ
224
+
225
+ int blockHeight = 10;//ブロックの半分の高さ
226
+
227
+ int margin =5;
228
+
229
+
230
+
231
+ float touchX,touchY;
232
+
233
+ int touchAction;//x座標、y座標,アクション種別の取得
234
+
235
+
236
+
237
+
238
+
239
+
240
+
241
+ //コンストラクタ
242
+
243
+ public CustomView(Context context) {
244
+
245
+ super(context);
246
+
247
+ // SurfaceView描画に用いるコールバックを登録する。
248
+
249
+ getHolder().addCallback(this);
250
+
251
+ // 描画用の準備
252
+
253
+
254
+
255
+
256
+
257
+ // ViewGroup root = (ViewGroup)getWindow().getDecorView().findViewById(android.R.id.content);
258
+
259
+ // view_h = root.getHeight();
260
+
261
+ // view_w = root.getWidth();
262
+
263
+ // Log.d("width", "view_w:" +view_w);
264
+
265
+ // Log.d("height", "view_h:" +view_h);
266
+
267
+
268
+
269
+ WindowManager wm = (WindowManager)getSystemService(WINDOW_SERVICE);
270
+
271
+ // ディスプレイのインスタンス生成
272
+
273
+ Display disp = wm.getDefaultDisplay();
274
+
275
+ Point size = new Point();
276
+
277
+ disp.getSize(size);
278
+
279
+ view_w =size.x;
280
+
281
+ view_h =size.y;
282
+
283
+
284
+
285
+ //ボールを生成
286
+
287
+ ball = new Ball(view_w/2, view_h/3 , view_w, view_h );
288
+
289
+
290
+
291
+ // バーを生成 view_w/2画面の半分
292
+
293
+ bar = new Bar( view_w/2 , view_h - 150 );
294
+
295
+
296
+
297
+
298
+
299
+ }
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+ @Override
308
+
309
+ public void surfaceChanged(SurfaceHolder holder, int format, int width,
310
+
311
+ int height) {
312
+
313
+
314
+
315
+ }
316
+
317
+
318
+
319
+
320
+
321
+ // SurfaceView生成時に呼び出されるメソッド。
322
+
323
+ @Override
324
+
325
+ public void surfaceCreated(SurfaceHolder holder) {
326
+
327
+ // 背景
328
+
329
+ Canvas canvas = holder.lockCanvas();
330
+
331
+ canvas.drawColor(Color.BLACK);
332
+
333
+ holder.unlockCanvasAndPost(canvas);
334
+
335
+ // スレッド開始
336
+
337
+ mainLoop = new Thread(this);
338
+
339
+ mainLoop.start();
340
+
341
+
342
+
343
+ }
344
+
345
+
346
+
347
+
348
+
349
+ @Override
350
+
351
+ public void surfaceDestroyed(SurfaceHolder holder) {
352
+
353
+
354
+
355
+ }
356
+
357
+
358
+
359
+ @Override
38
360
 
39
361
  public boolean onTouchEvent(MotionEvent event) {
40
362
 
41
-
363
+ // Canvas canvas = getHolder().lockCanvas();
42
-
364
+
365
+
366
+
43
- int x =(int)event.getRawX();//タッチした座標
367
+ touchX =event.getRawX();
44
-
368
+
45
- int y=(int)event.getRawY();
369
+ touchY=event.getRawY();
46
370
 
47
371
  touchAction=event.getAction();
48
372
 
373
+ // float diffX = bar.x - touchX;
374
+
49
375
 
50
376
 
51
377
  switch (touchAction) {
52
378
 
53
379
  case MotionEvent.ACTION_DOWN:
54
380
 
55
- int screenX = x;
381
+ bar.x = touchX;
56
-
382
+
57
- int screenY = y;
383
+ bar.y = touchY;
58
384
 
59
385
 
60
386
 
@@ -62,15 +388,13 @@
62
388
 
63
389
  case MotionEvent.ACTION_MOVE:
64
390
 
391
+
392
+
65
- int diffX = bar.x - screenX;//差分
393
+ bar.x = touchX;
66
-
394
+
67
- int diffY = bar.y - screenY;
395
+ bar.y = touchY;
68
-
396
+
69
- diffX/2;
397
+ // canvas.drawRect(touchX-bar.halfBar,bar.y +10, touchX + bar.halfBar, bar.y +10,paint3);
70
-
71
- diffY/2;
72
-
73
-
74
398
 
75
399
 
76
400
 
@@ -84,6 +408,12 @@
84
408
 
85
409
  }
86
410
 
411
+ // if(flag) {
412
+
413
+ // bar.x =touchX -bar.halfBar;
414
+
415
+ // }
416
+
87
417
 
88
418
 
89
419
  return super.onTouchEvent(event);
@@ -94,18 +424,116 @@
94
424
 
95
425
 
96
426
 
427
+ @Override
428
+
429
+ public void run(){
430
+
431
+
432
+
433
+ while (true) {
434
+
435
+ Canvas canvas = getHolder().lockCanvas();
436
+
437
+ if (canvas != null)
438
+
439
+ {
440
+
441
+ //背景
442
+
443
+ canvas.drawColor(Color.BLACK);
444
+
445
+
446
+
447
+ //max720 1280 画面サイズ
448
+
449
+ num =(view_w - 32 + margin)/(blockWidth*2);//横ブロックの個数.
450
+
451
+
452
+
453
+ //---中略----
454
+
455
+
456
+
457
+
458
+
459
+ //バーを描画する left top right bottom
460
+
461
+ canvas.drawRect(bar.x - bar.halfBar , bar.y +bar.height , bar.x + bar.halfBar , bar.y - bar.height , paint3);
462
+
463
+ //Ballクラスからボールを描画
464
+
465
+ canvas.drawCircle( ball.x, ball.y, ball.size , paint5);
466
+
467
+
468
+
469
+
470
+
471
+
472
+
473
+ //Ballクラスのボールを移動
474
+
475
+ ball.x += ball.vx;
476
+
477
+ ball.y += ball.vy;
478
+
479
+ //TODO:ボールの中心で判定されてるので、ボールの幅で判定するようにする(Option)
480
+
481
+ //壁に衝突
482
+
483
+ if (ball.x < 0 || getWidth() < ball.x) ball.vx *= -1;
484
+
485
+ if (ball.y < 0 || getHeight() < ball.y) ball.vy *= -1;
486
+
487
+ //バーに衝突
488
+
489
+ if (ball.x + ball.size >=bar.x) ball.vx *= -1;
490
+
491
+ if (ball.x <= bar.x + bar.halfBar) ball.vx *= -1;
492
+
493
+ if (ball.y >= bar.y) ball.vx *= -1;
494
+
495
+
496
+
497
+
498
+
499
+
500
+
501
+ getHolder().unlockCanvasAndPost(canvas);
502
+
503
+
504
+
505
+ }
506
+
507
+ }
508
+
509
+
510
+
511
+ }
512
+
513
+
514
+
515
+
516
+
517
+
518
+
519
+
520
+
521
+ }
522
+
523
+
524
+
525
+
526
+
527
+
528
+
529
+
530
+
531
+
532
+
533
+ }
534
+
535
+
536
+
537
+
538
+
97
539
  ```
98
-
99
- バーの描写部分
100
-
101
- ```java
102
-
103
- // バーを生成 view_w/2画面の半分
104
-
105
- bar = new Bar( view_w/2 , view_h - 150 );
106
-
107
-
108
-
109
- canvas.drawRect(bar.x - bar.halfBar , bar.y +bar.height , bar.x + bar.halfBar , bar.y - bar.height , paint3);
110
-
111
- ```

2

追記しました

2016/05/10 11:31

投稿

masaakitsuyoshi
masaakitsuyoshi

スコア102

test CHANGED
File without changes
test CHANGED
@@ -38,9 +38,11 @@
38
38
 
39
39
  public boolean onTouchEvent(MotionEvent event) {
40
40
 
41
- touchX =event.getRawX();//touchXタッチされたx座標
42
41
 
42
+
43
+ int x =(int)event.getRawX();//タッチした座標
44
+
43
- touchY=event.getRawY();
45
+ int y=(int)event.getRawY();
44
46
 
45
47
  touchAction=event.getAction();
46
48
 
@@ -50,7 +52,9 @@
50
52
 
51
53
  case MotionEvent.ACTION_DOWN:
52
54
 
53
- bar.x =touchX;//barのx座標にタッチされたx座標入れる
55
+ int screenX = x;
56
+
57
+ int screenY = y;
54
58
 
55
59
 
56
60
 
@@ -58,7 +62,15 @@
58
62
 
59
63
  case MotionEvent.ACTION_MOVE:
60
64
 
65
+ int diffX = bar.x - screenX;//差分
66
+
67
+ int diffY = bar.y - screenY;
68
+
61
- bar.x =touchX;
69
+ diffX/2;
70
+
71
+ diffY/2;
72
+
73
+
62
74
 
63
75
 
64
76
 
@@ -72,9 +84,13 @@
72
84
 
73
85
  }
74
86
 
87
+
88
+
75
- return super.onTouchEvent(event);
89
+ return super.onTouchEvent(event);
76
90
 
77
91
  }
92
+
93
+
78
94
 
79
95
 
80
96
 

1

バーの動きについて追記しました

2016/05/10 07:08

投稿

masaakitsuyoshi
masaakitsuyoshi

スコア102

test CHANGED
File without changes
test CHANGED
@@ -15,6 +15,20 @@
15
15
 
16
16
 
17
17
  どのように記述すればいいでしょうか?
18
+
19
+
20
+
21
+
22
+
23
+ 追記
24
+
25
+ ボールの動きのように滑らかにタッチした位置に移動させたい
26
+
27
+ 現在はタッチした位置に瞬間移動してしまっている
28
+
29
+
30
+
31
+ x座標を徐々に加減すればいい?
18
32
 
19
33
 
20
34