ブロック崩しをandroid studioで作成しています。
現在、MainActivityにdrawCircleしてボールらしきものを描いています。
これをBallクラスを使って描画、動作させたいです。
ひとまずBallクラスを使って描画はできたのですが、動作がうまくいきません。
Ballクラスにmoveメソッドを作成し、動きを定義しました。
drawCircleした円にこれを使いたいのですが、どのように書けばいいのでしょうか?
ーーーーー
また、それがわからなかったため、ひとまずmoveメソッドを使わずにMainActivityに下記動作を記述してみましたが、なぜか動作しませんでした。
//Ballクラスで作成したボールの動き
ball.x += ball.vx;
ball.y += ball.vy;
if (ball.x < 0 || getWidth() < ball.x) ball.vx *= -1;
if (ball.y < 0 || getHeight() < ball.y) ball.vy *= -1;
他の二つのボールは動いているので、???状態です。
お力貸していただけると幸いです。
Main
java
1package com.example.blockbreak; 2 3---中略--- 4 5 class CustomView extends SurfaceView implements SurfaceHolder.Callback, Runnable { 6 7 //スレッドクラス 8 Thread mainLoop; 9 // 描画用 10 Paint paint; 11 Paint paint2; 12 Paint paint3; 13 Paint paint4; 14 Paint paint5; 15 Paint paint6; 16 17 // 円のX,Y座標 18 private int circleX = 50; 19 private int circleY = 50; 20 private int circle2X = 30; 21 private int circle2Y = 30; 22 // 円の移動量 23 private int circleVx = 15; 24 private int circleVy = 15; 25 private int circle2Vx = 8; 26 private int circle2Vy = 15; 27 28 Block[] block; 29 Bar bar; //バー 30 int view_w, view_h; // SurfaceViewの幅と高さ 31 Ball ball; 32 int num; //カウンターiの最大値変数 画面サイズによって表示個数を変えるため 33 private float touch_x; // タッチされたx座標 34 35 36 37 38 //コンストラクタ 39 public CustomView(Context context) { 40 super(context); 41 // SurfaceView描画に用いるコールバックを登録する。 42 getHolder().addCallback(this); 43 // 描画用の準備 44 paint = new Paint(); 45 paint2 = new Paint(); 46 paint3 = new Paint(); 47 paint4 = new Paint(); 48 paint5 = new Paint(); 49 paint6 = new Paint(); 50 51 paint.setColor(Color.RED); 52 paint2.setColor(Color.CYAN); 53 paint3.setColor(Color.GREEN); 54 paint4.setColor(Color.RED); 55 paint5.setColor(Color.YELLOW); 56 paint6.setColor(Color.BLUE); 57 58 59 60 } 61 62 63 64 @Override 65 public void surfaceChanged(SurfaceHolder holder, int format, int width, 66 int height) { 67 // TODO 今回は何もしない。 68 69 } 70 71 72 // SurfaceView生成時に呼び出されるメソッド。 73 @Override 74 public void surfaceCreated(SurfaceHolder holder) { 75 // 背景 76 Canvas canvas = holder.lockCanvas(); 77 canvas.drawColor(Color.BLACK); 78 holder.unlockCanvasAndPost(canvas); 79 // スレッド開始 80 mainLoop = new Thread(this); 81 mainLoop.start(); 82 83 } 84 85 86 @Override 87 public void surfaceDestroyed(SurfaceHolder holder) { 88 // TODO 今回は何もしない。 89 } 90 91 @Override 92 public void run(){ 93 94 while (true) { 95 Canvas canvas = getHolder().lockCanvas(); 96 if (canvas != null) 97 { 98 //背景 99 canvas.drawColor(Color.BLACK); 100 101 view_w = canvas.getWidth(); 102 view_h = canvas.getHeight(); 103 touch_x = view_w/2; 104 105 // バーを生成 view_w/2画面の半分 106 bar = new Bar( view_w/2 , view_h - 100 ); 107 //ボールを生成 108 ball = new Ball(view_w/2, view_h/3 , view_w, view_h ); 109 110 block = new Block[21]; 111 112---中略--- 113 114 115 } 116 //バーを描画する left top right bottom 117 canvas.drawRect(bar.x - bar.halfBar , bar.y , bar.x + bar.halfBar , bar.y + 20 , paint3); 118 // 円1を描画する 119 canvas.drawCircle(circleX, circleY, 20, paint); 120 //円2を描画する 121 canvas.drawCircle(circle2X,circle2Y,20,paint2); 122 //Ballクラスからボールを描画 123 canvas.drawCircle( ball.x, ball.y, ball.size , paint5); 124 125 //Ballクラスのボールを動かす 126 ball.x += ball.vx; 127 ball.y += ball.vy; 128 if (ball.x < 0 || getWidth() < ball.x) ball.vx *= -1; 129 if (ball.y < 0 || getHeight() < ball.y) ball.vy *= -1; 130 131 132 // 円の座標を移動させる 133 circleX += circleVx; 134 circleY += circleVy; 135 circle2X += circle2Vx; 136 circle2Y += circle2Vy; 137 // 画面の領域を超えた? 138 if (circleX < 0 || getWidth() < circleX) circleVx *= -1; 139 if (circleY < 0 || getHeight() < circleY) circleVy *= -1; 140 if (circle2X < 0 || getWidth() < circle2X) circle2Vx *= -1; 141 if (circle2Y < 0 || getHeight() < circle2Y) circle2Vy *= -1; 142 143 144 145 getHolder().unlockCanvasAndPost(canvas); 146 147 } 148 } 149 150 151 152 153 154 } 155 } 156 157 158 159 160 161} 162 163
Ball
java
1public class Ball { 2 //ボールのサイズ 3 int size = 20; 4 // 表示座標 5 float x, y; 6 // 速度 7 double vx, vy; 8 // 画面の幅と高さ 9 int view_w, view_h; 10 // 弾の生死フラグ 11 boolean isLive = true; 12 13 //コンストラクタ 14 public Ball( int _x, int _y, int width, int height ){ 15 //初期座標をセット 16 x = (float)_x; 17 y = (float)_y; 18 //ビューの幅と高さをセット 19 view_w = width; 20 view_h = height; 21 vx = 15; 22 vy = 15; 23 } 24 public void move(){ 25 // 円の座標を移動させる 26 //circleX += circleVxの部分 27 x = x + (float)vx; 28 y = y + (float)vy; 29 30 // 壁に当たった時の処理、速度を入れ替える 31 if(x > view_w - size ){ 32 //画面右 33 vx = -vx; 34 x = ( view_w - size ); 35 }else if( x < 0 ){ 36 //画面左 37 vx = -vx; 38 x = 0; 39 } 40 if( y < 0 ){ 41 //画面上 42 vy = -vy; 43 y = 0; 44 }else if( y > view_h ){ 45 //画面下 46 isLive = false; 47 } 48 } 49}
回答1件
あなたの回答
tips
プレビュー