コード public class MainActivity extends Activity implements Runnable{ Handler mHandler; Thread thread; FrameLayout frameLayout; SampleView sv; ArrayList<Ball> list = new ArrayList<Ball>(); Ball ball = new Ball(180.0); Ball ball2 = new Ball(160.0); int time = 0; double mul = 0.1; double times; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); frameLayout = new FrameLayout(this); setContentView(frameLayout); sv = new SampleView(this); list.add(ball); list.add(ball2); sv.setBall(list); frameLayout.addView(sv); mHandler = new Handler(); thread = new Thread(this); thread.start(); } @Override public void run(){ ball.setX(); ball2.setX(); for(time = 0; time < 100; time++){ times = time*mul; try{ Thread.sleep(50); } catch(InterruptedException e){ } ball.setY(times); ball2.setY(times); mHandler.post(new Runnable(){ @Override public void run(){ sv.invalidate(); } }); } } }
コード public class SampleView extends View{ Paint paint = new Paint(); ArrayList<Ball> ball; public SampleView(Context context) { super(context); } public void setBall(ArrayList<Ball> ball){ this.ball = ball; } protected void onDraw(Canvas canvas){ paint.setColor(Color.RED); paint.setStrokeWidth(10); canvas.drawPoint(ball.get(0).getX(), ball.get(0).getY(), paint); canvas.drawPoint(ball.get(1).getX(), ball.get(1).getY(), paint); } }
コード public class Ball{ int x; int y; double V0x = 0.0; double V0y; double Vx; double Vy; double G = 9.8*2; //コンストラクタ public Ball(double V0y){ this.V0y = V0y; } public void setX(){ this.x = (int)(Math.random()*700) + 50; } public void setY(double times){ Vy = V0y - G*times; this.y = (int)(1100 - (V0y*times - G*times*times/2)); } public int getX(){ return this.x; } public int getY(){ return this.y; } }
上のプログラムは上にボール(点)を投げ上げたときの動きを示したプログラムです。
Ballクラスを作ってインスタンス化することでボールを複数作れました。
ですが、同時にボールを上げることはできたのですが、時間をずらしてあげることが出来ません。
はじめにボールを上げて、1秒後に2つ目のボール、3秒後に3つ目のボールを上げるようにするにはどうすればいいのでしょうか?
よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/04/10 15:41