title: Am i going wrong?? using view ondraw and thread to make a animate game
body: <p>i am new to android apps ,
  i just start to write a simple game.
  I used a Class extends View and using some thread to make a simple game
  i wanna ask if i go wrong direction ? should i use other way to do it?
  I think i am wrong on using thread in here right??
  I just use</p>
  
  <pre><code>Game gameview;
  gameview= new Game(this);
  setContentView(gameview);
  </code></pre>
  
  <p>to call this in class which extends activity </p>
  
  <pre><code>public class Game extends View {
  Bitmap background;
  Bitmap nomouthbear;
  Bitmap nomouthbearget;
  Bitmap apple;
  float bearX;
  float bearY;
  List&lt;Apple&gt; appleList;
  int appleCount = 0;
  float appleX;
  float appleY;
  float applescore;
  float applespeed;
  int totalscore = 0;
  float canvasheight;
  float canvaswidth;
  float randomNumber;// random number for apple appear in X
  float randomNumber2;// random number for score and speed
  float randomNumber3;
  int applecombo = 0; // count for how many apple that the bear eat in combo
  boolean checkbeareat; // for check if bear eat any of apple or not.
  boolean bearEating = false;
  Thread thread2;
  boolean ineatingthread = false;
  
  int time = 20; // game time =60sec
  
  public Game(Context context) {
      super(context);
  
      // setup image source
      nomouthbear = BitmapFactory.decodeResource(getResources(),
              R.drawable.nomouthbear_net);
      apple = BitmapFactory.decodeResource(getResources(), R.drawable.apple);
      nomouthbearget = BitmapFactory.decodeResource(getResources(),
              R.drawable.nomouthbearget);
      background = BitmapFactory.decodeResource(getResources(),
              R.drawable.background);
      // init bear x,y
      bearX=0;
      bearY =0;
      // setup background
      if (background != null) {
          setBackgroundDrawable(new BitmapDrawable(background));
      }
      // open a list which hold all apple
      appleList = new ArrayList&lt;Apple&gt;();
  
      // make a thread to creat apple every 2 sec
      Thread thread = new Thread() {
          @Override
          public void run() {
              try {
                  while (true) {
                      sleep(2000);
                      randomNumber = (float) Math.random();
                      randomNumber2 = (float) Math.random();
  
                      // avoid start at edge point
  
                      appleX = canvaswidth * randomNumber;
                      if (appleX &gt;= canvaswidth - apple.getWidth() / 2)
                          appleX = canvaswidth - apple.getWidth() / 2;
                      if (appleX &lt;= nomouthbear.getWidth() / 2)
                          appleX = nomouthbear.getWidth();
                      applescore = 1000 * randomNumber2;
                      // check if speed too low
                      applespeed = 10 * randomNumber2;
                      if (applespeed &lt;= 3) {
                          applespeed = 3;
                      }
                      // add new apple in the apple list
                      appleList.add(new Apple(appleX, 65, applescore,
                              applespeed));
                      appleCount++;
  
                  }
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      };
      // start the thread
      thread.start();
      // thread for timer
  
      Thread thread1 = new Thread() {
          @Override
          public void run() {
              try {
                  while (true)
  
                  {
  
                          sleep(1000);
                          if(time&gt;=0)
                      time--;
  
                  }
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      };
      thread1.start();
  
  //thread for controlling the bear eat pic appear time       
      Thread thread2 = new Thread() {
          @Override
          public void run() {
              try {
                  while (true)
  
                  {
  
                      if (bearEating == true) {
  
                              sleep(200);
                              bearEating = false;
  
                      }
  
  
                  }
              } catch (InterruptedException e) {
                  e.printStackTrace();
              }
          }
      };
      thread2.start();
  
  
  
  
  }
  
  @Override
  protected void onDraw(Canvas canvas) {
      // TODO Auto-generated method stub
      super.onDraw(canvas);
  
  
  
      if (time &lt;= 0) {
          Paint textPaint = new Paint();
          textPaint.setColor(Color.BLACK);
          textPaint.setTextAlign(Align.LEFT);
          textPaint.setTextSize(65);
          canvas.drawText(
                  "Game Over!",
                  canvas.getWidth() / 4, canvas.getHeight() / 2, textPaint);
          canvas.drawText(
                  "Your Score is ",
                  canvas.getWidth() / 4, canvas.getHeight() / 2+65, textPaint);
          canvas.drawText(
                  Integer.toString(totalscore),
                  canvas.getWidth() / 4, canvas.getHeight() / 2+130, textPaint);
  
  
  
  
  
  
      } else {
          checkbeareat = false;
          canvasheight = canvas.getHeight();
          canvaswidth = canvas.getWidth();
          // draw score text
          Paint textPaint = new Paint();
          textPaint.setColor(Color.BLACK);
          textPaint.setTextAlign(Align.LEFT);
          textPaint.setTextSize(65);
          canvas.drawText("Score: " + Integer.toString(totalscore), 50, 50,
                  textPaint);
          canvas.drawText("Time left: " + Integer.toString(time), 50, 120,
                  textPaint);
          // if have apple
          if (appleCount != 0) {
              for (int i = 0; i &lt; appleCount; i++) {
                  if (appleList.get(i).checkstate()) {
  
                      // make every apple fall down
  
                      if ((appleList.get(i).getAppleY() + apple.getHeight() / 2) &lt;= canvas
                              .getHeight()) {
  
                          appleList.get(i).setAppleY(
                                  appleList.get(i).getAppleY()
                                          + appleList.get(i).getspeed());
                      } else {
                          // appleList.get(i).setAppleY(appleList.get(i).getAppleY());
                          applecombo = 0;
                          appleList.get(i).destoryApple();
                      }
  
                      // check if bear eat the apple
  
                      if (bearX + nomouthbear.getWidth() / 2 &gt;= appleList
                              .get(i).getAppleX()
                              &amp;&amp; bearX &lt;= appleList.get(i).getAppleX()
                              &amp;&amp; bearY&gt;= appleList
                                      .get(i).getAppleY()
                              &amp;&amp; bearY-nomouthbear.getHeight()/2 &lt;= appleList.get(i).getAppleY()) {
                          // add score
                          totalscore += appleList.get(i).getAppleScore();
                          // change the state of apple to false so wont draw
                          // it
                          // again
                          appleList.get(i).destoryApple();
                          // draw bear ate
                          canvas.drawBitmap(nomouthbearget, bearX
                                  - nomouthbear.getWidth() / 2, bearY
                                  - nomouthbear.getHeight(), null);
                          checkbeareat = true;
                          bearEating = true;
  
                          applecombo++;
  
                      }
  
                      // draw apple
                      if (appleList.get(i).checkstate()) {
                          canvas.drawBitmap(
                                  apple,
                                  appleList.get(i).getAppleX()
                                          - (apple.getWidth() / 2),
                                  appleList.get(i).getAppleY()
                                          - (apple.getHeight() / 2), null);
                      }
  
                  }
              }
          }
  
          // draw bear
          // canvas.drawBitmap(nomouthbear, 0, 0, null);
          if (bearEating == false) {
  
                  if (bearX == 0 &amp;&amp; bearY == 0){
  
                      canvas.drawBitmap(nomouthbear, 0, canvas.getHeight()- nomouthbear.getHeight()*2, null);}
  
                  else{
                      canvas.drawBitmap(nomouthbear,
                              bearX - nomouthbear.getWidth() / 2, bearY
                                      - nomouthbear.getHeight(), null);}
  
  
          } else {
              canvas.drawBitmap(nomouthbearget,
                      bearX - nomouthbear.getWidth() / 2,
                      bearY - nomouthbear.getHeight(), null);
          }
          invalidate();
      }
  }
  
  @Override
  public boolean onTouchEvent(MotionEvent event) {
      // TODO Auto-generated method stub
  
      if (event.getX() &gt;= nomouthbear.getWidth() / 2
              &amp;&amp; event.getX() &lt;= canvaswidth - nomouthbear.getWidth() / 2) {
          bearX = event.getX();
      } else if (event.getX() &gt;= canvaswidth - nomouthbear.getWidth() / 2)
          bearX = canvaswidth - nomouthbear.getWidth() / 2;
      else
          bearX = nomouthbear.getWidth() / 2;
  
      bearY = canvasheight - nomouthbear.getHeight();
  
      return true;
  }
  
  }
  </code></pre>