Android studio Game speed different devices

Viewed 115

I am making a game in Android Studio. Now my game is complete, but game speed is different on large displays...

I run my game with this timer:

if(timer == null){
  timer = new Timer();
  timer.schedule(new TimerTask() {
    @Override
    public void run() {
      if (start_flg) {
        handler.post(new Runnable() {
          @Override
          public void run() {
            changePos();
          }
        });
      }
    }
  }, 0, 20);
}

changePos() looks like this:

    public void changePos() {
SPEED_BOX =(screenHeight/280);
        
          long time = System.nanoTime();
          double delta_time = (double) ((time - last_time) / 1000000)/10;
          last_time = time;
        
          // Move Box
          if (action_flg) {
            // Touching
            boxY += SPEED_BOX*delta_time;
            box.setImageDrawable(imageBox1);
          } else {
            // Releasing
            boxY -= SPEED_BOX*delta_time;
            box.setImageDrawable(imageBox2);
          }
        
          // Check box position.
          if (boxY < 0) {
            boxY = 0;
            box.setImageDrawable(imageBox1);
          }
          if (frameWidth - boxSize < boxY) {
            boxY = frameWidth - boxSize;
            box.setImageDrawable(imageBox2);
          }
          
          box.setY(boxY);
    }

id correct that my deltaTime is allways between 1.5 to 2.9?

but every time I try it in different ways always game speed is not correct. Is it possible to make my game running same speed on different devices, different screen sizes?

1 Answers

The problem was that screenHeight was screens height in pixels but the game does not use the whole screen. This caused that the speed was different on different devices. So screenHeight should be changed to gamesLayout.getHeight().

Related