Exemple #1
0
        public void handleMessage(Message msg) {
          switch (msg.what) {
            case TIME_1S:
              int timeLeft = Integer.parseInt(timeText.getText().toString()) - 1;
              timeText.setText(Integer.toString(timeLeft));
              if (timeLeft == 0) {
                timer.cancel();
                gameOver.setMessage("Time is up! Again ?");
                gameOver.show();
              }
              break;
            case RESET_TIME:
              timeText.setText("100");
              timer = new Timer();

              timerTask =
                  new TimerTask() {
                    public void run() {
                      Message msg = new Message();
                      msg.what = TIME_1S;
                      handler.sendMessage(msg);
                    }
                  };
              timer.schedule(timerTask, 1000, 1000);
              break;

            case MOVE_IMAGE:
              if ((speedX > 0 && acceleratorX < 0) || (speedX < 0 && acceleratorX > 0))
                speedX -= 2 * acceleratorX;
              else speedX -= 0.1 * acceleratorX;

              if ((speedY > 0 && acceleratorY < 0) || (speedY < 0 && acceleratorY > 0))
                speedY += 2 * acceleratorY;
              else speedY += 0.1 * acceleratorY;

              int dx = (int) (speedX * 0.05);
              int dy = (int) (speedY * 0.05);
              View v = findViewById(R.id.image);
              int left = v.getLeft() + dx;
              int top = v.getTop() + dy;
              int right = v.getRight() + dx;
              int bottom = v.getBottom() + dy;

              if (left < 20) {
                left = 20;
                right = left + v.getWidth();
                speedX = 0;
              }
              if (right > screenWidth - 20) {
                right = screenWidth - 20;
                left = right - v.getWidth();
                speedX = 0;
              }
              if (top < 100) {
                top = 100;
                bottom = top + v.getHeight();
                speedY = 0;
              }
              if (bottom > screenHeight - 20) {
                bottom = screenHeight - 20;
                top = bottom - v.getHeight();
                speedY = 0;
              }

              if (arrayMap.isCollision(left, top, right, bottom) != 0) {

                MessageSend(COLLISION);
                top = v.getTop();
                bottom = v.getBottom();

                if (arrayMap.isCollision(left, top, right, bottom) == 1) {
                  speedY = 0;
                  top = v.getTop();
                  bottom = v.getBottom();
                } else if (arrayMap.isCollision(left, top, right, bottom) == 2) {
                  speedX = 0;
                  left = v.getLeft();
                  right = v.getRight();
                }
              } else if (arrayMap.isArrive(left, top, right, bottom)) {
                MessageSend(NEXT_LEVEL);
                left = v.getLeft();
                top = v.getTop();
                right = v.getRight();
                bottom = v.getBottom();

                LinearLayout map = (LinearLayout) findViewById(R.id.map);
                map.removeAllViews();

                speedX = speedY = 0;
              } else if (arrayMap.isDead(left, top, right, bottom)) {
                left = v.getLeft();
                top = v.getTop();
                right = v.getRight();
                bottom = v.getBottom();
                speedX = speedY = 0;
                removeImage();
                gameOver.show();
                MessageSend(ACCELERATOR_STOP);
              }

              v.layout(left, top, right, bottom);
              break;
            case NEXT_LEVEL:
              LevelDatabaseHelper dbhelper = new LevelDatabaseHelper(OnGame.this, "LevelData", 1);
              SQLiteDatabase db = dbhelper.getReadableDatabase();
              //	    			db.execSQL("update levellock set lock=? where difficulty=? and level=?",
              //	    					new Object[]{0,difficulty,level+1});
              LevelDatabaseHelper.Unlock(db, level, difficulty);
              nextLevel.show();
              break;
            case BALL_START:
              //	    			BallStart();
              break;
            case ACCELERATOR_STOP:
              SManager.unregisterListener(listener);
              acceleratorX = 0;
              acceleratorY = 0;
              speedX = 0;
              speedY = 0;
              break;
          }
        }
Exemple #2
0
  @Override
  public boolean onTouch(View v, MotionEvent event) {

    int action = event.getAction();
    switch (action) {
      case MotionEvent.ACTION_DOWN:
        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();
        break;
      case MotionEvent.ACTION_MOVE:
        int dx = (int) event.getRawX() - lastX;
        int dy = (int) event.getRawY() - lastY;

        int left = v.getLeft() + dx;
        int top = v.getTop() + dy;
        int right = v.getRight() + dx;
        int bottom = v.getBottom() + dy;

        if (left < 20) {
          left = 20;
          right = left + v.getWidth();
        }
        if (right > screenWidth - 20) {
          right = screenWidth - 20;
          left = right - v.getWidth();
        }
        if (top < 100) {
          top = 100;
          bottom = top + v.getHeight();
        }
        if (bottom > screenHeight - 20) {
          bottom = screenHeight - 20;
          top = bottom - v.getHeight();
        }
        if (arrayMap.isCollision(left, top, right, bottom) != 0) {
          MessageSend(COLLISION);
          if (arrayMap.isCollision(left, top, right, bottom) == 2) {
            speedY = 0;
            top = v.getTop();
            bottom = v.getBottom();
          } else if (arrayMap.isCollision(left, top, right, bottom) == 1) {
            left = v.getLeft();
            right = v.getRight();
            speedX = 0;
          }
        } else if (arrayMap.isArrive(left, top, right, bottom)) {
          MessageSend(NEXT_LEVEL);
          left = v.getLeft();
          top = v.getTop();
          right = v.getRight();
          bottom = v.getBottom();
          speedX = speedY = 0;
        } else if (arrayMap.isDead(left, top, right, bottom)) {
          //					SManager.registerListener(null, sensor,SensorManager.SENSOR_DELAY_FASTEST);
          left = v.getLeft();
          top = v.getTop();
          right = v.getRight();
          bottom = v.getBottom();
          speedX = speedY = 0;
        }
        v.layout(left, top, right, bottom);
        lastX = (int) event.getRawX();
        lastY = (int) event.getRawY();
        break;
      case MotionEvent.ACTION_UP:
        break;
    }
    return false;
  }