androidでブロック崩しを作っています。
drawRectでボールを弾くバーを作成、タッチしたらx軸で左右に移動するようにしたいです。
if(bar.x + bar.half_size < touch_x){ bar.right(touch_x); }else{ bar.left(touch_x); }
このようなコードをMainに記述して動かしたいのですが、このbarに、drawRectで描いた短形を対応させる?のはどうしたらいいでしょうか?
または別のやり方の方が賢いですか?
Main
while (true) { Canvas canvas = getHolder().lockCanvas(); if (canvas != null) { //背景 canvas.drawColor(Color.BLACK); view_w = canvas.getWidth(); view_h = canvas.getHeight(); touch_x = view_w/2; // バーを生成 view_w/2画面の半分 bar = new Bar( view_w/2 , view_h - 100 ); //バーを描画する left top right bottom canvas.drawRect(bar.x - bar.halfBar , bar.y , bar.x + bar.halfBar , bar.y + 20 , paint3); //Ballクラスからボールを描画 canvas.drawCircle( ball.x, ball.y, ball.size , paint5); //Ballクラスのmoveメソッド 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; getHolder().unlockCanvasAndPost(canvas); } }
Bar
public class Bar { //表示座標 public float x; public float y; //バーのサイズ public final float halfBar = 100; //コンストラクタ public Bar( int _x, int _y ){ //初期座標をセット x = (float)_x; y = (float)_y; } // 右移動 public void right(float touch_x){ x = touch_x - halfBar;//x座標に,タッチされた座標-バーの半分の長さ } // 左移動 public void left(float touch_x){ x = touch_x - halfBar;//x座標に,タッチされた座標-バーの半分の長さ } }
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/28 05:01