###前提・実現したいこと
下記サイトのように、画面をタップした場所にボールが移動するプログラムを作成したいです。
https://goo.gl/5a8YB8
###発生している問題・エラーメッセージ
2つのクラスを作成し、実行すると
「プロジェクト名が繰り返し停止しています。」
という表示が出てきてしまいます。
###SampleSurficeView.java
java
1import android.content.Context; 2import android.view.MotionEvent; 3import android.view.SurfaceHolder; 4import android.view.SurfaceView; 5 6public class SampleSurficeView extends SurfaceView { 7 8 private SampleHolderCallBack cb; 9 10 public SampleSurficeView(Context context) { 11 super(context); 12 SurfaceHolder holder = getHolder(); 13 cb = new SampleHolderCallBack(); 14 holder.addCallback(cb); 15 } 16 17 @Override 18 public boolean onTouchEvent(MotionEvent event) { 19 float x = event.getX(); // X座標を取得 20 float y = event.getY(); // Y座標を取得 21 cb.getTouchPoint(x, y); 22 return true; 23 } 24}
###SampleHolderCallBack.java
java
1import android.graphics.Canvas; 2import android.graphics.Color; 3import android.graphics.Paint; 4import android.graphics.PorterDuff; 5import android.view.SurfaceHolder; 6 7public class SampleHolderCallBack implements SurfaceHolder.Callback, Runnable{ 8 9 private SurfaceHolder holder = null; 10 private Thread thread = null; 11 private boolean isAttached = true; 12 13 private float dx = 10, dy = 10; 14 private float width, height; 15 private int circle_x, circle_y; 16 17 private long t1 = 0, t2 = 0; // スリープ用 18 19 private float touch_x; //タッチされたX座標 20 21 @Override 22 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 23 // TODO 自動生成されたメソッド・スタブ 24 this.width = width; 25 this.height = height; 26 } 27 28 @Override 29 public void surfaceCreated(SurfaceHolder holder) { 30 // TODO 自動生成されたメソッド・スタブ 31 this.holder = holder; 32 thread = new Thread(this); 33 thread.start(); //スレッドを開始 34 } 35 36 @Override 37 public void surfaceDestroyed(SurfaceHolder holder) { 38 // TODO 自動生成されたメソッド・スタブ 39 isAttached = false; 40 thread = null; //スレッドを終了 41 } 42 43 @Override 44 public void run() { 45 // TODO 自動生成されたメソッド・スタブ 46 // メインループ(無限ループ) 47 while( isAttached ){ 48 t1 = System.currentTimeMillis(); 49 50 //丸の表示位置を動かす 51 if( circle_x < 0 || circle_x > this.width ){ 52 dx = -dx; 53 } 54 if( circle_y < 0 || circle_y > this.height ){ 55 dy = -dy; 56 } 57 58 circle_x += dx; 59 circle_y += dy; 60 61 //描画処理を開始 62 Canvas canvas = holder.lockCanvas(); 63 canvas.drawColor(0,PorterDuff.Mode.CLEAR ); 64 Paint paint = new Paint(); 65 paint.setColor(Color.WHITE); 66 canvas.drawCircle( circle_x, circle_y, 50, paint); 67 68 //描画処理を終了 69 holder.unlockCanvasAndPost(canvas); 70 71 // スリープ 72 t2 = System.currentTimeMillis(); 73 if(t2 - t1 < 16){ // 1000 / 60 = 16.6666 74 try { 75 Thread.sleep(16 - (t2 - t1)); 76 } catch (InterruptedException e) { 77 } 78 } 79 } 80 } 81 public void getTouchPoint( float x, float y ){ 82 circle_x = (int)x; 83 circle_y = (int)y; 84 } 85}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。